5

Tôi có một AppWidget với 2 mục đích đang chờ xử lý. Họ làm việc hầu hết thời gian, nhưng sau một thời gian họ ngừng đáp ứng. Chỉ có điều tôi đã có thể xác định là chúng bị tê liệt sau khi khởi động lại Trình khởi chạy, tức là tôi sử dụng Trình khởi chạy Pro và đôi khi cũng có cài đặt và phải khởi động lại. Sau đó họ không làm việc gì cả.AppWidget PendingIntent không hoạt động sau khi khởi động lại Khởi động

Dưới đây là onRecieve()onUpdate() tôi phương pháp:

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

Tôi đã đọc về việc đưa những ý đồ trong một dịch vụ, nhưng đã cố gắng và thất bại. Bất kỳ trợ giúp được đánh giá cao.

+0

cuộc gọi với 'super.onUpdate();' là gì? Nó không làm bất cứ điều gì theo nguồn http://androidxref.com/4.4.4_r1/xref/frameworks/base/core/java/android/appwidget/AppWidgetProvider.java#113. – faizal

+0

'appWidgetManager.updateAppWidget()' không cần phải được gọi trong một vòng lặp cho mỗi widget. Nó chấp nhận một mảng nguyên. Vì vậy, bạn có thể chỉ cần gọi 'appWidgetManager.updateAppWidget (appWidgetIds, trên cùng)' một lần thay vì 'appWidgetManager.updateAppWidget (appWidgetId, top)' nhiều lần. – faizal

Trả lời

4

Như tôi đã viết here, bạn chỉ nên tạo một phiên bản của RemoteView.

+0

Không thực sự, việc tạo một cá thể đơn lẻ sẽ làm cho tiện ích sụp đổ cuối cùng; khi bộ nhớ sẽ tăng lên với mỗi lệnh được gửi đến giao diện từ xa – htafoya

Các vấn đề liên quan