如何在oreo中启动后台服务?

时间:2018-09-28 09:37:28

标签: android service android-8.0-oreo

默认情况下,所有服务都在startService()之前的版本中使用oreo在后​​台启动,但是在oreo中有一些在后台启动服务的限制,我可以在其中启动后台服务吗? oreo using startService()吗?

2 个答案:

答案 0 :(得分:1)

只要您的应用程序处于前台状态,就可以使用startService(),如果您的应用程序进入后台并且调用startService(),您将获得IllegalStateException

或者,您可以使用startForeground()来启动服务

从文档

  

应用程序位于前台时,它可以自由创建和运行前台和后台服务。当应用进入后台时,它会有几分钟的窗口,仍允许其创建和使用服务。在该窗口结束时,该应用程序被认为是空闲的。此时,系统将停止应用程序的后台服务,就像该应用程序已调用服务的Service.stopSelf()方法一样

检查Documentation以获取更多信息

答案 1 :(得分:0)

您可以在后台服务中运行服务。但是,如果您想运行后台操作,而不管该应用程序是否位于前台中,并且您没有将该服务绑定到服务器,那么我将使用前台服务。所以在您的主要通话中:

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

然后,当您使用服务时,必须记录前台服务正在运行。您可以调用此方法以在前台处理它(有点笨拙):

 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();
}

您还必须在清单中(活动标记之间)记录服务的存在:

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

顶喜欢并发表评论,如果您需要帮助来制作通知图标