MainActivity 关闭后停止服务(已编辑)

Stop a service after MainActivity is closed (EDITED)

我想我一点都不清楚,我确实希望服务能够持续存在,即使主 activity 通过用户操作或 android 系统被销毁,它做得很好,但是当应用程序在某个时候重新打开时,我想检查是否存在 bg activity 并使用操作按钮停止它,THX 提前。


我启动了一个后台服务,在我的 MainActivity 我可以停止它并重新运行它,当应用程序从 运行 应用程序列表中关闭时该服务仍然存在,问题是当我重新启动时关闭的应用程序尝试使用按钮停止服务我的应用程序崩溃导致它显然试图停止它不再具有引用的服务。

private void startBg(){

    if (!hasPermissions() || mScanning) {
        return;
    }

    clearLogs();

    BgServiceIntent = new Intent(MainActivity.this, BgScanService.class);
    startService(BgServiceIntent);
}
private void stopBg(){
    stopService(BgServiceIntent);
}

重新打开应用程序后调用 stopBg() 失败,因为 BgServiceIntent 不再指向此服务,因此我得到:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: mobile.link.imbera.apsys.imberalink, PID: 20104
              java.lang.NullPointerException
                  at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1568)
                  at android.app.ContextImpl.stopServiceCommon(ContextImpl.java:1628)
                  at android.app.ContextImpl.stopService(ContextImpl.java:1589)
                  at android.content.ContextWrapper.stopService(ContextWrapper.java:499)
                  at mobile.link.imbera.apsys.imberalink.MainActivity.stopBg(MainActivity.java:180)
                  at mobile.link.imbera.apsys.imberalink.MainActivity.lambda$onCreate$MainActivity(MainActivity.java:124)
                  at mobile.link.imbera.apsys.imberalink.MainActivity$$Lambda.onClick(Unknown Source)
                  at android.view.View.performClick(View.java:4463)
                  at android.view.View$PerformClick.run(View.java:18770)
                  at android.os.Handler.handleCallback(Handler.java:808)
                  at android.os.Handler.dispatchMessage(Handler.java:103)
                  at android.os.Looper.loop(Looper.java:193)
                  at android.app.ActivityThread.main(ActivityThread.java:5333)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:515)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
                  at dalvik.system.NativeStart.main(Native Method)

也许您需要做的就是在尝试阻止它之前检查它是否 null:

private void stopBg(){
     if(BgServiceIntent != null)
         stopService(BgServiceIntent);
}

如果该服务已经 运行 在后台运行,那么您需要对该服务的同一实例调用 stopSelf()。 现在根据服务生命周期 onCreate() only call once in the lifetime of service 。 WhereAs onStartCommand 每次您以新意图调用 startService() 时都会被调用。所以你可以做的是传递一个标志来阻止它。

Intent BgServiceIntent = new Intent(MainActivity.this, BgScanService.class);
    BgServiceIntent.putExtra("close",true);
    startService(BgServiceIntent);

并在服役中。

 @Override
public int onStartCommand(Intent intent, int flags, int startId) {
    boolean shouldClose=intent.getBooleanExtra("close",false);
    if(shouldClose){
        stopSelf();
    } else {
        // Continue to action here
    }
    return START_STICKY;
}

首先在START_NOT_STICKY.
启动一个服务 在您的服务中覆盖此方法

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

并将其添加到您的 MainActivity.class

@override
private onDestroy() {
    stopService(YourActiveService);
    finishAffinity();
    super.onDestroy()
}