关闭MainActivity后停止服务(已编辑) [英] Stop a service after MainActivity is closed (EDITED)

查看:125
本文介绍了关闭MainActivity后停止服务(已编辑)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我根本不清楚,即使通过用户操作或android系统破坏了主要活动,我也确实希望该服务能够持续存在,但是,当在某些时候重新打开该应用程序时,我会做到这一点想检查是否存在bg活动,并提前使用操作按钮THX停止活动.

I think im not clear at all, i do want the service to persist even if the main activity is destroyed via user action or android system does it, it does it well, but when the app is reopened at certain point i will want to check if a bg activity exists and stop it using a action button, THX in advance.

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

I launch a background service, in my MainActivity I can stop it and rerun it, the service persists when the app is closed from the running apps list, the problem is when I relaunch the closed app an try to stop the service with a button I have the app crashes cause it obviously tries to stop a service from which it no longer has a reference.

private void startBg(){

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

    clearLogs();

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

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

Calling stopBg() after reopening the app fails, because BgServiceIntent no longer points to this service and thus I get this:

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)

推荐答案

如果该服务已经在后台运行,则需要在该服务的同一实例上调用 stopSelf(). 现在,按照服务生命周期 onCreate(),在服务生命周期中仅调用一次. 每次以新的意图调用startService()时,都会调用 onStartCommand .因此,您可以做的是传递一个意图将其停止的标志.

If the service already running in background then you need to call stopSelf() on same instance of that service . Now as per service life Cycle onCreate() only call once in the lifetime of service . WhereAs onStartCommand get called each time you call startService() with new intent. So what you can do is to pass a flag in intent to stop it .

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

这篇关于关闭MainActivity后停止服务(已编辑)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