1

我正在开发一个 voip 应用程序,它基本上使用一个主 Activity (XTabsContainer) 和一个服务 (NativeService)。主 Activity 总是以标志 FLAG_ACTIVITY_SINGLE_TOP 和 FLAG_ACTIVITY_NEW_TASK 启动(即从 SplashScreen Activity)。

在主活动中,我有一个 OptionsMenu,其中只有一项用于退出应用程序,具有以下功能:

public void exit(){
    Log.d(TAG, "exit()");
    mExitThread = new ExitThread();
    mExitThread.start();
    this.finish();
}

ExitThread() 只是关闭了一些东西并终止了服务:

private class ExitThread extends Thread
{
    public void run() {
        Log.d(TAG, "Starting Unregistration thread");
        INgnSipService sipService = Engine.getInstance().getSipService();
        int count = 0;
        if (sipService.isRegistered())
        {
            sipService.unRegister();
            while (count <= NativeService.mTimeout && sipService.isRegistered())
            {
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                count++;
            }
            // Exit for timeout
            if (count > NativeService.mTimeout)
                Log.w(TAG, "Exiting Unregistration thread for TIMEOUT");
            else
                Log.d(TAG, "Exiting Unregistration thread");
        }
        if (!Engine.getInstance().stop()) {
            Log.e(TAG, "Failed to stop engine");
        }   
    }   
}

比完成()应该关闭主要活动。

非常随机的情况是,在调用完finish()之后,主Activity又被启动了,我不明白为什么,所以我需要再次点击OptionsMenu中的“退出”按钮才能真正退出应用程序.

发生这种情况时遵循日志。特别是第一次调用 exit() 函数是在 04-26 10:29:21.580 进行的。

提前感谢您的帮助。

## Raise the app in foreground

04-26 10:29:16.020: D/it.domain.myapp.XTabsContainer(32540): onResume()
04-26 10:29:16.020: D/it.domain.myapp.XTabsContainer(32540): TabsFragmentActivity handleAction(): ACTION_NOTIFY_HIDDEN_CALL value=false
04-26 10:29:18.840: W/ResourceType(32540): Failure getting entry for 0x010802c1 (t=7 e=705) in package 0 (error -75)
04-26 10:29:19.075: D/dalvikvm(32540): GC_CONCURRENT freed 1601K, 26% free 14639K/19527K, paused 22ms+4ms, total 171ms

## Press Exit button in the OptionsMenu

04-26 10:29:21.580: D/it.domain.myapp.XTabsContainer(32540): exit()
04-26 10:29:21.605: D/it.domain.myapp.XTabsContainer(32540): Starting Unregistration thread
04-26 10:29:21.605: D/org.doubango.ngn.services.impl.NgnConfigurationService(32540): stopping...
04-26 10:29:21.615: D/org.doubango.ngn.services.impl.NgnHistoryService(32540): Stopping
04-26 10:29:21.615: D/org.doubango.ngn.services.impl.NgnStorageService(32540): stopping...
04-26 10:29:21.615: D/org.doubango.ngn.services.impl.NgnContactService(32540): stopping...
04-26 10:29:21.620: D/org.doubango.ngn.services.impl.NgnContactService(32540): Observer Looper exit()
04-26 10:29:21.620: D/org.doubango.ngn.services.impl.NgnSipService(32540): stopping...
04-26 10:29:21.870: D/org.doubango.ngn.services.impl.NgnSipService(32540): GSM Observer stopped...
04-26 10:29:21.870: D/org.doubango.ngn.services.impl.NgnSoundService(32540): stopping...
04-26 10:29:21.870: D/org.doubango.ngn.services.impl.NgnNetworkService(32540): Stopping...
04-26 10:29:21.970: D/it.domain.myapp.NativeService(32540): onDestroy()
04-26 10:29:21.970: D/org.doubango.ngn.NgnNativeService(32540): onDestroy()

## Here the main Activity is started again

04-26 10:29:21.980: D/it.domain.myapp.XTabsContainer(32540): XTabsContainer()
04-26 10:29:21.980: D/it.domain.myapp.XTabsContainer(32540): onCreate()
04-26 10:29:21.980: D/org.doubango.ngn.services.impl.NgnNetworkService(32540): acquireNetworkLock()
04-26 10:29:21.985: D/org.doubango.ngn.services.impl.NgnNetworkService(32540): netType=1 and netSubType=0
04-26 10:29:22.000: D/it.domain.myapp.XTabsContainer(32540): initialiseTabHost()
04-26 10:29:22.005: D/it.domain.myapp.TabFragRecents(32540): TabFragRecents()
04-26 10:29:22.005: D/it.domain.myapp.TabFragRecents(32540): onCreateView()
04-26 10:29:22.015: D/AbsListView(32540): Get MotionRecognitionManager
04-26 10:29:22.015: D/it.domain.myapp.TabFragRecents(32540): onActivityCreated()
04-26 10:29:22.015: D/it.domain.myapp.ContactsListAdapter(32540): ContactsListAdapter()
04-26 10:29:22.015: D/it.domain.myapp.TabFragRecents(32540): MyRecentsListAdapter()
04-26 10:29:22.020: D/it.domain.myapp.TabFragRecents(32540): onStart()
04-26 10:29:22.030: D/it.domain.myapp.XTabsContainer(32540): BadgeNumberUpdate()
04-26 10:29:22.030: D/it.domain.myapp.XTabsContainer(32540): BadgeNumberUpdate(): SIP NOT REGISTERED
04-26 10:29:22.155: D/it.domain.myapp.XTabsContainer(32540): onResume()
04-26 10:29:22.155: D/it.domain.myapp.XTabsContainer(32540): TabsFragmentActivity handleAction(): ACTION_NOTIFY_HIDDEN_CALL value=false
04-26 10:29:22.395: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:22.395: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length

