5

我有一个AppWidget,其中有两个待定目标。他们大部分时间都在工作,但过了一段时间他们停止了回应。我唯一能指出的一点是,在启动器重新启动之后它们已经瘫痪了,也就是我使用了Launcher Pro,并且有时候会调整设置并重新启动它。之后,他们根本不工作。AppWidget PendingIntent在启动器重新启动后不起作用

这里是我的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); 
} 
} 

我看了一下把意图的服务,但已经尝试和失败。任何帮助表示赞赏。

+0

什么是调用`super.onUpdate();`?根据源http://androidxref.com/4.4.4_r1/xref/frameworks/base/core/java/android/appwidget/AppWidgetProvider.java#113,它不会执行任何操作。 – faizal 2014-07-16 06:19:17

+0

`appWidgetManager.updateAppWidget()`不需要在每个小部件的循环中调用。它接受一个整数数组。所以你可以简单地多次调用`appWidgetManager.updateAppWidget(appWidgetId,top)`而不是`appWidgetManager.updateAppWidget(appWidgetId,top)`。 – faizal 2014-07-16 06:48:50

回答

4

正如我写的here你应该只产生一个RemoteView的实例。

相关问题