Android-为服务实现startForeground?


124

因此,我不确定在哪里/如何实现此方法以使我的服务在前台运行。目前,我在另一个活动中通过以下步骤启动服务:

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

然后在myServices的onCreate()中尝试startForeground()...?

Notification notification = new Notification();
startForeground(1, notification);

是的,我有点迷茫,不确定如何执行此操作。


嗯,这是行不通的,至少就我所知我的服务仍然可以作为后台服务并被杀死而言。
JDS

Answers:


131

我将从完全填写内容开始Notification这是一个示例项目,演示的使用startForeground()


8
是否可以在没有通知的情况下使用startForeground()?还是我们以后可以更新相同的通知?
JRC 2012年

2
您是否有特定的原因1337
科迪

33
@DoctorOreo:它在应用程序中必须是唯一的,尽管不一定在设备上是唯一的。我选择1337是因为1337。:-)
CommonsWare'2

@JRC问题是一个好问题。是否可以在没有通知的情况下使用startForeground()?
Snicolas 2012年

2
@Snicolas:感谢您指出Android的缺陷。我将努力解决此问题。
CommonsWare,2012年

78

在您的主要活动中,使用以下代码启动该服务:

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

然后在您的服务中,onCreate()您将构建通知并将其设置为前台,如下所示:

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

@mike如何从MainActivity更新此通知?
Roon13年

1
@ Roon13使用ID,在这种情况下为1337 ...您应该能够建立新的通知并使用ID调用startForeground
mikebertiean


@mikebertiean如何从MainActivity调用startForeground?流程完成后,如何从MainActvity清除通知?
Roon13年

@mikebertiean我知道必须在Service类中再次调用startForeground,但是如何?我是否必须再次调用startService()?
Roon13年

30

这是我将服务设置为前台的代码:

private void runAsForeground(){
    Intent notificationIntent = new Intent(this, RecorderMainActivity.class);
    PendingIntent pendingIntent=PendingIntent.getActivity(this, 0,
            notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

    Notification notification=new NotificationCompat.Builder(this)
                                .setSmallIcon(R.drawable.ic_launcher)
                                .setContentText(getString(R.string.isRecording))
                                .setContentIntent(pendingIntent).build();

    startForeground(NOTIFICATION_ID, notification);

}

我需要使用PendingIntent构建一个通知,以便可以从通知开始我的主要活动。

要删除通知,只需调用stopForeground(true);即可。

在onStartCommand()中调用它。请参考我的代码:https : //github.com/bearstand/greyparrot/blob/master/src/com/xiong/richard/greyparrot/Mp3Recorder.java


如果删除调用stopForeground(true)的通知,则将取消startforeground服务
sdelvalle57

6
您从哪里调用此方法?
Srujan Barai 2015年

7
Intent.FLAG_ACTIVITY_NEW_TASK在的上下文中无效PendingIntent
mixel

30

Oreo 8.1解决方案

我遇到了一些问题,例如RemoteServiceException由于最新版本的Android频道ID无效,。这就是我解决的方法:

活动内容

override fun onCreate(savedInstanceState: Bundle?) {
    val intent = Intent(this, BackgroundService::class.java)

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

BackgroundService:

override fun onCreate() {
    super.onCreate()
    startForeground()
}

private fun startForeground() {

    val service = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    val channelId =
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                createNotificationChannel()
            } else {
                // If earlier version channel ID is not used
                // https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context)
                ""
            }

    val notificationBuilder = NotificationCompat.Builder(this, channelId )
    val notification = notificationBuilder.setOngoing(true)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setPriority(PRIORITY_MIN)
            .setCategory(Notification.CATEGORY_SERVICE)
            .build()
    startForeground(101, notification)
}


@RequiresApi(Build.VERSION_CODES.O)
private fun createNotificationChannel(): String{
    val channelId = "my_service"
    val channelName = "My Background Service"
    val chan = NotificationChannel(channelId,
            channelName, NotificationManager.IMPORTANCE_HIGH)
    chan.lightColor = Color.BLUE
    chan.importance = NotificationManager.IMPORTANCE_NONE
    chan.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
    val service = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    service.createNotificationChannel(chan)
    return channelId
}

相当于Java

public class YourService extends Service {

    // Constants
    private static final int ID_SERVICE = 101;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }

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

        // do stuff like register for BroadcastReceiver, etc.

        // Create the Foreground Service
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String channelId = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? createNotificationChannel(notificationManager) : "";
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
        Notification notification = notificationBuilder.setOngoing(true)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setPriority(PRIORITY_MIN)
                .setCategory(NotificationCompat.CATEGORY_SERVICE)
                .build();

        startForeground(ID_SERVICE, notification);
    }

    @RequiresApi(Build.VERSION_CODES.O)
    private String createNotificationChannel(NotificationManager notificationManager){
        String channelId = "my_service_channelid";
        String channelName = "My Foreground Service";
        NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
        // omitted the LED color
        channel.setImportance(NotificationManager.IMPORTANCE_NONE);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        notificationManager.createNotificationChannel(channel);
        return channelId;
    }
}

