我正在开发一个通过下载大图像文件进行更新的简单小部件。 为此,我只是启动一个检索图像的服务。我从AppWidgetProvider的onUpdate方法启动它。 像那样:
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds)
{
Intent intent = new Intent(context, UpdateService.class);
context.startService(intent);
}
在wifi中一切都运行良好。 但在3G网络速度慢的情况下,下载需要花费太多时间。结果我的服务被android系统杀死了。 这里的痕迹:
03-04 15:19:02.698: INFO/ActivityManager(166): Start proc com.testpic for broadcast
com.testpic/com.testpic.TestPicActivity: pid=1994 uid=10087 gids={1015, 3003}
03-04 15:19:22.889: WARN/ActivityManager(166): Timeout executing service:
ServiceRecord{41851230 com.testpic/com.testpic.service.UpdateService}
03-04 15:19:22.944: INFO/dalvikvm(166): Jit: resizing JitTable from 8192 to 16384
03-04 15:19:22.967: INFO/Process(166): Sending signal. PID: 1994 SIG: 3
03-04 15:19:22.967: INFO/dalvikvm(1994): threadid=3: reacting to signal 3
03-04 15:19:22.994: INFO/dalvikvm(1994): Wrote stack traces to '/data/anr/traces.txt'
03-04 15:19:22.994: INFO/Process(166): Sending signal. PID: 166 SIG: 3
03-04 15:19:22.994: INFO/dalvikvm(166): threadid=3: reacting to signal 3
03-04 15:19:23.053: INFO/dalvikvm(166): Wrote stack traces to '/data/anr/traces.txt'
03-04 15:19:23.053: INFO/Process(166): Sending signal. PID: 232 SIG: 3
03-04 15:19:23.053: INFO/dalvikvm(232): threadid=3: reacting to signal 3
03-04 15:19:23.061: INFO/dalvikvm(232): Wrote stack traces to '/data/anr/traces.txt'
03-04 15:19:23.061: INFO/Process(166): Sending signal. PID: 342 SIG: 3
03-04 15:19:23.061: INFO/dalvikvm(342): threadid=3: reacting to signal 3
03-04 15:19:23.084: INFO/dalvikvm(342): Wrote stack traces to '/data/anr/traces.txt'
03-04 15:19:23.084: INFO/Process(166): Sending signal. PID: 359 SIG: 3
03-04 15:19:23.084: INFO/dalvikvm(359): threadid=3: reacting to signal 3
03-04 15:19:23.096: INFO/dalvikvm(359): Wrote stack traces to '/data/anr/traces.txt'
03-04 15:19:23.272: DEBUG/dalvikvm(166): GC_CONCURRENT freed 914K, 16% free
13371K/15879K, paused 3ms+5ms
03-04 15:19:23.467: DEBUG/dalvikvm(166): GC_EXPLICIT freed 459K, 16% free 13475K/15879K,
paused 3ms+5ms
03-04 15:19:24.065: ERROR/ActivityManager(166): ANR in com.testpic
Reason: Executing service com.testpic/com.testpic.service.UpdateService
Load: 1.58 / 1.52 / 0.84
CPU usage from 12327ms to 0ms ago with 99% awake:
16% 166/system_server: 7.3% user + 9.4% kernel / faults: 8 minor
9.1% 1994/com.testpic: 7.1% user + 2% kernel / faults: 249 minor
0.7% 374/com.android.launcher: 0.6% user + 0% kernel / faults: 15 minor
0.6% 342/com.android.phone: 0.4% user + 0.2% kernel / faults: 15 minor
0.4% 5/kworker/u:0: 0% user + 0.4% kernel
0.4% 1343/kworker/0:1: 0% user + 0.4% kernel
0.4% 66/yaffs-bg-1: 0% user + 0.4% kernel
0.4% 77/rild: 0% user + 0.3% kernel
0.2% 897/de.devmil.minimaltext: 0.2% user + 0% kernel / faults: 63 minor
0.1% 293/com.google.process.gapps: 0% user + 0% kernel / faults: 4 minor
0% 159/logcat: 0% user + 0% kernel
0% 160/flush-179:0: 0% user + 0% kernel
0% 232/com.android.systemui: 0% user + 0% kernel / faults: 1 minor
0% 835/com.google.android.apps.maps:NetworkLocationService: 0% user + 0% kernel /
faults: 15 minor
25% TOTAL: 14% user + 11% kernel + 0.2% softirq
CPU usage from 577ms to 1090ms later with 99% awake:
22% 166/system_server: 8.7% user + 14% kernel
10% 182/ActivityManager: 3.5% user + 7% kernel
7% 179/system_server: 0% user + 7% kernel
3.5% 199/er$SensorThread: 0% user + 3.5% kernel
1.7% 181/er.ServerThread: 1.7% user + 0% kernel
4.5% 1994/com.testpic: 4.5% user + 0% kernel / faults: 4 minor
4.5% 1994/com.testpic: 4.5% user + 0% kernel
19% TOTAL: 7.6% user + 11% kernel
03-04 15:19:24.065: WARN/ActivityManager(166): Killing ProcessRecord{416316c0
1994:com.testpic/10087}: background ANR
03-04 15:19:24.073: INFO/ActivityManager(166): Process com.testpic (pid 1994) has died.
03-04 15:19:24.073: WARN/ActivityManager(166): Scheduling restart of crashed service
com.testpic/com.testpic.service.UpdateService in 42380ms
所以我的问题是: - 我该怎么做才能避免这种ANR? - 在这种情况下使用服务是否正确? - 如果不是我应该用什么来进行这种治疗?
提前感谢您的帮助:)