2016-02-23 23 views
11

Tôi có tiện ích ứng dụng và sự kiện nhấp có thể được nhận trong onUpdate() trong nhà cung cấp của tôi.
Tuy nhiên, khi tôi cố gắng buộc đóng trình khởi chạy nhà, sự kiện nhấp sẽ bị mất.
Tôi thậm chí đặt điểm ngắt trong tất cả onEnabled(), onReceived() ... v.v.: kết nối dường như bị mất.Sự kiện nhấp vào nút Android AppWidget không nhận được sau khi lực lượng khởi động của nhà dừng lại

Kết quả là làm cách nào để "kết nối lại" sự kiện nút?

WidgetProvider kéo dài AppWidgetProvider:

@Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     Log.d(TAG, "onUpdate()"); 
     Log.d(TAG, "isLoading: " + CONSTANT.isLoading); 
     // update each of the widgets with the remote adapter from updateService 
     // Get all ids 
     ComponentName thisWidget = new ComponentName(context, ScoreWidgetProvider.class); 
     int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget); 

     // Build the intent to call the service 
     Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class); 
     intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds); 

     // Update the widgets via the service 
     context.startService(intent); 

//  super.onUpdate(context, appWidgetManager, appWidgetIds); 
    } 

UpdateWidgetService kéo dài Service:

@Override 
     public void onStart(Intent intent, int startId) { 

      Log.i(TAG, "Called"); 

      AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext()); 

      int[] appWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS); 

      for (int i = 0; i < appWidgetIds.length; ++i) { 
       // Here we setup the intent which points to the StackViewService which will 
       // provide the views for this collection. 
       Intent remoteViewsIntent = new Intent(this.getApplicationContext(), ScoreWidgetRemoteViewsService.class); 
       remoteViewsIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]); 
       // When intents are compared, the extras are ignored, so we need to embed the extras 
       // into the data so that the extras will not be ignored. 
       remoteViewsIntent.setData(Uri.parse(remoteViewsIntent.toUri(Intent.URI_INTENT_SCHEME))); 
       RemoteViews rv = new RemoteViews(this.getApplicationContext().getPackageName(), R.layout.widget_layout); 
       rv.setRemoteAdapter(appWidgetIds[i], R.id.score_list, remoteViewsIntent); 

       // Set the empty view to be displayed if the collection is empty. It must be a sibling 
       // view of the collection view. 
       rv.setEmptyView(R.id.score_list, R.id.empty_view); 

       // Bind the click intent for the refresh button on the widget 
       final Intent refreshIntent = new Intent(this.getApplicationContext(), ScoreWidgetProvider.class); 
       refreshIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       refreshIntent.setAction(ScoreWidgetProvider.REFRESH_ACTION); 
       final PendingIntent refreshPendingIntent = PendingIntent 
         .getBroadcast(this.getApplicationContext(), 0, refreshIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
       rv.setOnClickPendingIntent(R.id.btn_refresh, refreshPendingIntent); 

       appWidgetManager.updateAppWidget(appWidgetIds[i], rv); 
      } 

    //  stopSelf(); 

      super.onStart(intent, startId); 
     } 
+0

Bắt đầu dịch vụ OnUpdate! bạn có thể thực hiện các tác phẩm của mình Trên dịch vụ –

+1

http://stackoverflow.com/questions/26108355/how-to-start-an-activity-by-the-click-on-an-image-in-widget/26288400#26288400 –

+0

widget của tôi chứa listview, vì vậy nó đã có lớp mở rộng RemoteViewsService. vì vậy tôi cần phải viết một dịch vụ khác? – jjLin

Trả lời

0

Hãy chắc chắn rằng bạn đang sử dụng bối cảnh đúng chức năng onStart của bạn. Hãy xem getApplicationContext trong phần onStart mã của bạn, chuyển nhầm loại ngữ cảnh có thể gây ra lỗi. Đây là một liên kết để biết thêm thông tin: Context.

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