8
您可以ContextCompat.startForegroundService(Context,Intent)在“活动”中使用它将做正确的事情。(developer.android.com/reference/android/support/v4/content/...
西蒙·费瑟斯通

3
您可能要使用.setCategory(NotificationCompat.CATEGORY_SERVICE)而不是Notification.CATEGORY_SERVICE您的最小API <21
某处某人

6
请注意,应用定位Build.VERSION_CODES.P(API等级28)或更高版本必须申请许可Manifest.permission.FOREGROUND_SERVICE才能使用startForeground()-看到developer.android.com/reference/android/app/...
瓦迪姆·科托夫

21

除了RAWA答案,此代码的安全性:

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

您可以更改为:

ContextCompat.startForegroundService(context, yourIntent);

如果您要查看此方法的内部,则可以看到该方法为您完成了所有检查工作。


9

如果要使IntentService成为前台服务

那么你应该onHandleIntent()像这样覆盖

Override
protected void onHandleIntent(@Nullable Intent intent) {


    startForeground(FOREGROUND_ID,getNotification());     //<-- Makes Foreground

   // Do something

    stopForeground(true);                                // <-- Makes it again a normal Service                         

}

如何发出通知?

简单。这是getNotification()方法

public Notification getNotification()
{

    Intent intent = new Intent(this, SecondActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);


    NotificationCompat.Builder foregroundNotification = new NotificationCompat.Builder(this);
    foregroundNotification.setOngoing(true);

    foregroundNotification.setContentTitle("MY Foreground Notification")
            .setContentText("This is the first foreground notification Peace")
            .setSmallIcon(android.R.drawable.ic_btn_speak_now)
            .setContentIntent(pendingIntent);


    return foregroundNotification.build();
}

深入了解

当服务成为前台服务时会发生什么

有时候是这样的

在此处输入图片说明

什么是前台服务?

前台服务,

  • 通过提供通知,确保用户主动意识到后台正在发生某些事情。

  • (最重要的)在内存不足时不会被系统杀死

前台服务的用例

在Music App中实现歌曲下载功能


5

在onCreate()中为“ OS> = Build.VERSION_CODES.O”添加给定的代码服务类

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

     .................................
     .................................

    //For creating the Foreground Service
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    String channelId = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? getNotificationChannel(notificationManager) : "";
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
    Notification notification = notificationBuilder.setOngoing(true)
            .setSmallIcon(R.mipmap.ic_launcher)
           // .setPriority(PRIORITY_MIN)
            .setCategory(NotificationCompat.CATEGORY_SERVICE)
            .build();

    startForeground(110, notification);
}



@RequiresApi(Build.VERSION_CODES.O)
private String getNotificationChannel(NotificationManager notificationManager){
    String channelId = "channelid";
    String channelName = getResources().getString(R.string.app_name);
    NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
    channel.setImportance(NotificationManager.IMPORTANCE_NONE);
    channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
    notificationManager.createNotificationChannel(channel);
    return channelId;
}

在清单文件中添加此权限:

 <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

1

通过使用处理服务的startCommand的意图。

 stopForeground(true)

此调用将使该服务从前台状态中删除,从而在需要更多内存时将其终止。 这不会停止服务运行。为此,您需要调用stopSelf()或相关方法。

传递值truefalse指示是否要删除通知。

val ACTION_STOP_SERVICE = "stop_service"
val NOTIFICATION_ID_SERVICE = 1
...  
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
    super.onStartCommand(intent, flags, startId)
    if (ACTION_STOP_SERVICE == intent.action) {
        stopForeground(true)
        stopSelf()
    } else {
        //Start your task

        //Send forground notification that a service will run in background.
        sendServiceNotification(this)
    }
    return Service.START_NOT_STICKY
}

stopSelf()调用销毁时处理任务。

override fun onDestroy() {
    super.onDestroy()
    //Stop whatever you started
}

创建一个通知,以保持服务在前台运行。

