Android - implementing startForeground for a service?

前端 未结 11 1113
不思量自难忘°
不思量自难忘° 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:18

    I'd start by completely filling in the Notification. Here is a sample project demonstrating the use of startForeground().

    0 讨论(0)
  • 2020-11-22 07:22

    From your main activity, start the service with the following code:

    Intent i = new Intent(context, MyService.class); 
    context.startService(i);
    

    Then in your service for onCreate() you would build your notification and set it as foreground like so:

    Intent notificationIntent = new Intent(this, MainActivity.class);
    
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                    notificationIntent, 0);
    
    Notification notification = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.app_icon)
                    .setContentTitle("My Awesome App")
                    .setContentText("Doing some work...")
                    .setContentIntent(pendingIntent).build();
    
    startForeground(1337, notification);
    
    0 讨论(0)
  • 2020-11-22 07:25

    Note: If your app targets API level 26 or higher, the system imposes restrictions on using or creating background services unless the app itself is in the foreground.

    If an app needs to create a foreground service, the app should call startForegroundService(). That method creates a background service, but the method signals to the system that the service will promote itself to the foreground.

    Once the service has been created, the service must call its startForeground() method within five seconds.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 07:28

    @mikebertiean solution almost did the trick, but I had this problem with additional twist -- I use Gingerbread system and I didn't want to add some extra package just to run notification. Finally I found: https://android.googlesource.com/platform/frameworks/support.git+/f9fd97499795cd47473f0344e00db9c9837eea36/v4/gingerbread/android/support/v4/app/NotificationCompatGingerbread.java

    then I hit additional problem -- notification simply kills my app when it runs (how to solve this problem: Android: How to avoid that clicking on a Notification calls onCreate()), so in total my code in service looks like this (C#/Xamarin):

    Intent notificationIntent = new Intent(this, typeof(MainActivity));
    // make the changes to manifest as well
    notificationIntent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
    PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, notificationIntent, 0);
    Notification notification = new Notification(Resource.Drawable.Icon, "Starting service");
    notification.SetLatestEventInfo(this, "MyApp", "Monitoring...", pendingIntent);
    StartForeground(1337, notification);
    
    0 讨论(0)
提交回复
热议问题