Android 9 (Pie) Only: Context.startForegroundService() did not then call Service.startForeground() - Works fine on Oreo

后端 未结 3 1404
陌清茗
陌清茗 2021-02-04 03:04

We adjusted our ongoing notification for Oreo and it worked great. Now, on Pie only (not happening on Oreo devices), we\'re getting the titled error. Has something changed in

3条回答
  •  南笙
    南笙 (楼主)
    2021-02-04 03:36

    I stopped to get the error after implementing below code in ONCREATE

    Intent intent1 = new Intent(this, YourActivity.class);
              try {
                    startService(intent1);
                }catch ( Exception e1){
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        this.startForegroundService(intent1);
                    }else {
                        Crashlytics.log("crash for first time, trying another.");
                        this.startService(intent1);
                    }
                }
    

    For me sounds strange but is working fine without crash.

提交回复
热议问题