//This is from Util class so as not to cloud your service
fun sendServiceNotification(myService: Service) {
    val notificationTitle = "Service running"
    val notificationContent = "<My app> is using <service name> "
    val actionButtonText = "Stop"
    //Check android version and create channel for Android O and above
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        //You can do this on your own
        //createNotificationChannel(CHANNEL_ID_SERVICE)
    }
    //Build notification
    val notificationBuilder = NotificationCompat.Builder(applicationContext, CHANNEL_ID_SERVICE)
    notificationBuilder.setAutoCancel(true)
            .setDefaults(NotificationCompat.DEFAULT_ALL)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.ic_location)
            .setContentTitle(notificationTitle)
            .setContentText(notificationContent)
            .setVibrate(null)
    //Add stop button on notification
    val pStopSelf = createStopButtonIntent(myService)
    notificationBuilder.addAction(R.drawable.ic_location, actionButtonText, pStopSelf)
    //Build notification
    val notificationManagerCompact = NotificationManagerCompat.from(applicationContext)
    notificationManagerCompact.notify(NOTIFICATION_ID_SERVICE, notificationBuilder.build())
    val notification = notificationBuilder.build()
    //Start notification in foreground to let user know which service is running.
    myService.startForeground(NOTIFICATION_ID_SERVICE, notification)
    //Send notification
    notificationManagerCompact.notify(NOTIFICATION_ID_SERVICE, notification)
}

在通知上提供一个停止按钮,以在用户需要时停止服务。

/**
 * Function to create stop button intent to stop the service.
 */
private fun createStopButtonIntent(myService: Service): PendingIntent? {
    val stopSelf = Intent(applicationContext, MyService::class.java)
    stopSelf.action = ACTION_STOP_SERVICE
    return PendingIntent.getService(myService, 0,
            stopSelf, PendingIntent.FLAG_CANCEL_CURRENT)
}

1

注意:如果您的应用程序的目标是API级别26或更高级别,则除非应用程序本身位于前台,否则系统会限制使用或创建后台服务。

如果应用程序需要创建前台服务,则应调用startForegroundService()该方法创建了后台服务,但是该方法向系统发出信号,表明该服务会将自己提升到前台。

创建服务后,该服务必须调用其 startForeground() method within five seconds.


1
希望您在谈论当前的问题。否则,在社区#1没有这样的规则
法里德

生产就绪的环境代码中的@RogerGusmao不会总是保存您的项目。此外-我的答案上下都有很多很棒的代码示例。.我的项目在发行过程中出现了问题,正是因为我对startForegroundService方法
一无所知

0

就我而言,这完全不一样,因为我没有参加在奥利奥(Oreo)启动该服务的活动。

以下是我用来解决此前台服务问题的步骤-

public class SocketService extends Service {
    private String TAG = this.getClass().getSimpleName();

    @Override
    public void onCreate() {
        Log.d(TAG, "Inside onCreate() API");
        if (Build.VERSION.SDK_INT >= 26) {
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
            mBuilder.setSmallIcon(R.drawable.ic_launcher);
            mBuilder.setContentTitle("Notification Alert, Click Me!");
            mBuilder.setContentText("Hi, This is Android Notification Detail!");
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            // notificationID allows you to update the notification later on.
            mNotificationManager.notify(100, mBuilder.build());
            startForeground(100, mBuilder.mNotification);
        }
        Toast.makeText(getApplicationContext(), "inside onCreate()", Toast.LENGTH_LONG).show();
    }


    @Override
    public int onStartCommand(Intent resultIntent, int resultCode, int startId) {
        Log.d(TAG, "inside onStartCommand() API");

        return startId;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "inside onDestroy() API");

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}

在启动此服务之后,我在cmd下面触发了-


adb -s“ + serial_id +” shell是startforegroundservice -n com.test.socket.sample / .SocketService


因此,这可以帮助我在不使用Oreo设备进行活动的情况下开始服务:)


0

@mikebertiean解决方案几乎可以解决问题,但是我遇到了其他问题-我使用了Gingerbread系统,我不想添加一些额外的程序包来运行通知。终于我找到了:https : //android.googlesource.com/platform/frameworks/support.git+/f9fd97499795cd47473f0344e00db9c9837eea36/v4/gingerbread/android/support/v4/app/NotificationCompatGingerbread.java

然后我遇到了另一个问题-通知在运行时会扼杀我的应用程序(如何解决此问题:Android:如何避免单击Notification调用onCreate()),所以总的来说,我在服务中的代码看起来像这样(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);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.