## Here the previous main Activity is actually destroyed and the new-created main Activity shows up

04-26 10:29:22.395: D/it.domain.myapp.XTabsContainer(32540): onDestroy
04-26 10:29:22.410: D/it.domain.myapp.TabFragRecents(32540): onDestroy

## The new-created main Activity continues its life

04-26 10:29:30.635: D/it.domain.myapp.TabFragContacts(32540): TabFragContacts()
04-26 10:29:30.635: D/it.domain.myapp.TabFragContacts(32540): onCreateView()
04-26 10:29:30.670: D/AbsListView(32540): Get MotionRecognitionManager
04-26 10:29:30.675: D/it.domain.myapp.TabFragContacts(32540): onActivityCreated()
04-26 10:29:30.675: D/it.domain.myapp.TabFragContacts(32540): Pressed button: 2131296425
04-26 10:29:30.680: D/it.domain.myapp.TabFragContacts(32540): onResume()
04-26 10:29:30.695: E/SensorManager(32540): thread start
04-26 10:29:30.700: D/SensorManager(32540): registerListener :: handle = 0  name= K3DH Acceleration Sensor delay= 200000 Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,+*a):8(
04-26 10:29:30.790: D/it.domain.myapp.ContactsListAdapter(32540): ContactsListAdapter()
04-26 10:29:30.795: D/it.domain.myapp.TabFragContacts(32540): updateSections()
04-26 10:29:30.855: E/it.domain.myapp.TabFragContacts(32540): onPostExecute(): Exception: java.lang.NullPointerException
04-26 10:29:30.855: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:30.855: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:30.860: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:30.860: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:35.365: D/it.domain.myapp.XTabsContainer(32540): Recents Tab Pressed!
04-26 10:29:35.365: D/SensorManager(32540): unregisterListener:: Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,+*a):8(
04-26 10:29:35.370: D/Sensors(32540): Remain listener = Sending .. normal delay 200ms
04-26 10:29:35.370: I/Sensors(32540): sendDelay --- 200000000
04-26 10:29:35.370: D/SensorManager(32540): JNI - sendDelay
04-26 10:29:35.370: I/SensorManager(32540): Set normal delay = true
04-26 10:29:35.380: D/it.domain.myapp.TabFragRecents(32540): onResume()
04-26 10:29:35.380: D/it.domain.myapp.TabFragRecents(32540): updateSections()
04-26 10:29:35.380: D/it.domain.myapp.TabFragRecents(32540): sendActionMsg()
04-26 10:29:35.380: D/it.domain.myapp.TabFragRecents(32540): sendActionMsg(): SIP NOT REGISTERED
04-26 10:29:35.475: W/IInputConnectionWrapper(32540): getSelectedText on inactive InputConnection
04-26 10:29:35.505: W/IInputConnectionWrapper(32540): setComposingText on inactive InputConnection
04-26 10:29:35.505: W/IInputConnectionWrapper(32540): getExtractedText on inactive InputConnection
04-26 10:29:37.030: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:37.030: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:38.950: D/it.domain.myapp.FragmentServices(32540): onCreateView()
04-26 10:29:38.970: D/AbsListView(32540): Get MotionRecognitionManager
04-26 10:29:38.970: D/it.domain.myapp.FragmentServices(32540): buildData()
04-26 10:29:38.970: D/it.domain.myapp.FragmentServices(32540): putData()
04-26 10:29:38.970: D/it.domain.myapp.FragmentServices(32540): putData()
04-26 10:29:38.970: D/it.domain.myapp.FragmentServices(32540): putData()
04-26 10:29:38.975: D/it.domain.myapp.FragmentServices(32540): onResume()
04-26 10:29:39.095: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:39.095: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:41.715: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:41.720: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:47.700: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:47.700: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:49.350: D/it.domain.myapp.ViewInformation(32540): ViewInformation()
04-26 10:29:49.400: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:49.400: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:49.495: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:49.495: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:30:13.320: D/it.domain.myapp.XTabsContainer(32540): onResume()
04-26 10:30:13.320: D/it.domain.myapp.XTabsContainer(32540): TabsFragmentActivity handleAction(): ACTION_NOTIFY_HIDDEN_CALL value=false
04-26 10:30:13.435: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:30:13.435: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:30:16.050: W/ResourceType(32540): Failure getting entry for 0x010802c1 (t=7 e=705) in package 0 (error -75)

## Here I pressed the Exit button the second time

04-26 10:30:16.995: D/it.domain.myapp.XTabsContainer(32540): exit()
04-26 10:30:17.030: D/it.domain.myapp.XTabsContainer(32540): Starting Unregistration thread
04-26 10:30:17.750: D/it.domain.myapp.XTabsContainer(32540): onDestroy
04-26 10:30:17.765: D/it.domain.myapp.TabFragRecents(32540): onDestroy
04-26 10:30:17.770: D/it.domain.myapp.TabFragContacts(32540): onDestroy()
4

1 回答 1

0

您正在使用Handler.post()这意味着您的代码ExitThread实际上并未在另一个线程中运行,而是在主(UI)线程上运行。您的ExitThread睡眠中有一个循环。这不好在 UI 线程上运行,因为它会阻塞 UI。Android 最终会以 ANR(应用程序无响应)错误终止此活动。

您需要ExitThread在单独的线程中实际运行您的内容。代替:

mHandler.post(new ExitThread());

做:

new ExitThread().start();
于 2013-04-26T12:45:36.643 回答