2017-01-10 8 views
1

Android 신참 여기. 하나의 액티비티가있는 앱과 하나의 AppWidget이 있습니다. 둘 다 IntentService에 명령을 보냅니다. 액티비티는 잘 작동하지만 두 버튼으로 구성된 AppWidget은 어떤 버튼을 클릭하든 동일한 결과를 제공합니다. 어쩌면 이처럼 AppWidget에서 IntentService를 사용할 수 없습니까? 나는 단지 배우는 중이다. 어떤 도움을 주시면 감사하겠습니다. Android AppWidget 및 IntentService, 두 가지 다른 버튼의 동일한 결과

내 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에있는 두 개의 버튼을 클릭하여 출력 :

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

레이아웃 게시 xml. –

답변

1

당신이 (PendingIntent.getService(context, 0, intentButton1, 0)를 사용 특히, 마지막 매개 변수로 0), 엑스트라는 r을 얻지 못합니다 PendingIntent overview 당 eplaced :

이 동작의

, 두 개의 텐트가 PendingIntent를 검색 목적을 위해 동일한 것으로 간주 할 때 아는 것이 중요합니다. 사람들이 흔히 저지르는 실수 중 하나는 매번 다른 PendingIntent를 얻을 것으로 예상되는 "여분"내용 만 다른 Intents로 여러 개의 PendingIntent 객체를 만드는 것입니다. 이것은 일어나지 않습니다. 일치에 사용되는 의도 부분은 Intent.filterEquals에 의해 정의 된 것과 동일합니다. Intent.filterEquals에 해당하는 두 개의 Intent 객체를 사용하면 두 객체에 동일한 PendingIntent가 표시됩니다.

그들은 각 PendingIntent이 (두 번째 매개 변수) 고유 요청 코드를 가지고 귀하의 경우에 당신이 그것을 변경할 수 있지만, 그들과 거래를하는 방법에 대한 자세한로 이동합니다

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

이 발생합니다 Android를 두 개의 별도 PendingIntents로 표시합니다.

+0

감사합니다. 나는 더 조심스럽게 문서를 읽어야한다. –