2011-10-04 63 views
6

我有一個Android的homescreenwidget有兩個按鈕。兩個按鈕都應該調用相同的活動(類),只需使用不同的意圖和意圖附加功能,即可知道哪個按鈕稱爲類。 現在只有button1正在工作並調用活動。我還會在被調用的活動中收到關鍵值。Android小部件的兩個按鈕調用相同的活動有不同的意圖

我怎樣才能使第二個按鈕的工作? 這裏是我的代碼:

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

    super.onUpdate(context, appWidgetManager, appWidgetIds); 

    for (int i =0; i<appWidgetIds.length ; i++){ 

     int appWidgetId = appWidgetIds[i]; 

     Intent intent2 = new Intent(context, Main.class); 
     Intent intent1 = new Intent(context, Main.class); 

     // Intent put extras Button 1 
     String bread1 = "secure"; 
     Bundle basket1 = new Bundle(); 
     basket1.putString("key", bread1); 
     intent1.putExtras(basket1); 

     // Intent put extras Button 2 
     String bread2 = "insecure"; 
     Bundle basket2 = new Bundle(); 
     basket2.putString("key", bread2); 
     intent2.putExtras(basket2); 

     PendingIntent pending1 = PendingIntent.getActivity(context,0,intent1, 0); 
     PendingIntent pending2 = PendingIntent.getActivity(context, 0, intent2, 0); 

     RemoteViews views1 = new RemoteViews(context.getPackageName(),R.layout.maina); 
     RemoteViews views2 = new RemoteViews(context.getPackageName(),R.layout.maina); 

     views1.setOnClickPendingIntent(R.id.button1, pending1); 
     views2.setOnClickPendingIntent(R.id.button2, pending2); 

     appWidgetManager.updateAppWidget(appWidgetId, views1); 
     appWidgetManager.updateAppWidget(appWidgetId, views2); 

這裏是maina.xml

  <?xml version="1.0" encoding="utf-8"?> 
      <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" android:weightSum="1"    android:orientation="vertical"> 
      <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:id="@+id/tvWidget" android:textAppearance="?android:attr/textAppearanceLarge"></TextView> 

      <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" android:weightSum="1" android:orientation="horizontal"> 



      <Button android:text="@string/button1" android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="wrap_content"></Button> 
      <Button android:text="@string/button2" android:id="@+id/button2" android:layout_height="wrap_content" android:layout_width="wrap_content"></Button> 

      </LinearLayout> 

</LinearLayout> 
+0

你能否提供關於第二個按鈕不起作用的更多詳細信息?即點擊時它什麼都不做? –

+0

對,它在點擊時根本沒有做任何事情。 – Arnold

回答

12

@Arnold已創建2的PendingIntent這是....

PendingIntent pending1 = PendingIntent.getActivity(context,0,intent1, 0); 
PendingIntent pending2 = PendingIntent.getActivity(context, 0, intent2, 0); 

凡PendingIntent.getActivity(上下文的背景下,INT requestCode,意圖意圖,詮釋標誌)有4個參數。您必須爲不同的PendingIntents發送不同的「requestCode」。

你的代碼應該是....

PendingIntent pending1 = PendingIntent.getActivity(context,0,intent1, PendingIntent.FLAG_UPDATE_CURRENT); 
PendingIntent pending2 = PendingIntent.getActivity(context, 1, intent2, PendingIntent.FLAG_UPDATE_CURRENT); 
在您需要創建這個主類

...

String value; 
@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 

      Bundle b=intent.getExtras(); 


    try{ 
    value=b.getString("key"); 
    } 
    catch (Exception e) { 
     // TODO: handle exception 
    } 

    super.onReceive(context, intent); 

} 

用的onUpdate代碼....

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

    // Get all ids 
    ComponentName thisWidget = new ComponentName(context, 
      main.class); 
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget); 

    for (int widgetId : allWidgetIds) { 
     // Create some random data 

     RemoteViews remoteViews = new RemoteViews(context.getPackageName(), 
       R.layout.main); 


     // Register an onClickListener for 1st button 
     Intent intent = new Intent(context, main.class); 

     intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); 
     intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,allWidgetIds); 
     intent.putExtra("key", "1st Button"); 

     PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 
       0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

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

     // Register an onClickListener for 2nd button............ 
     Intent intent2 = new Intent(context, main.class); 

      intent2.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); 
        intent2.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,allWidgetIds); 
      intent2.putExtra("key", "2nd Button"); 

     PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 
       1, intent2, PendingIntent.FLAG_UPDATE_CURRENT); 

     remoteViews.setOnClickPendingIntent(R.id.button2, pendingIntent2); 

     appWidgetManager.updateAppWidget(widgetId, remoteViews); 
    } 
} 

然後你可以檢查value = 1st Buttonvalu e =第二個按鈕瞭解哪個Button已被點擊。 它應該工作...如果它不能正常工作,請讓我知道是什麼問題...

0

小部件只會有1個遠程視圖。試着改變你的代碼段的末尾:

RemoteViews remoteView = new RemoteViews(context.getPackageName(),R.layout.maina); 

remoteView.setOnClickPendingIntent(R.id.button1, pending1); 
remoteView.setOnClickPendingIntent(R.id.button2, pending2); 

appWidgetManager.updateAppWidget(appWidgetId, remoteView); 

做不到這一點,這將是看你的佈局R.layout.maina有用。

+0

我改變了你提到的代碼。它不工作。 button2沒有功能。該按鈕會在您點擊它時更改顏色,但不會啓動所需的活動。感謝您的幫助 – Arnold

+0

對不起,Arnold除了指出您在maina.xml中缺少一個關閉的標籤之外,不能發現問題。但是,我懷疑這是一個複製和粘貼問題,因爲Eclipse應該將此標記爲錯誤,否則。祝你好運(如果你想出來的話,在這裏發佈答案)! –

2

我在使用widget上的2個按鈕時遇到了類似的問題。我在它們上設置了OnClickPendingIntents,並通過不同的setExtra()調用進行區分。但只有後者的意圖被稱爲,即使點擊第一個按鈕。 我發現2個解決方案: - 分配不同的行動,既PendingIntents - 第二PendingEvent設置PendingEvent.FLAG_CANCEL_CURRENT標誌

我與PendingEvents和窗口小部件很陌生,所以我不能看到利弊,將堅持第一個解決方案。

也許這可能會幫助你解決你的問題。

1

在我的情況:

remoteViews.setOnClickPendingIntent(R.id.widget_head, touch_man(context)); 

remoteViews.setOnClickPendingIntent(R.id。widget_head2,touch_woman(context));

public static PendingIntent touch_man(Context context) { 

    Intent intent = new Intent(); 
    intent.setAction("touch_man"); 
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

} 

public static PendingIntent touch_woman(Context context) { 

    Intent intent = new Intent(); 
    intent.setAction("touch_woman"); 
    return PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

} 

在AndroidManifest

<receiver 
     android:name="Receiver" 
     android:label="widgetBroadcastReceiver" > 
     <intent-filter> 
      <action android:name="touch_man" /> 
      <action android:name="touch_woman"/> 
     </intent-filter> 

     <meta-data 
      android:name="android.appwidget.provider" 
      android:resource="@xml/demo_widget_provider" /> 
    </receiver> 

它的工作。

相關問題