Android 启动程序重新启动后AppWidget PendingEvent不工作
我有一个AppWidget,有两个待定的意图。他们大部分时间都在工作,但过了一段时间他们就停止了反应。我能指出的唯一一件事是,它们在重新启动启动器后会瘫痪,也就是说,我使用Launcher Pro,有时会摆弄设置,不得不重新启动它。在那之后,他们根本不工作 以下是我的OnReceive和onUpdate方法: 我读过关于将意图放入服务中的文章,但尝试过却失败了。非常感谢您的帮助。正如我所写,您应该只生成RemoteView的一个实例。PendingEvent.getBroadcast有一个名为requestCode的参数,如果有多个PendingEvent,它必须是唯一的代码。Android 启动程序重新启动后AppWidget PendingEvent不工作,android,android-intent,onclick,android-appwidget,android-pendingintent,Android,Android Intent,Onclick,Android Appwidget,Android Pendingintent,我有一个AppWidget,有两个待定的意图。他们大部分时间都在工作,但过了一段时间他们就停止了反应。我能指出的唯一一件事是,它们在重新启动启动器后会瘫痪,也就是说,我使用Launcher Pro,有时会摆弄设置,不得不重新启动它。在那之后,他们根本不工作 以下是我的OnReceive和onUpdate方法: 我读过关于将意图放入服务中的文章,但尝试过却失败了。非常感谢您的帮助。正如我所写,您应该只生成RemoteView的一个实例。PendingEvent.getBroadcast有一个名为r
您已将所有挂起的内容设置为0。调用super.onUpdate;,有什么用?它不根据源代码执行任何操作。不需要在每个小部件的循环中调用appWidgetManager.updateAppWidget。它接受整数数组。因此,您可以简单地多次调用appWidgetManager.UpdateAppWidgetId,top once而不是appWidgetManager.UpdateAppWidgetId,top;因为每次发送到远程视图的指令都会增加内存
public void onReceive(Context context, Intent intent)
{
super.onReceive(context, intent);
String action = intent.getAction();
if(action.equals("android.tristan.widget.digiclock.CLICK"))
{
PackageManager packageManager = context.getPackageManager();
Intent alarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
String clockImpls[][] = {
{"HTC Alarm Clock", "com.htc.android.worldclock", "com.htc.android.worldclock.AlarmClock" },
{"Standar Alarm Clock", "com.android.deskclock", "com.android.deskclock.AlarmClock"},
{"Froyo Nexus Alarm Clock", "com.google.android.deskclock", "com.android.deskclock.DeskClock"},
{"Moto Blur Alarm Clock", "com.motorola.blur.alarmclock", "com.motorola.blur.alarmclock.AlarmClock"}
};
boolean foundClockImpl = false;
for(int i=0; i<clockImpls.length; i++) {
String vendor = clockImpls[i][0];
String packageName = clockImpls[i][1];
String className = clockImpls[i][2];
try {
ComponentName cn = new ComponentName(packageName, className);
ActivityInfo aInfo = packageManager.getActivityInfo(cn, PackageManager.GET_META_DATA);
alarmClockIntent.setComponent(cn);
foundClockImpl = true;
} catch (NameNotFoundException e) {
Log.d(LOGTAG, "Error," + vendor + " does not exist");
}
}
if (foundClockImpl) {
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(50);
final RemoteViews views = new RemoteViews(context.getPackageName(), layoutID);
views.setOnClickPendingIntent(R.id.TopRow, PendingIntent.getActivity(context, 0, new Intent(context, DigiClock.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK), PendingIntent.FLAG_UPDATE_CURRENT));
AppWidgetManager.getInstance(context).updateAppWidget(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), views);
alarmClockIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(alarmClockIntent);
}
}
}
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
context.startService(new Intent(UpdateService.ACTION_UPDATE));
context.startService(new Intent(context, ScreenUpdateService.class));
final int Top = appWidgetIds.length;
final int Bottom = appWidgetIds.length;
for (int i=0; i<Top; i++)
{
int[] appWidgetId = appWidgetIds;
final RemoteViews top=new RemoteViews(context.getPackageName(), layoutID);
Intent clickintent=new Intent("android.tristan.widget.digiclock.CLICK");
PendingIntent pendingIntentClick=PendingIntent.getBroadcast(context, 0, clickintent, 0);
top.setOnClickPendingIntent(R.id.TopRow, pendingIntentClick);
appWidgetManager.updateAppWidget(appWidgetId, top);
}
for (int i=0; i<Bottom; i++)
{
int[] appWidgetId = appWidgetIds;
RemoteViews bottom=new RemoteViews(context.getPackageName(), layoutID);
Intent clickintent=new Intent("android.tristan.widget.digiclock.CLICK_2");
PendingIntent pendingIntentClick=PendingIntent.getBroadcast(context, 0, clickintent, 0);
bottom.setOnClickPendingIntent(R.id.BottomRow, pendingIntentClick);
appWidgetManager.updateAppWidget(appWidgetId, bottom);
}
}