2017-06-12 59 views
0

我想继续运行一个服务其内容的应用程序被关闭后收到的消息[点击最近使用的应用按钮,然后关闭它。我尝试过使用START_STICKY,隔离的过程,但没有任何工作。是否有可能在最新的Android版本?有服务运行时应用程序从最近关闭应用

+0

您可以在一个单独的进程中运行你的服务。 – Christopher

+0

我想这样做,我的推杆进程名,并分离成真,但应用进行了killd。 – NooB8374

+0

如果您的服务回报onstartcommand START_STICKY,比如果应用程序被杀害,将再次重新启动服务。 入住这里 - https://developer.android.com/reference/android/app/Service.html#START_STICKY –

回答

0

尝试startForeground(notitficationId, notificationObject);命令onStartCommand()

0
private void startForground(){ 
    Intent notificationIntent = new Intent(this,MainActivity.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("App Name") 
           .setContentIntent(pendingIntent) 
           .build(); 

    startForeground(NOTIFICATION_ID, notification); 
} 

如果您不想显示通知或图标,但还是要在后台运行,试图this answer

+0

我不想显示任何图标。希望将其作为后台服务运行。 – NooB8374

0

制作粘服务,将保证您重新启动后应用已关闭,但您需要致电onStartComand()中的方法。

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    // do your job here 
    // start reading your incoming messages 
    readMessages(); 
    return START_STICKY; 
} 
+0

START_STICKY不起作用。当我从最近使用过的应用程序中刷出服务时,服务会死亡。 – NooB8374

+0

是的,但应该在死后开始。您无法保持服务始终运行。 –

0

尝试

public int onStartCommand(Intent intent, int flags, int startId) 
{ 
.......... 

return super.onStartCommand(intent, flags, startId); 
} 

但是当你关闭应用程序onStartCommand从一开始就重新启动它是否已经完成了它的工作或不!

相关问题