我在做什么?罕见的崩溃用于AppWidget更新
我更新使用IntentService的AppWidget。
什么问题?
一切都工作得很好,除了一些稀有次大概每隔12-15小时或者我可以说是随机的,小部件的更新不会发生。在通过这种情况进行调试之后,看起来是问题的logcat消息。
05-27 20:21:13.122: WARN/ActivityManager(97): Scheduling restart of crashed service com.myapp.android/.myAppWidget$UpdateService in 5000ms
以下是一些更为logcat的消息 - 这是真的很难复制这一点,因为这一段时间发生一次,但发生这种情况时,我重新启动我使用调试模式通过USB端口连接真实设备上的应用程序。
05-27 20:21:16.712: DEBUG/AndroidRuntime(24419): --- registering native functions ---
05-27 20:21:16.742: INFO/global(24420): Default buffer size used in BufferedInputStream constructor. It would be better to be explicit if an 8k buffer is required.
05-27 20:21:16.842: DEBUG/Configuration(24420): active site = local
05-27 20:21:16.872: DEBUG/FREESPACE(24420): Bytes to fill: 580550656
05-27 20:21:16.942: VERBOSE/AlarmManager(97): Adding Alarm{46389f38 type 2 com.google.android.apps.maps} Jan 01 09:30:42 am
05-27 20:21:17.032: INFO/ActivityManager(97): Start proc com.myApp.android for broadcast com.myApp.android/.myAppWidget: pid=24431 uid=10080 gids={1015, 3003}
05-27 20:21:17.092: DEBUG/dalvikvm(24420): GC_FOR_MALLOC freed 3967 objects/320968 bytes in 162ms
05-27 20:21:17.172: DEBUG/FREESPACE(24420): Bytes to fill: 580550656
05-27 20:21:17.252: ERROR/UpdateService(24431): Service Started..
05-27 20:21:17.332: INFO/ActivityManager(97): Force stopping package com.myApp.android uid=10080
05-27 20:21:17.332: INFO/Process(97): Sending signal. PID: 24431 SIG: 9
05-27 20:21:17.332: WARN/ActivityManager(97): Scheduling restart of crashed service com.myApp.android/.myAppWidget$UpdateService in 5000ms
05-27 20:21:17.332: INFO/ActivityManager(97): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.myApp.android/.myApp3 }
05-27 20:21:17.372: INFO/ActivityManager(97): Start proc com.myApp.android for activity com.myApp.android/.myApp3: pid=24444 uid=10080 gids={1015, 3003}
05-27 20:21:17.402: DEBUG/AndroidRuntime(24419): Shutting down VM
以下是的onReceive(),的onUpdate()和onHandleIntent()的代码段延伸IntentService
@Override
public void onReceive(Context context, Intent intent) {
check_intent = intent.getAction();
if (check_intent.equals("android.appwidget.action.APPWIDGET_UPDATE")) {
if (!getLock(context).isHeld()) { // fail-safe for crash restart
getLock(context).acquire();
}
try {
this.onUpdate(context, intent);
} finally {
getLock(context).release();
}
}
if (check_intent.equals("android.appwidget.action.APPWIDGET_ENABLED")) {
this.onEnabled(context);
}
if (check_intent.equals("android.appwidget.action.APPWIDGET_DELETED")) {
this.onDeleted(context);
}
if (check_intent.equals("android.appwidget.action.APPWIDGET_DISABLED")) {
this.onDisabled(context);
}
super.onReceive(context, intent);
}
这里UpdateService类onUpdate
其中startService
方法被称为
public void onUpdate(Context context, Intent intent) {
mAppPreferences = PreferenceManager.getDefaultSharedPreferences(context);
int saved_num_widgets = mAppPreferences.getInt(NUM_WIDGETS, 0);
if (saved_num_widgets > 0) {
Intent widgetUpdate = new Intent(context, myAppWidget.class);
widgetUpdate.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
AlarmManager alarms =
(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
PendingIntent newPending =
PendingIntent.getBroadcast(context,
0,
widgetUpdate,
PendingIntent.FLAG_CANCEL_CURRENT);
alarms.set(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + PERIOD,
newPending);
context.startService(new Intent(context, UpdateService.class));
} else {
//Show Notification
}
}
最后这里是代码onHandleIntent()
@Override
protected void onHandleIntent(Intent intent) {
// here is where your long running task goes
RemoteViews updateViews = buildUpdate(this);
// Push update for this widget to the home screen
if (updateViews != null) {
ComponentName thisWidget = new ComponentName(this, myAppWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);
} else {
updateViews = new RemoteViews(getApplicationContext().getPackageName(),
R.layout.tuwidget);
updateViews.setImageViewResource(R.id.ad, R.drawable.myApp_null_game);
Intent defineIntent1 = new Intent(getApplicationContext(), myApp3.class);
PendingIntent pendingIntent1 =
PendingIntent.getActivity(getApplicationContext(),
0 /* no requestCode */,
defineIntent1,
0 /* no flags */);
updateViews.setOnClickPendingIntent(R.id.tuwidget, pendingIntent1);
ComponentName thisWidget = new ComponentName(this,myAppWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);
}
}
我也想提一提的UpdateService类从IntentService扩展是
- 我不使用onStartCommand
的onCreate()是如下
@Override public void onCreate() { super.onCreate(); Log.e("UpdateService", "Service Started.. "); }
的小部件以正确的时间间隔更新,一切正常,没有强制关闭,但我完全失去了为什么更新不会发生有时候。
我没有为buildUpdate的函数,返回RemoteViews因为我110%肯定这部分工作没有问题,更新窗口小部件提供的代码。
更新:我注意到,每当发生此问题时,我看到IntentService的早期实例仍在应用程序中运行 - >运行服务,这意味着onDestroy()有时不会被调用,并且服务不会自动停止正如它应该的那样。有趣的是什么,我所做的就是创建一个共享县以存储服务的运行状态或停止,并从的onCreate()和的onDestroy()将其切换。现在摆在我调用startService()我检查共享PREF的状态,如果该服务的实例仍在运行我叫stopService(),然后再startService()。我仍然在测试它,但是在编写这个解决方法之后,问题还没有发生!
@Aakash:“通过这里的情况调试后,logcat消息似乎是问题” - 不,这表明你有问题。如果服务崩溃了,可能会在日志中的此行之前有一个与该崩溃相关的堆栈跟踪。如果没有,您将需要添加更多的'Log'语句来尝试隔离发生崩溃的位置。顺便说一句,你正在获取并释放'WakeLock',其中一个已经被'AlarmManager'(例如触发'updatePeriodMillis()'的警报)占据。 – CommonsWare 2011-05-28 11:41:16
啊!我感觉好多了Mr.Mark救我! OK对onReceive中的wakelock感到抱歉,我只是在调试不同的场景。因此记录了一些消息并进行了一些调试,可以看到在偶然的崩溃中,我的服务已经启动,然后什么也没有发生。增加了一些更多的logcat消息。 – Aakash 2011-05-28 13:11:23
@Aakash:对于“强制停止包”日志消息,有各种各样的触发器,我无法确定哪些可能会跳过。抱歉! – CommonsWare 2011-05-28 13:37:27