2017-01-10 12 views
1

Android newbie here。私は1つのアクティビティを持つアプリと1つのAppWidgetを持っています。どちらもIntentServiceにコマンドを送信します。アクティビティは正常に動作しますが、2つのボタンで構成されるAppWidgetは、どのボタンをクリックしても同じ結果を出します。 このようなAppWidgetからIntentServiceを使用することはできません...?私はちょうど学んでいます。どんな助けもありがとう。 Android AppWidgetとIntentService、2つの異なるボタンと同じ結果

マイIntentServiceクラス:

public class RemoteIntentService extends IntentService { 
    private static final String TAG = "RemoteIntentService"; 

    public RemoteIntentService() { 
     super("RemoteIntentService"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     Bundle data = intent.getExtras(); 
     String command = data.getString("command"); 
     Log.d(TAG, "onHandleIntent: command = " + command); 
    } 
} 

マイAppWidgetクラス:

public class RemoteWidget extends AppWidgetProvider { 

    private static final String TAG = "RemoteWidget"; 

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) { 

     // Construct the RemoteViews object 
     RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.remote_widget); 

     // Instruct the widget manager to update the widget 
     appWidgetManager.updateAppWidget(appWidgetId, views); 
    } 



    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     // There may be multiple widgets active, so update all of them 
     super.onUpdate(context, appWidgetManager, appWidgetIds); 

     for (int appWidgetId : appWidgetIds) { 

      Intent intentButton1 = new Intent(context, RemoteIntentService.class); 
      intentButton1.putExtra("command", "Button1"); 

      Intent intentButton2 = new Intent(context, RemoteIntentService.class); 
      intentButton2.putExtra("command", "Button2"); 

      PendingIntent pendingButton1 = PendingIntent.getService(context, 0, intentButton1, 0); 
      PendingIntent pendingButton2 = PendingIntent.getService(context, 0, intentButton2, 0); 

      RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.remote_widget); 

      views.setOnClickPendingIntent(R.id.button1, pendingButton1); 
      views.setOnClickPendingIntent(R.id.button2, pendingButton2); 

      appWidgetManager.updateAppWidget(appWidgetId, views); 
     } 
    } 
} 

私はAppWidget上の2つのボタンをクリックし、出力:

D/RemoteIntentService: onHandleIntent: command = Button1 
D/RemoteIntentService: onHandleIntent: command = Button1 
+0

あなたのレイアウトxmlを投稿してください。 –

答えて

1

は、あなたが(PendingIntent.getService(context, 0, intentButton1, 0)を使用具体的には、最後のパラメータとして0)、エキストラはrを取得しませんPendingIntent overviewごとeplaced:

この動作のため、2つのインテントは、PendingIntentを取得する目的のために同じであると考えられている際に知っておくことが重要です。人々がよくやっている間違いは、「余分な」内容だけが変化するインテントを持つ複数のPendingIntentオブジェクトを作成し、毎回異なるPendingIntentを取得することを期待することです。これは起こりません。照合に使用されるインテントの部分は、Intent.filterEqualsで定義されたものと同じです。 Intent.filterEqualsと同等の2つのインテントオブジェクトを使用する場合は、それらの両方に対して同じPendingIntentを取得します。

PendingIntent pendingButton1 = PendingIntent.getService(context, 0, 
    intentButton1, 0); 
PendingIntent pendingButton2 = PendingIntent.getService(context, 1, 
    intentButton2, 0); 

これが原因となります:各PendingIntentはユニークなリクエストコード(第2パラメータ)を持つようにあなたのケースでは、あなたがそれを変更することができますが、彼らは、それらに対処する方法についての詳細に入る

Androidを2つの別々のPendingIntentsとして表示します。

+0

ありがとう!私はより慎重にドキュメントを読む必要があります。 –

関連する問題