2010-03-18 65 views
17

I saw this topic並按照說明實現IntentService,但如果我想要多一個按鈕會怎麼樣?我怎樣才能區分按鈕? 我想setFlags,但onHandleIntent無法閱讀()方法:在Android Widget上處理多個按鈕點擊

public static class UpdateService extends IntentService { 
... 

    @Override 
    public void onHandleIntent(Intent intent) { 
     ComponentName me = new ComponentName(this, ExampleProvider.class); 
     AppWidgetManager manager = AppWidgetManager.getInstance(this); 
     manager.updateAppWidget(me, buildUpdate(this)); 
    } 

    private RemoteViews buildUpdate(Context context) { 
     RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.main_layout); 

     Intent i = new Intent(this, ExampleProvider.class); 
     PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0); 
     updateViews.setOnClickPendingIntent(R.id.button_refresh, pi); 

     i = new Intent(this, ExampleProvider.class); 
     pi = PendingIntent.getBroadcast(context, 0, i, 0); 
     updateViews.setOnClickPendingIntent(R.id.button_about, pi); 

     return updateViews; 
    } 
} 

在這小小的一段代碼,我有兩個的PendingIntent與setOnClickPendingIntent聯繫,我能分辨這不是出於不同的動作和處理? 感謝您的幫助

+0

顯然,我找到了答案。我們必須註冊意向過濾器,如http://www.helloandroid.com/files/xmaswidget/android_howto-hellowidget.pdf所述。現在我會嘗試然後寫下結果。 – dive 2010-03-18 17:17:46

回答

54

這樣的作品,從一個擴展的AppWidgetProvider Widget類:

public class ExampleProvider extends AppWidgetProvider { 

// our actions for our buttons 
public static String ACTION_WIDGET_REFRESH = "ActionReceiverRefresh"; 
public static String ACTION_WIDGET_SETTINGS = "ActionReceiverSettings"; 
public static String ACTION_WIDGET_ABOUT = "ActionReceiverAbout"; 


@Override 
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main_layout); 

    Intent active = new Intent(context, ExampleProvider.class); 
    active.setAction(ACTION_WIDGET_REFRESH); 
    PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0); 
    remoteViews.setOnClickPendingIntent(R.id.button_refresh, actionPendingIntent); 

    active = new Intent(context, ExampleProvider.class); 
    active.setAction(ACTION_WIDGET_SETTINGS); 
    actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0); 
    remoteViews.setOnClickPendingIntent(R.id.button_settings, actionPendingIntent); 

    active = new Intent(context, ExampleProvider.class); 
    active.setAction(ACTION_WIDGET_ABOUT); 
    actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0); 
    remoteViews.setOnClickPendingIntent(R.id.button_about, actionPendingIntent); 

    appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); 
} 

@Override 
public void onReceive(Context context, Intent intent) { 
    if (intent.getAction().equals(ACTION_WIDGET_REFRESH)) { 
     Log.i("onReceive", ACTION_WIDGET_REFRESH); 
    } else if (intent.getAction().equals(ACTION_WIDGET_SETTINGS)) { 
     Log.i("onReceive", ACTION_WIDGET_SETTINGS); 
    } else if (intent.getAction().equals(ACTION_WIDGET_ABOUT)) { 
     Log.i("onReceive", ACTION_WIDGET_ABOUT); 
    } else { 
     super.onReceive(context, intent); 
    } 
} 
... 

而且AndroidManifest.xml中,我們必須註冊我們的行動:

<receiver android:name="ExampleProvider"> 
     <intent-filter> 
      <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/> 
      <action android:name="org.divenvrsk.widgets.ExampleProvider.ACTION_WIDGET_REFRESH"/> 
      <action android:name="org.divenvrsk.widgets.ExampleProvider.ACTION_WIDGET_SETTINGS"/> 
      <action android:name="org.divenvrsk.widgets.ExampleProvider.ACTION_WIDGET_ABOUT"/> 
     </intent-filter> 
     <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info"/> 
    </receiver> 
</application>