Android:当应用程序被杀时,保持服务运行

use*_*872 43 service android background intentservice

IntentService即使应用程序被杀,我也想继续在后台运行.并且被"杀死"我的意思是按住主页按钮很长时间 - > 查看所有正在运行的应用程序 - >将我的应用程序放在一边 - > 应用程序被杀按下后退按钮很长时间 - > 应用程序被杀

我的代码如下.在我的MainActivity中:

Intent intent = new Intent(this, MyService.class);
this.startService(intent);
Run Code Online (Sandbox Code Playgroud)

在我的MyService中:

public class MyService extends IntentService {

@Override
protected void onHandleIntent(Intent intent) {
    System.out.println("MyService started");
    run();
}

private void run() {
    while (true){
        System.out.println("MyService still running");
        doSomething();
        waitSomeTime();
    }
}

}
Run Code Online (Sandbox Code Playgroud)

我看到应用程序打开时服务正在运行.当我通过主页按钮最小化应用程序时,它仍在运行.当我通过后退按钮关闭应用程序时,它仍在运行.但如果我如上所述杀死它,它就会停止.我该如何解决这个问题?

Bhe*_*aar 35

如果您的应用程序启动了您的服务,那么实际上您的服务正在主进程上运行.因此,当应用程序被杀死时,服务也将被停止.所以你可以做的是,从onTaskRemoved你的服务方法发送广播如下:

 Intent intent = new Intent("com.android.ServiceStopped");
 sendBroadcast(intent);
Run Code Online (Sandbox Code Playgroud)

并有一个广播接收器,将再次启动服务.我试过了.服务从所有类型的杀戮重新开始.

  • 这是正确的答案.我在[博客文章]中写了一个简单的例子(http://fabcirablog.weebly.com/blog/creating-a-never-ending-background-service-in-android) (14认同)
  • 有时当 android 指定它需要杀死一些进程以释放更多内存时,它甚至不会调用 `onTaskRemoved` 表单服务。它只会杀死它。 (5认同)
  • 我用的是联想TB3-730X。当应用程序被用户杀死时, onTaskRemoved 不会被调用,我也尝试过 onDestroy 。它也不起作用。请建议我任何解决方案 (2认同)

Say*_*Sil 29

所有的答案看起来都是正确的,所以我会继续在这里给出一个完整的答案.

首先,最简单的方法是在手动杀死应用程序时,在Android中启动广播,然后定义一个自定义以在此之后触发服务重新启动.BroadcastReceiver

现在让我们跳转到代码.


在中创建您的服务 YourService.java

请注意该onCreate()方法,我们为大于Android Oreo的 Build版本启动前台服务的方式不同.这是因为最近引入了严格的通知政策,我们必须定义自己的通知渠道才能正确显示它们.

this.sendBroadcast(broadcastIntent);onDestroy()方法是异步发送一个广播动作名称的声明"restartservice".我们稍后将使用它作为重启我们服务的触发器.

在这里,我们定义了一个简单的Timer任务,它每隔1秒打印一个计数器值,并Log在每次打印时自行递增.

public class YourService extends Service {
public int counter=0;

    @Override
    public void onCreate() {
        super.onCreate();
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O)
            startMyOwnForeground();
        else
            startForeground(1, new Notification());
    }

    @RequiresApi(Build.VERSION_CODES.O)
    private void startMyOwnForeground()
    {
        String NOTIFICATION_CHANNEL_ID = "example.permanence";
        String channelName = "Background Service";
        NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
        chan.setLightColor(Color.BLUE);
        chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        assert manager != null;
        manager.createNotificationChannel(chan);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
        Notification notification = notificationBuilder.setOngoing(true)
                .setContentTitle("App is running in background")
                .setPriority(NotificationManager.IMPORTANCE_MIN)
                .setCategory(Notification.CATEGORY_SERVICE)
                .build();
        startForeground(2, notification);
    }


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


    @Override
    public void onDestroy() {
        super.onDestroy();
        stoptimertask();

        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction("restartservice");
        broadcastIntent.setClass(this, Restarter.class);
        this.sendBroadcast(broadcastIntent);
    }



    private Timer timer;
    private TimerTask timerTask;
    public void startTimer() {
        timer = new Timer();
        timerTask = new TimerTask() {
            public void run() {
                Log.i("Count", "=========  "+ (counter++));
            }
        };
        timer.schedule(timerTask, 1000, 1000); //
    }

    public void stoptimertask() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

创建一个广播接收器以响应您的自定义广播 Restarter.java

具有"restartservice"您刚定义的操作名称的广播YourService.java现在应该触发一个将重新启动您的服务的方法.这是BroadcastReceiver在Android中使用.

我们覆盖内置onRecieve()方法BroadcastReceiver以添加将重新启动服务的语句.在startService()无法按照预期的工作,在以上的Android 8.1奥利奥,如严格的背景下政策将很快重启后终止服务,一旦应用程序被杀害.因此,我们使用startForegroundService()for更高版本并显示连续通知以保持服务运行.

public class Restarter extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("Broadcast Listened", "Service tried to stop");
        Toast.makeText(context, "Service restarted", Toast.LENGTH_SHORT).show();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(new Intent(context, YourService.class));
        } else {
            context.startService(new Intent(context, YourService.class));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

定义您MainActivity.java在应用启动时调用服务.

这里我们定义一个单独的isMyServiceRunning()方法来检查后台服务的当前状态.如果服务没有运行,我们通过使用启动它startService().

由于应用程序已在前台运行,因此我们无需将服务作为前台服务启动,以防止自身被终止.

请注意,onDestroy()我们专门调用stopService(),以便调用我们的重写方法.如果没有这样做,那么在app被杀死之后服务就会自动结束,而不会调用我们修改过的onDestroy()方法YourService.java

public class MainActivity extends AppCompatActivity {
    Intent mServiceIntent;
    private YourService mYourService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mYourService = new YourService();
        mServiceIntent = new Intent(this, mYourService.getClass());
        if (!isMyServiceRunning(mYourService.getClass())) {
            startService(mServiceIntent);
        }
    }

    private boolean isMyServiceRunning(Class<?> serviceClass) {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                Log.i ("Service status", "Running");
                return true;
            }
        }
        Log.i ("Service status", "Not running");
        return false;
    }


    @Override
    protected void onDestroy() {
        stopService(mServiceIntent);
        super.onDestroy();
    }
}
Run Code Online (Sandbox Code Playgroud)

