Android - implementing startForeground for a service?

前端 未结 11 1284
不思量自难忘°
不思量自难忘° 2020-11-22 06:26

So I\'m not sure where/how to implement this method to make my service run in the foreground. Currently I start my service by the following in another activity:

<         


        
11条回答
  •  深忆病人
    2020-11-22 07:26

    In addition to RAWA answer, this peace of code:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForegroundService(intent)
    } else {
        startService(intent)
    }
    

    You can change to:

    ContextCompat.startForegroundService(context, yourIntent);
    

    If you will look inside this method then you can see that this method do all checking work for you.

提交回复
热议问题