4

一开始,我想为糟糕的英语道歉。

我的问题是:

我在 android 中有一个服务,它在活动运行时在后台运行。(此服务以指定的时间间隔将用户数据与服务器同步)。

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 方法被调用(用户按下然后返回按钮)时,服务会成功停止并调用 CService 中的 onDestoroy。

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


    }

当活动崩溃时,我需要停止后台服务。因为当 android 关闭活动并开始堆栈中的前一个活动(即登录屏幕)时,现在没有用户记录。

感谢您的建议。

4

2 回答 2

2

尝试这个

    Thread.currentThread().setUncaughtExceptionHandler(this);

UPD
这是默认的 android 异常处理程序

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() 调用,因为默认的 android 异常处理程序只会破坏整个进程 System.exit()。你可以尽量不要调用 defaultUEH.uncaughtException(thread, ex); 或者在(如果)您的服务被破坏之后再调用它。

于 2012-11-08T13:29:56.363 回答
1

伙计,顺便问一下,您为什么要在 onCreate 方法中执行操作?正如文档所说,这个方法是从主线程调用的,我实际上想知道它是如何工作的。您应该从另一个线程进行任何繁重的计算。

http://developer.android.com/reference/android/app/Service.html

当一个 Service 组件被实际创建时,出于上述任一原因,系统实际所做的只是实例化该组件并在主线程上调用其 onCreate() 和任何其他适当的回调。由服务以适当的行为来实现这些,例如创建一个辅助线程来执行其工作。

于 2012-11-08T15:45:34.037 回答