2012-03-20 31 views
1
public class ExampleAppWidgetProvider extends AppWidgetProvider { 

@Override 
public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
     int[] appWidgetIds) { 

    final int N = appWidgetIds.length; 

    // Perform this loop procedure for each App Widget that belongs to this 
    // provider 
    for (int i = 0; i < N; i++) { 
     int appWidgetId = appWidgetIds[i]; 

     // Get the layout for the App Widget and attach an on-click listener 
     // to the button 
     RemoteViews views = new RemoteViews(context.getPackageName(), 
       R.layout.example_appwidget); 

     Intent intent = new Intent(context, Fragment_testActivity.class); 
     intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); 
     intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 
       0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

     views.setOnClickPendingIntent(R.id.button1, pendingIntent); 

     // Tell the AppWidgetManager to perform an update on the current app 
     // widget 
     appWidgetManager.updateAppWidget(appWidgetId, views); 

    } 
} 

} 

爲什麼不Button1的觸發onUpdate(也不onReceive)當小部件添加到主屏幕和按鈕1被點擊?添加/刪除小部件會觸發這些事件,但不會單擊該按鈕。應用件按鈕來觸發的onUpdate

Manifestwidget declarationwidget layout declaration

+1

我看到的第一件事:'new Intent(context,Fragment_testActivity.class); ' - 你指定一個活動作爲接收組件。該活動與更新過程無關。嘗試將ExampleAppWidgetProvider設置爲intent的組件,或者根本不指定組件('new Intent()')。 – 2012-03-20 10:07:34

+0

SYLARRR,看到這一個[處理按鈕點擊小部件](http://stackoverflow.com/questions/9633100/how-to-handle-button-click-on-widget/9633400#9633400),你必須註冊一個意圖按鈕點擊更新的動作 – 2012-03-20 10:13:39

+0

@alextsec謝謝你,'新的意圖(上下文,ExampleAppWidgetProvider.class);'修復了這個問題。添加這個作爲這個問題的答案,我會接受它。 – 2012-03-20 10:36:28

回答

0

第一件事,我看到:

new Intent(context, Fragment_testActivity.class); 

您指定的活動爲接收組件。該活動與更新過程無關。嘗試將ExampleAppWidgetProvider設置爲意圖的組件,或者根本不指定組件(例如,通過使用new Intent())。