当按下后退按钮并在Android模拟器上崩溃时,Android应用程序变得无法响应

时间:2013-01-22 10:12:06

标签: android android-activity intentservice

我的应用程序包含两个活动。在第一个活动中,一些输入来自用户,并通过意图传递给第二个活动。第二个活动启动两个IntentServices以执行一些长时间运行的后台任务。我有一个停止服务的停止按钮。当我在停止服务后尝试按下后退按钮时,之前的活动会加载,但应用程序将无响应并关闭。我尝试通过浏览本网站上的建议来解决问题,但问题仍然存在。

从第一个开始第二个活动的代码

Intent intent = new Intent(ConnectActivity.this, MessageActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.putExtra("ip", ip);
intent.putExtra("port", port);
intent.putExtra("tcpport", tcpport);
intent.putExtra("tcpip", tcpip);
intent.putExtra("packetType", PT);
startActivity(intent);

第二项活动的backButtonPressed()代码

public void onBackPressed() {
    if(UdpSendService.send)
    while(!endTransmission());
    //Intent intent = new Intent(this, ConnectActivity.class);
    //intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    //startActivity(intent);
    finish();
    //super.onBackPressed();
}

endTransmission()方法

boolean endTransmission() {
    UdpSendService.send=false;
    if(receiver1!=null && receiver2!=null){
        unregisterReceiver(receiver1);
        unregisterReceiver(receiver2);
        receiver1=null;
        receiver2=null;
    }
    while(stopService(udpReceiveIntent)!=true);
    System.out.println("\nreceive service stopped");
    while(stopService(udpServiceIntent)!=true);
    System.out.println("\nsend service stopped");
    return true;
}

logcat的:

01-22 15:18:39.893: E/ActivityManager(162): ANR in com.example.udpmessageclient (com.example.udpmessageclient/.ConnectActivity)
01-22 15:18:39.893: E/ActivityManager(162): Reason: keyDispatchingTimedOut
01-22 15:18:39.893: E/ActivityManager(162): Load: 1.39 / 1.05 / 0.66
01-22 15:18:39.893: E/ActivityManager(162): CPU usage from 12723ms to 0ms ago:
01-22 15:18:39.893: E/ActivityManager(162):   63% 162/system_server: 53% user + 9.6% kernel / faults: 593 minor
01-22 15:18:39.893: E/ActivityManager(162):   35% 1467/com.example.udpmessageclient: 27% user + 7.9% kernel / faults: 44 minor
01-22 15:18:39.893: E/ActivityManager(162):   0.1% 293/com.android.phone: 0.1% user + 0% kernel / faults: 4 minor
01-22 15:18:39.893: E/ActivityManager(162): 99% TOTAL: 82% user + 17% kernel
01-22 15:18:39.893: E/ActivityManager(162): CPU usage from 3151ms to 3707ms later:
01-22 15:18:39.893: E/ActivityManager(162):   58% 162/system_server: 47% user + 10% kernel
01-22 15:18:39.893: E/ActivityManager(162):     9% 334/Binder_3: 9% user + 0% kernel
01-22 15:18:39.893: E/ActivityManager(162):     7.2% 173/Binder_1: 5.4% user + 1.8% kernel
01-22 15:18:39.893: E/ActivityManager(162):     7.2% 174/Binder_2: 7.2% user + 0% kernel
01-22 15:18:39.893: E/ActivityManager(162):     5.4% 162/system_server: 3.6% user + 1.8% kernel
01-22 15:18:39.893: E/ActivityManager(162):     5.4% 195/InputDispatcher: 1.8% user + 3.6% kernel
01-22 15:18:39.893: E/ActivityManager(162):     5.4% 336/Binder_4: 3.6% user + 1.8% kernel
01-22 15:18:39.893: E/ActivityManager(162):     5.4% 367/Binder_5: 5.4% user + 0% kernel
01-22 15:18:39.893: E/ActivityManager(162):     5.4% 374/Binder_6: 5.4% user + 0% kernel
01-22 15:18:39.893: E/ActivityManager(162):     5.4% 380/Binder_7: 3.6% user + 1.8% kernel
01-22 15:18:39.893: E/ActivityManager(162):     5.4% 383/Binder_8: 3.6% user + 1.8% kernel
01-22 15:18:39.893: E/ActivityManager(162):   39% 1467/com.example.udpmessageclient: 28% user + 10% kernel / faults: 2 minor
01-22 15:18:39.893: E/ActivityManager(162):     35% 1467/dpmessageclient: 25% user + 10% kernel
01-22 15:18:39.893: E/ActivityManager(162): 100% TOTAL: 78% user + 21% kernel
01-22 15:18:43.783: E/InputDispatcher(162): channel '4119ec38 com.example.udpmessageclient/com.example.udpmessageclient.ConnectActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
01-22 15:18:43.853: E/InputDispatcher(162): Received spurious receive callback for unknown input channel.  fd=187, events=0x9
01-22 15:21:45.544: E/Trace(1523): error opening trace file: No such file or directory (2)
01-22 15:21:46.265: E/ActivityThread(278): Failed to find provider info for com.android.inputmethod.latin.dictionarypack
01-22 15:21:46.265: E/BinaryDictionaryGetter(278): Could not find a dictionary pack
01-22 15:21:46.355: E/ActivityThread(278): Failed to find provider info for com.android.inputmethod.latin.dictionarypack
01-22 15:21:46.355: E/BinaryDictionaryGetter(278): Could not find a dictionary pack

4 个答案:

答案 0 :(得分:1)

而(!endTransmission());

看起来更危险,因为这个循环锁定了ui线程。 你的活动花了太长时间才对Android操作系统说'嘿,我还活着'! (这是UI线程的作用)。

Application Responsiveness time

答案 1 :(得分:0)

可能是你的问题出现在这部分代码中:

public void onBackPressed() {
    if(UdpSendService.send)
    while(!endTransmission());
    //Intent intent = new Intent(this, ConnectActivity.class);
    //intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    //startActivity(intent);
    finish();
    //super.onBackPressed();
}

你在while循环中花费了足够的(太多的)时间,因为它阻止了UI线程,使你的应用程序无法响应。

答案 2 :(得分:0)

将过程延迟1秒而不是使用while。 使用处理程序postdelay方法。

答案 3 :(得分:0)

应用程序不应阻止主线程超过5秒。使用线程和处理程序使您的应用更具响应性。 http://developer.android.com/training/articles/perf-anr.html