最后在你的账号中注册 AndroidManifest.xml

以上三个类都需要单独注册AndroidManifest.xml.

请注意,我们定义了一个intent-filter操作名称作为"restartservice"其中Restarter.java注册为receiver.这可确保BroadcastReciever在系统遇到具有给定操作名称的广播时调用我们的自定义.

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <receiver
        android:name="Restarter"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="restartservice" />
        </intent-filter>
    </receiver>

    <activity android:name="MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name="YourService"
        android:enabled="true" >
    </service>
</application>
Run Code Online (Sandbox Code Playgroud)

如果应用程序从任务管理器中被杀死,现在应该再次重新启动服务.只要用户没有Force Stop来自" 应用程序设置"的应用程序,此服务将继续在后台运行.

  • 服务的 `onDestroy` 可能不会被调用:/sf/answers/506626571/ (6认同)
  • 当我们已经从 onStartCommand() 返回 START_STICKY 时,这意味着如果服务被终止,操作系统将重新启动服务。那么为什么我们需要在服务的 onDestroy() 中启动服务的广播?重新启动不是已经被操作系统处理了吗? (3认同)
  • 当应用程序被杀死或在奥利奥后台运行时,这仍然无法在后台运行。它仅在应用程序处于前台时才有效。..请帮忙解决这个问题。 (2认同)
  • 谢谢!我更新了我的答案以确保知道这种可能性。我今天学了些新东西。 (2认同)

Ima*_*shi 10

在您的服务中,添加以下代码。

@Override
public void onTaskRemoved(Intent rootIntent){
    Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
    restartServiceIntent.setPackage(getPackageName());

    PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmService.set(
    AlarmManager.ELAPSED_REALTIME,
    SystemClock.elapsedRealtime() + 1000,
    restartServicePendingIntent);

    super.onTaskRemoved(rootIntent);
 }
Run Code Online (Sandbox Code Playgroud)

  • 要消除“Missing PendingIntent mutability flag”警告,请将“PendingIntent.FLAG_ONE_SHOT”替换为“PendingIntent.FLAG_ONE_SHOT |” PendingIntent.FLAG_IMMUTABLE`。 (4认同)

Ris*_*hah 6

原因是您正在尝试使用 IntentService。这是API 文档中的一行

IntentService 执行以下操作:

处理完所有启动请求后停止服务,因此您无需调用 stopSelf()。

因此,如果您希望服务无限期地运行,我建议您扩展 Service 类。然而,这并不能保证您的服务将无限期地运行。如果你的服务是低优先级的,那么你的服务仍然有机会在低内存状态下被内核杀死。所以你有两个选择:
1)通过调用该方法保持它在前台运行startForeground()
2)如果服务被杀死,请重新启动该服务。这是文档中示例的一部分,其中讨论了服务被终止后重新启动服务

 public int onStartCommand(Intent intent, int flags, int startId) {
      Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();

      // For each start request, send a message to start a job and deliver the 
      // start ID so we know which request we're stopping when we finish the job 
      Message msg = mServiceHandler.obtainMessage();
      msg.arg1 = startId;
      mServiceHandler.sendMessage(msg);

      // If we get killed, after returning from here, restart 
      return START_STICKY;
  }  
Run Code Online (Sandbox Code Playgroud)


kit*_*rk3 5

在onstart命令put START_STICKY...这个服务不会杀死,除非它做了太多任务而内核想要为它杀死它...

@Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.i("LocalService", "Received start id " + startId + ": " + intent);
            // We want this service to continue running until it is explicitly
            // stopped, so return sticky.
            return START_STICKY;
        }
Run Code Online (Sandbox Code Playgroud)

  • 在某个时候,如果用户杀死了应用程序,也许我们应该尊重用户并让服务终止。 (3认同)