fail to set top app changed原因查找

我在程序的onPause里面使用了向通知栏添加通知的方式,告诉用户程序是在后台运行,类似手机QQ一样,但今天我在通过点击通知栏重新激活程序的时候程序竟然见上帝去了 ???

在日志里面发了fail to set top app changed! 这很明显是失败了,我检查了一个添加通知栏的代码:

Intent notifyIntent = new Intent(this, CoreActivityGroup.class);
 
        notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 
        PendingIntent appIntent = PendingIntent.getActivity(
                getApplicationContext(), 0, notifyIntent, 0);
 
        
        Notification myNoti = new Notification();
        
        myNoti.icon = iconId;
        
        myNoti.tickerText = text;
        
        // myNoti.defaults = Notification.DEFAULT_SOUND;
        myNoti.flags |= Notification.FLAG_ONGOING_EVENT;
        myNoti.flags |= Notification.FLAG_NO_CLEAR;
 
        
        myNoti.setLatestEventInfo(getApplicationContext(), "金山共享精灵", text,
                appIntent);
 
        
        nm.notify(NOTIFICATION_ID, myNoti);

没有任何问题,我还在一个新工程里面测试了一下,证明这段代码是正常的。。。

下来接着看日志,在日志中发现下面一段输出:

03-02 00:05:32.037: DEBUG/NotificationService(104): cancelNotification, ACTION_NOTIFICATION_REMOVE,pkg=com.xxxxxx.xxxxxxx,id=10001
03-02 00:05:32.048: INFO/LSState(104): EventReceiver:android.intent.action.NOTIFICATION_REMOVE
03-02 00:05:32.108: INFO/AudioHardwareQSD(67): AUDIO_START: start kernel pcm_out driver.
03-02 00:05:32.157: INFO/AndroidRuntime(5753): AndroidRuntime onExit calling exit(0)
03-02 00:05:32.178: INFO/WindowManager(104): WIN DEATH: Window{4636d478 com.xxxxxxxxx.xxxxxxxxx/com.xxxxxxxxx.xxxxxx.ui.CoreActivityGroup paused=true}
03-02 00:05:32.178: INFO/ActivityManager(104): Process com.xxxxxxxx.xxxxx (pid 5753) has died.
03-02 00:05:32.187: ERROR/ActivityManager(104): fail to set top app changed!
03-02 00:05:32.227: INFO/UsageStats(104): Unexpected resume of com.fede.launcher while already resumed in com.xxxxxxx.xxxxx

上面的xxxxx是替换了真实的包名^_^,  里面这句:

Process com.xxxxxxxx.xxxxx (pid 5753) has died.

OMG! 原来程序已经退出鸟,难怪会失败了,问题的根源已经近了一步,下来就要检查程序为什么退出了,在代码中只发现了这个地方比较可疑:

@Override
    protected void onDestroy() {
        nm.cancel(NOTIFICATION_ID);        
        super.onDestroy();
        System.exit(0);
    }

将System.exit(0)注释掉后,我KAO,一切正常鸟!

此时问题又来了,onDestroy怎么会被执行呢?又仔细查了一个Activty的生命周期,怎么看这个函数也不会被执行的,添加了日志发现,从通知栏切换回来的时候,确实会调用onDestroy(),

下来我对这个Activty设置为单实例属性后,就不会调用onDestroy()方法。

神奇的问题!

========

更新(0210-03-02):

昨晚在加了单实例后,通知栏的问题是解决了,可是在程序切换到后台的时候,点击程序的图标(快捷方式)启动又会有问题,最终发现对Activty设置成android:launchMode="singleTask"就解决了。

posted @ 2011-03-02 00:37  Gang.Wang  阅读(2781)  评论(0编辑  收藏  举报