1

我的应用程序由两个活动组成。在第一个活动中,从用户那里获取一些输入并通过意图传递给第二个活动。第二个活动启动两个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;
}

日志猫:

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

4 回答 4

1

而(!endTransmission());

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

应用响应时间

于 2013-01-22T10:49:29.790 回答
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 线程以使您的应用程序无响应。

于 2013-01-22T10:17:28.723 回答
0

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

于 2013-01-22T10:31:43.220 回答
0

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

于 2013-01-22T10:40:27.573 回答