【问题标题】:Stop a service after MainActivity is closed (EDITED)MainActivity 关闭后停止服务(已编辑)
【发布时间】:2017-12-14 01:21:48
【问题描述】:

我认为我根本不清楚,我确实希望服务能够持续存在,即使主要活动被用户操作或 android 系统破坏,它也做得很好,但是当应用程序在某个点重新打开时,我会想检查是否存在 bg 活动并使用操作按钮将其停止,提前 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$1$MainActivity(MainActivity.java:124)
                  at mobile.link.imbera.apsys.imberalink.MainActivity$$Lambda$1.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)

【问题讨论】:

  • 使用MainActivity的@Override protected void onResume()和@Override protected void onPause()来启动和停止服务

标签: android android-service


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 我认为它将永远为空,因为 mainactivity 被破坏了,我想要的是一种检索正在运行的实例或其 startId 或事后杀死它的方法。
    【解决方案3】:

    首先在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()
    }
    

    【讨论】:

    • 我确实希望它具有粘性,这就是成为 bg 服务的意义所在,即使主应用程序死机,我也希望它能够持续存在,但我需要一种方法来在我重新打开以前的应用程序时检索该实例关闭主要活动并在那一点停止
    猜你喜欢
    • 2020-03-12
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    • 1970-01-01
    相关资源
    最近更新 更多