2014-10-28 3 views
2

나는 미디어 플레이어 서비스를 제어하는 ​​위젯을 가지고 있습니다. 위젯과 다중 대기중인 의도가있는 Android 문제

  • 정지 앞으로
  • 네 개의 버튼은 플레이어에게 브로드 캐스트를 전송 보류중인 의도를 할당

  • 일시 정지

    • 이전
    • 재생/: 내 위젯에 4 개 버튼이 서비스에 추가 조치로 포함되어 있습니다.

      내가 겪고있는 문제는 보류중인 인 텐트를 아래의 버튼에 할당 할 때 모두 동일한 브로드 캐스트 값을 갖는 것으로 보입니다. 예를 들어, 아래의 코드를 사용하면 4 개의 버튼이 모두 Globals.PlaybackActions.SKIP_FORWARD를 보내는 것처럼 보입니다. 그러나 "재생"버튼을 제외하고 보류중인 인 텐트를 지정하는 코드를 주석 처리 한 다음 "재생"을 누르면 올바른 값이 방송됩니다.

      위젯에서 여러 개의 보류중인 인 텐트를 브로드 캐스트로 사용하지 못하거나 내가 찾지 못하는 코드에서 바보 같은 실수를 저질렀습니까? 아니면 비밀 옵션 C?

      나는 길을 잃어 버리고 혼란스러워하며 어떤 도움을 주셔서 감사합니다. 감사.

      public class PlayerWidget extends AppWidgetProvider { 
      
           private boolean serviceIsPlaying = false; 
      
           private PendingIntent pendingIntentPrev = null; 
           private PendingIntent pendingIntentNext = null; 
           private PendingIntent pendingIntentStop = null; 
           private PendingIntent pendingIntentPlay = null; 
      
           public void onReceive(Context context, Intent intent) { 
            if (!intent.getAction().equals(Globals.BROADCAST_WIDGET_UPDATE)) { 
             return; 
            } 
      
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); 
            ComponentName thisAppWidget = new ComponentName(context.getPackageName(), PlayerWidget.class.getName()); 
            int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget); 
      
            this.updateViews(context, appWidgetManager, appWidgetIds); 
           } 
      
           private void updateViews(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){ 
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_player); 
      
            this.setClickIntents(context); 
      
            if (this.serviceIsPlaying) { 
             remoteViews.setImageViewResource(R.id.btnPlay, R.drawable.ic_action_pause); 
             } 
            else { 
             remoteViews.setImageViewResource(R.id.btnPlay, R.drawable.ic_action_play); 
            } 
      
            remoteViews.setTextViewText(R.id.txtArtistName, currentTrack.getArtistName()); 
            remoteViews.setTextViewText(R.id.txtSongName, currentTrack.getSongTitle()); 
            remoteViews.setTextViewText(R.id.txtAlbumName, currentTrack.getAlbumName()); 
            remoteViews.setImageViewBitmap(R.id.imgAlbumArt, BitmapFactory.decodeFile(currentTrack.getAlbumArtPath())); 
      
            remoteViews.setOnClickPendingIntent(R.id.btnPrev, pendingIntentPrev); 
            remoteViews.setOnClickPendingIntent(R.id.btnNext, pendingIntentNext); 
            remoteViews.setOnClickPendingIntent(R.id.btnStop, pendingIntentStop); 
            remoteViews.setOnClickPendingIntent(R.id.btnPlay, pendingIntentPlay); 
      
            appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); 
           } 
      
           private void setClickIntents(Context context) { 
            Intent intentPrev = new Intent(Globals.BROADCAST_PLAYBACK_ACTION); 
            Intent intentNext = new Intent(Globals.BROADCAST_PLAYBACK_ACTION); 
            Intent intentStop = new Intent(Globals.BROADCAST_PLAYBACK_ACTION); 
            Intent intentPlay = new Intent(Globals.BROADCAST_PLAYBACK_ACTION); 
      
            intentPrev.putExtra(Globals.EXTRA_PLAYBACK_ACTION, Globals.PlaybackActions.SKIP_BACKWARD); 
            intentNext.putExtra(Globals.EXTRA_PLAYBACK_ACTION, Globals.PlaybackActions.SKIP_FORWARD); 
            intentStop.putExtra(Globals.EXTRA_PLAYBACK_ACTION, Globals.PlaybackActions.STOP); 
      
            if (this.serviceIsPlaying) { 
             intentPlay.putExtra(Globals.EXTRA_PLAYBACK_ACTION, Globals.PlaybackActions.PAUSE); 
            } 
            else { 
             intentPlay.putExtra(Globals.EXTRA_PLAYBACK_ACTION, Globals.PlaybackActions.PLAY); 
            } 
      
            pendingIntentPrev = PendingIntent.getBroadcast(context, 0, intentPrev, 0); 
            pendingIntentNext = PendingIntent.getBroadcast(context, 0, intentNext, 0); 
            pendingIntentStop = PendingIntent.getBroadcast(context, 0, intentStop, 0); 
            pendingIntentPlay = PendingIntent.getBroadcast(context, 0, intentPlay, 0); 
           } 
          } 
      
  • 답변

    2

    문제는 PendingIntent.getBroadcast() 메소드의 두 번째 매개 변수라고 생각합니다. 두 번째 매개 변수는 requestCode가 있고 그것이 다른해야 4 PendingIntent-S는 using.For 예를 들면 다음과 같습니다

    pendingIntentPrev = PendingIntent.getBroadcast(context, 0, intentPrev, 0); 
    pendingIntentNext = PendingIntent.getBroadcast(context, 1, intentNext, 0); 
    pendingIntentStop = PendingIntent.getBroadcast(context, 2, intentStop, 0); 
    pendingIntentPlay = PendingIntent.getBroadcast(context, 3, intentPlay, 0); 
    

    나는 며칠 전 같은 문제가이 나를 도왔다. 희망도 그것은 당신을 도울 것입니다.

    +0

    굉장 - 내 문제가 해결되었습니다. 나는 온라인에서 기사의 코드에 대한 개념을 가지고 있고 그들은 모두 0을 가지고있다. 그들이 어떻게 일할 것이라고 기대했는지는 알 수 없습니다. 감사합니다 Michal - 정말 고맙습니다. – TheCrimsonSpace

    관련 문제