android - 启动器重启后 AppWidget PendingIntent 不工作

标签 android android-intent onclick android-appwidget android-pendingintent

我有一个带有 2 个待处理 Intent 的 AppWidget。他们大部分时间都在工作,但过一会儿他们就会停止响应。我唯一能够确定的是,在 Launcher 重新启动后它们会瘫痪,即我使用 Launcher Pro,有时会摆弄设置并不得不重新启动它。之后他们就完全不工作了。

这是我的 onRecieve()onUpdate() 方法:

   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);
}
}

我读过有关将 Intent 放入服务的文章,但尝试过但失败了。感谢您的帮助。

最佳答案

正如我所写的here您应该只生成一个 RemoteView 实例。

关于android - 启动器重启后 AppWidget PendingIntent 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4909886/

相关文章:

android - Kotlin 运行时错误 kotlin.Any 未找到

android - 如何浏览 Android C++ 源代码

java - 将字符串传递给其他 Activity 不起作用

android - 重写 onNewIntent 时是否有任何理由不调用 setIntent?

javascript - 为什么要将子元素属性传递给父 <tr> 元素的 onClick 事件(React)?

android - 如何使用谷歌地图 V2 在 android 中使用 MapView?

Android:如何将监听器添加到硬件菜单按钮?

android - Intent in android - 黑莓

android - Android 上未调用 onClick 方法

javascript - 切换的替代方法