Context.startForegroundService() did not then call

2019-01-04 06:46发布

I am using Service Class on the Android O OS.

I plan to use the Service in the background.

The Android recommendation states that startService should use startForegroundService.

If you use startForegroundService, the Service throws a Context.startForegroundService() did not then call Service.startForeground() error.

What's wrong with this?

20条回答
劳资没心,怎么记你
2楼-- · 2019-01-04 07:13

I called ContextCompat.startForegroundService(this, intent) to start the service then

In service onCreate

 @Override
 public void onCreate() {
        super.onCreate();

        if (Build.VERSION.SDK_INT >= 26) {
            String CHANNEL_ID = "my_channel_01";
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);

            ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);

            Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setContentTitle("")
                    .setContentText("").build();

            startForeground(1, notification);
        }
}
查看更多
萌系小妹纸
3楼-- · 2019-01-04 07:13

Problem With Android O API 26

If you stop the service right away (so your service does not actually really runs (wording / comprehension) and you are way under the ANR interval, you still need to call startForeground before stopSelf

https://plus.google.com/116630648530850689477/posts/L2rn4T6SAJ5

Tried this Approach But it Still creates an error:-

if (Util.SDK_INT > 26) {
    mContext.startForegroundService(playIntent);
} else {
    mContext.startService(playIntent);
}

I Am Using this until the Error is Resolved

mContext.startService(playIntent);
查看更多
干净又极端
4楼-- · 2019-01-04 07:13

I just check the PendingIntent null or nor not before calling the context.startForegroundService(service_intent) function.

this works for me

PendingIntent pendingIntent=PendingIntent.getBroadcast(context,0,intent,PendingIntent.FLAG_NO_CREATE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && pendingIntent==null){
            context.startForegroundService(service_intent);
        }
        else
        {
            context.startService(service_intent);
        }
}
查看更多
乱世女痞
5楼-- · 2019-01-04 07:17

This all goes away if you rewrite your service to be started w/ bindService.

An example of how to do this can be seen at: https://github.com/paulpv/ForegroundServiceAPI26/tree/bound

A diff of my "repro" branch can been seen at: https://github.com/paulpv/ForegroundServiceAPI26/compare/repro...bound?expand=1

查看更多
叛逆
6楼-- · 2019-01-04 07:18

This error also occurs on Android 8+ when Service.startForeground(int id, Notification notification) is called while id is set to 0.

id int: The identifier for this notification as per NotificationManager.notify(int, Notification); must not be 0.

查看更多
啃猪蹄的小仙女
7楼-- · 2019-01-04 07:22

From Google's docs on Android 8.0 behavior changes:

The system allows apps to call Context.startForegroundService() even while the app is in the background. However, the app must call that service's startForeground() method within five seconds after the service is created.

Solution: Call startForeground() in onCreate() for the Service which you use Context.startForegroundService()

See also: Background Execution Limits for Android 8.0 (Oreo)

查看更多
登录 后发表回答