活动崩溃后的And​​r​​oid停止后台服务 [英] android stop background service after activity crash

查看:226
本文介绍了活动崩溃后的And​​r​​oid停止后台服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开始的时候,我想对于英语不好道歉。

At the beginning, I would like apologize for bad english.

有是我的问题:

我在Android的服务是活动运行时,在后台运行。 (此服务与特定的时间间隔服务器同步用户数据)。

I have a service in android which runs on background when is activity running. (This service synchronize user data with server with specified time intervals).

public class CService extends Service
{
   private Boolean isDestroyed;



@Override
    public int onStartCommand (Intent intent, int flags, int startId)
    {
            if (intent != null)
            {
                   new Thread(new Runnable()
                   {
                   //run new thread to disable flush memory for android and destroy this service
                           @Override
                           public void run ()
                           {
                                     this.isDestroyed = Boolean.FALSE
                                     while(!this.isDestroyed)
                                     {
                                     //loop until service isn't destroyed
                                     }
                            }

                    }).start();
            }
            return Service.START_NOT_STICKY;

    }

    @Override
    public void onDestroy ()
    {
           //THIS ISNT CALLED FROM uncaughtException IN ACTIVITY BUT from onDestroy method is this called.

           //when is service destroyed then onDestroy is called and loop finish
           this.isDestroyed = Boolean.TRUE;
    }

}

和从活动开始onCreateMethod。本次活动实现Thread.UncaughtExceptionHandler和onCreate方法注册捕捉活动中的所有意外的异常。当一些活动中抛出异常的方法uncaughtException被调用,并且服务应与stopService(serviceIntent)站下车;但在服务onDestoy不叫。但在活动的onDestroy方法称为(用户推然后后退按钮),当服务被成功地停止onDestoroy在内部CService被调用。

And is started from activity in onCreateMethod. This activity implements Thread.UncaughtExceptionHandler and register in onCreate method to catch all unexpected exceptions in activity. When something in activity throw exception method uncaughtException is called and and service should stop with stopService(serviceIntent); But onDestoy in service isn't called. But when is onDestroy method in activity called (user push then back button) service is stopped succesfully and onDestoroy in CService is called.

    public class CActivity extends Activity implements Thread.UncaughtExceptionHandler
    {
            private Thread.UncaughtExceptionHandler defaultUEH;

        @Override
        protected void onCreate (Bundle savedInstanceState)
        {

            this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();

            // create intent for service
            Intent serviceIntent = new Intent(this, CService.class);
            // run service
            startService(serviceIntent);

            //set default handler when application crash
            Thread.setDefaultUncaughtExceptionHandler(this);
            super.onCreate(savedInstanceState);
        }

        @Override
        public void uncaughtException (Thread thread, Throwable ex)
        {
            //THIS DOESN'T WORK

            //when global exception in activity is throws then this method is called. 
            Intent serviceIntent = new Intent(this, CService.class);
            //method to service stop is called. BUT THIS METHOD DON'T CALL onDestroy in CService
            stopService(serviceIntent);
            defaultUEH.uncaughtException(thread, ex);       
        }

        @Override
        public void onDestroy ()
        {
            //this work fine
            Intent serviceIntent = new Intent(this, CService.class);
            stopService(serviceIntent);
            super.onDestroy();
            }


    }

我需要在活动崩溃停止后台服务。 Bacause当Android的密切活动,并在堆栈开始previous活动(这是登录屏幕)现在没有用户登录。

I need stop background service when activity crashes. Bacause when android close activity and start previous activity in stack (Which is login screen) no user is now logged.

感谢您的建议。

推荐答案

试试这个

    Thread.currentThread().setUncaughtExceptionHandler(this);

UPD 结果
这是默认的Andr​​oid异常处理程序

UPD
this is default android exception handler

private static class UncaughtHandler implements Thread.UncaughtExceptionHandler {
        public void uncaughtException(Thread t, Throwable e) {
            try {
                // Don't re-enter -- avoid infinite loops if crash-reporting crashes.
                if (mCrashing) return;
                mCrashing = true;

                if (mApplicationObject == null) {
                    Slog.e(TAG, "*** FATAL EXCEPTION IN SYSTEM PROCESS: " + t.getName(), e);
                } else {
                    Slog.e(TAG, "FATAL EXCEPTION: " + t.getName(), e);
                }

                // Bring up crash dialog, wait for it to be dismissed
                ActivityManagerNative.getDefault().handleApplicationCrash(
                        mApplicationObject, new ApplicationErrorReport.CrashInfo(e));
            } catch (Throwable t2) {
                try {
                    Slog.e(TAG, "Error reporting crash", t2);
                } catch (Throwable t3) {
                    // Even Slog.e() fails!  Oh well.
                }
            } finally {
                // Try everything to make sure this process goes away.
                Process.killProcess(Process.myPid());
                System.exit(10);
            }
        }
    }

我相信你没有得到的onDestroy()调用,因为默认的Andr​​oid异常处理程序将破坏整个过程System.exit()。你可以尽量不叫defaultUEH.uncaughtException(螺纹,除息);或者(如果)你的服务destoryed后来经过调用它。

I believe you don't get the onDestroy() call, because the default android exception handler simply destroys entire process System.exit(). You can try not to call defaultUEH.uncaughtException(thread, ex); or call it later after(if) your service is destoryed.

这篇关于活动崩溃后的And​​r​​oid停止后台服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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