5

J'ai un AppWidget avec 2 intentions en attente. Ils travaillent la plupart du temps, mais après un certain temps, ils cessent de répondre. La seule chose que j'ai pu identifier est qu'ils sont paralysés après un redémarrage de Launcher, c'est-à-dire que j'utilise Launcher Pro, et que je bidouille parfois avec des paramètres et que je dois le redémarrer. Après cela, ils ne travaillent plus du tout.Appending PendingIntent ne fonctionne pas après le redémarrage du Launcher

Voici mes onRecieve() et onUpdate() méthodes:

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

j'ai lu les intentions de mettre en service, mais ont essayé et échoué. Toute aide est appréciée.

+0

qu'est-ce que c'est avec l'appel de 'super.onUpdate();'? Il ne fait rien selon la source http://androidxref.com/4.4.4_r1/xref/frameworks/base/core/java/android/appwidget/AppWidgetProvider.java#113. – faizal

+0

'appWidgetManager.updateAppWidget()' n'a pas besoin d'être appelé dans une boucle pour chaque widget. Il accepte un tableau d'entiers. Vous pouvez donc simplement appeler 'appWidgetManager.updateAppWidget (appWidgetIds, top)' une fois au lieu de 'appWidgetManager.updateAppWidget (appWidgetId, top)' plusieurs fois. – faizal

Répondre

4

Comme j'ai écrit here, vous ne devriez produire qu'une seule instance de RemoteView.

+0

Pas vraiment, la création d'une seule instance entraînera éventuellement un effondrement du widget; comme la mémoire va augmenter avec chaque instruction envoyée à la vue à distance – htafoya

Questions connexes