我制作了一项服务,旨在互联网开启时每10小时在服务器上检索一次数据。
我以前很少使用Service而且我不明白那里发生了什么,但是当我启动我的应用程序时,在显示第一个布局之前我得到一个ANR,片刻之后布局出现并且我可以继续(但我注意到另一个屏幕变化,现在非常慢):
E/ActivityManager: ANR in com.m360.android.debug
PID: 17742
Reason: executing service com.m360.android.debug/com.m360.android.service.offline.SyncOfflineContentService
Load: 11.95 / 12.06 / 12.2
CPU usage from 0ms to 7113ms later:
95% 17742/com.m360.android.debug: 93% user + 1.9% kernel / faults: 4145 minor 1 major
46% 2133/system_server: 19% user + 26% kernel / faults: 5070 minor 4 major
0% 335/debuggerd: 0% user + 0% kernel / faults: 9088 minor 5 major
13% 4932/com.android.phone: 4.7% user + 8.2% kernel / faults: 3168 minor
8.1% 4109/com.android.systemui: 3.6% user + 4.4% kernel / faults: 2137 minor
6.3% 21043/com.google.android.apps.maps: 3% user + 3.2% kernel / faults: 709 minor
5% 25184/adbd: 0.5% user + 4.4% kernel / faults: 2042 minor
2.6% 264/surfaceflinger: 0.7% user + 1.9% kernel
2.6% 4922/com.quicinc.cne.CNEService: 1.2% user + 1.4% kernel / faults: 810 minor
0% 4927/com.qualcomm.qti.tetherservice: 0% user + 0% kernel / faults: 806 minor 2 major
1.1% 188/mmcqd/0: 0% user + 1.1% kernel
1.1% 193/ueventd: 0.4% user + 0.7% kernel
0.8% 6737/mpdecision: 0% user + 0.8% kernel
0.5% 16314/ksoftirqd/3: 0% user + 0.5% kernel
0.4% 3/ksoftirqd/0: 0% user + 0.4% kernel
0.4% 3487/com.android.chrome: 0.4% user + 0% kernel / faults: 350 minor 60 major
0.4% 16316/kworker/3:1: 0% user + 0.4% kernel
0.4% 30162/kworker/0:0: 0% user + 0.4% kernel
0.2% 7/kworker/u:0H: 0% user + 0.2% kernel
0.2% 159/cfinteractive: 0% user + 0.2% kernel
0.2% 1047/kworker/u:5: 0% user + 0.2% kernel
0.2% 1457/com.whatsapp: 0.2% user + 0% kernel
0.2% 6653/irq/33-cpubw_hw: 0% user + 0.2% kernel
0.2% 13154/kworker/0:0H: 0% user + 0.2% kernel
0.2% 16313/kworker/3:0H: 0% user + 0.2% kernel
0.2% 30595/kworker/0:1: 0% user + 0.2% kernel
0.1% 181/boost_sync/0: 0% user + 0.1% kernel
0.1% 184/boost_sync/3: 0% user + 0.1% kernel
0.1% 212/logd: 0.1% user + 0% kernel
0.1% 261/healthd: 0% user + 0.1% kernel
0.1% 334/netd: 0% user + 0.1% kernel / faults: 18 minor
0% 371/oemlogkit: 0% user + 0% kernel
0.1% 10135/com.google.android.talk: 0.1% user + 0% kernel
0.1% 16176/logcat: 0.1% user + 0% kernel
0.1% 22082/com.google.android.apps.messaging: 0.1% user + 0% kernel
0.1% 25409/kworker/u:3: 0% user + 0.1% kernel
+0% 18943/migration/2: 0% user + 0% kernel
+0% 18944/kworker/2:0: 0% user + 0% kernel
+0% 18945/kworker/2:0H: 0% user + 0% kernel
+0% 18946/ksoftirqd/2: 0% user + 0% kernel
+0% 18947/kworker/2:1: 0% user + 0% kernel
+0% 18955/migration/1: 0% user + 0% kernel
+0% 18956/kworker/1:0: 0% user + 0% kernel
+0% 18957/kworker/1:0H: 0% user + 0% kernel
+0% 18958/ksoftirqd/1: 0% user + 0% kernel
70% TOTAL: 44% user + 24% kernel + 0.7% iowait + 0.1% softirq
CPU usage from 6332ms to 6867ms later:
92% 17742/com.m360.android.debug: 92% user + 0% kernel
90% 17742/0.android.debug: 90% user + 0% kernel
5.6% 2133/system_server: 1.8% user + 3.7% kernel / faults: 9 minor
5.6% 2241/ActivityManager: 1.8% user + 3.7% kernel
1.8% 4039/Binder_3: 1.8% user + 0% kernel
4.6% 21043/com.google.android.apps.maps: 0% user + 4.6% kernel / faults: 19 minor
4.6% 21043/droid.apps.maps: 0% user + 4.6% kernel
5% 25184/adbd: 0% user + 5% kernel / faults: 166 minor
3.3% 25184/adbd: 0% user + 3.3% kernel
+0% 18939/adbd: 0% user + 0% kernel
2.4% 264/surfaceflinger: 0% user + 2.4% kernel
1.2% 298/DispSync: 0% user + 1.2% kernel
1.2% 324/EventThread: 1.2% user + 0% kernel
1.1% 3/ksoftirqd/0: 0% user + 1.1% kernel
1.1% 7/kworker/u:0H: 0% user + 1.1% kernel
1.3% 6737/mpdecision: 0% user + 1.3% kernel
1.3% 6756/mpdecision: 0% user + 1.3% kernel
1.4% 16314/ksoftirqd/3: 0% user + 1.4% kernel
+0% 18938/sh: 0% user + 0% kernel
+0% 18942/ps: 0% user + 0% kernel
54% TOTAL: 40% user + 14% kernel
03-20 18:20:02.174 2133-18959/? E/ActivityManager: Error reading /data/anr/traces.txt
java.io.FileNotFoundException: /data/anr/traces.txt: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:452)
at java.io.FileInputStream.<init>(FileInputStream.java:76)
at android.os.FileUtils.readTextFile(FileUtils.java:236)
at com.android.server.am.ActivityManagerService$21.run(ActivityManagerService.java:13183)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:438)
at java.io.FileInputStream.<init>(FileInputStream.java:76)
at android.os.FileUtils.readTextFile(FileUtils.java:236)
at com.android.server.am.ActivityManagerService$21.run(ActivityManagerService.java:13183)
另请注意,永远不会调用onRunTask
(从不显示第一行的日志)
Gcm版本:compile 'com.google.android.gms:play-services-gcm:10.2.0'
也许那是我今天早上更新它的问题,在我开始获得ANR之前
我在清单中定义它的方式:
<service
android:name=".service.offline.SyncOfflineContentService"
android:description="@string/connection"
android:exported="true"
android:permission="com.google.android.gms.permission.BIND_NETWORK_TASK_SERVICE">
<intent-filter>
<action android:name="com.google.android.gms.gcm.ACTION_TASK_READY" />
</intent-filter>
</service>
我的服务:
public class SyncOfflineContentService extends GcmTaskService {
private final static String TAG = "SyncOfflineContentSer";
//private final static long SYNC_PERIOD_SECONDS = 36000;
private final static long SYNC_PERIOD_SECONDS = 30;
private final static String PERIODIC_SYNC_TAG = "SyncOfflineContentService Done";
private final Api api;
private final ImageManagerFactory imageManagerFactory;
private final FileManager fileManager;
public SyncOfflineContentService() {
api = Api.getInstance();
imageManagerFactory = new ImageManagerFactory();
fileManager = new FileManager(api.getOkHttpClient());
}
@Override
public int onRunTask(TaskParams taskParams) {
Log.i(TAG, "onRunTask called");
try (Realm realm = Realm.getDefaultInstance()) {
try {
//some networking
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
//storing data in Realm storage
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
} //auto close
return GcmNetworkManager.RESULT_SUCCESS;
}
public static void scheduleSync(final Context ctx) {
Log.i(TAG, "scheduleSync");
GcmNetworkManager gcmNetworkManager = GcmNetworkManager.getInstance(ctx);
PeriodicTask periodicTask = new PeriodicTask.Builder()
.setPeriod(SYNC_PERIOD_SECONDS) // occurs at *most* once this many seconds - note that you can't control when
.setRequiredNetwork(PeriodicTask.NETWORK_STATE_CONNECTED) // various connectivity scenarios are available
.setTag(PERIODIC_SYNC_TAG) // returned at execution time to your endpoint
.setService(SyncOfflineContentService.class) // the GcmTaskServer you created earlier
.setPersisted(true) // persists across reboots or not
.setUpdateCurrent(true) // replace an existing task with a matching tag - defaults to false!
.build();
gcmNetworkManager.schedule(periodicTask);
}
public static void stopSync(Context ctx) {
Log.i(TAG, "stopSync");
GcmNetworkManager gcmNetworkManager = GcmNetworkManager.getInstance(ctx);
gcmNetworkManager.cancelAllTasks(SyncOfflineContentService.class);
}
}
非常感谢!