how to start a background service in oreo?

主宰稳场 提交于 2019-12-14 03:59:41

问题


by default all the service starts in Background using startService() before oreo version , but in oreo there is some restriction to start a service in background, Can I start a background service in oreo using startService() ?


回答1:


You can use startService() as long as your app is in foreground , if your app goes background and you call startService() you will get IllegalStateException

Alternatively you can use startForeground() to start a service

From documentation

While an app is in the foreground, it can create and run both foreground and background services freely. When an app goes into the background, it has a window of several minutes in which it is still allowed to create and use services. At the end of that window, the app is considered to be idle. At this time, the system stops the app's background services, just as if the app had called the services' Service.stopSelf() methods

check Documentation for more info




回答2:


You can run a service in background service. But if you want to run a background operations regardless if the app is in the foreground is not and you're not binding the service to a server then I'd use a foreground service. So in your main call this:

if(Build.VERSION.SDK_INT >25){
            startForegroundService(new Intent(this, Service.class));
        }else{
            startService(new Intent(this, Service.class));
        }

Then when you're in your service you have to document that the foreground service is running. You can call this method to handle documenting it in the foreground (it is a little clumsy):

 private void startRunningInForeground() {

    //if more than or equal to 26
    if (Build.VERSION.SDK_INT >= 26) {

        //if more than 26
        if(Build.VERSION.SDK_INT > 26){
            String CHANNEL_ONE_ID = "Package.Service";
            String CHANNEL_ONE_NAME = "Screen service";
            NotificationChannel notificationChannel = null;
            notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,
                    CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_MIN);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setShowBadge(true);
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            if (manager != null) {
                manager.createNotificationChannel(notificationChannel);
            }

            Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.background_running);
            Notification notification = new Notification.Builder(getApplicationContext())
                    .setChannelId(CHANNEL_ONE_ID)
                    .setContentTitle("Recording data")
                    .setContentText("App is running background operations")
                    .setSmallIcon(R.drawable.background_running)
                    .setLargeIcon(icon)
                    .build();

            Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            notification.contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);

            startForeground(101, notification);
        }
        //if version 26
        else{
            startForeground(101, updateNotification());
        }
    }
    //if less than version 26
    else{
        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("App")
                .setContentText("App is running background operations")
                .setSmallIcon(R.drawable.background_running)
                .setOngoing(true).build();

        startForeground(101, notification);
    }
}

private Notification updateNotification() {

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MainActivity.class), 0);

    return new NotificationCompat.Builder(this)
            .setContentTitle("Activity log")
            .setTicker("Ticker")
            .setContentText("app is running background operations")
            .setSmallIcon(R.drawable.background_running)
            .setContentIntent(pendingIntent)
            .setOngoing(true).build();
}

you'll also have to document the presence of a service in the manifest (in between the activity tags):

<service android:name = ".Service"/>

like and comment if you need help making a notification icon



来源:https://stackoverflow.com/questions/52552450/how-to-start-a-background-service-in-oreo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!