0

이 문제에 관해서는 백만 번 5 가지 질문이 있지만 그 중 모두를 통해 정렬을 시도 했으므로 제대로 작동하지 않습니다. 나는 AppWidgetProvider가 단지 BroadcastReceiver를 확장한다는 것을 안다. 내 AppWidgetProvider 내 모든 것을 다 할 수있다. ACTION_HEADSET_PLUG 인 텐트 필터가 Intent.FLAG_RECEIVER_REGISTERED_ONLY이고 다음과 같이 시도했음을 읽었습니다. Detecting whether a headset is plugged into an Android device or not. 코드에서 내 필터를 설정했지만 예쁜 mish-mash 였기 때문에 머리를 감쌀 수 없었습니다.헤드폰이 연결되어 있는지 확인하십시오. 플러그가 뽑혀 졌을 때 음악을 일시 중지하십시오.

필자의 onReceive 메소드에서 intent.getAction()을 토스트했으며, 헤드폰을 연결하거나 분리 할 때 아무것도 표시되지 않습니다. 버튼을 눌렀을 때, 나는 그들과 연관된 int 상수를 보았습니다. 등록 된 유일한 것에 의해 차단 되었기 때문에 이것이라고 가정합니다.

나는 또한 끈적 거리는 무엇인가에 대해 읽었고, 잠시 동안 그걸 포크로 썼다.

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.musicplayerforburrito" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="15" 
     android:targetSdkVersion="16" /> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> 
    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <receiver android:name="MyWidgetProvider" > 
      <intent-filter > 
       <action 
        android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
       <action android:name="android.appwidget.action.PLAY_PAUSE_WIDGETS" /> 
       <action android:name="android.appwidget.action.NEXT_WIDGETS" /> 
       <action android:name="android.appwidget.action.PREVIOUS_WIDGETS" /> 
       <action android:name="android.appwidget.action.RELOAD_WIDGETS" /> 
       <action android:name="android.appwidget.action.ACTION_HEADSET_PLUG" /> 

      </intent-filter> 
      <intent-filter> 
       <action android:name="android.intent.action.ACTION_HEADSET_PLUG"/> 
      </intent-filter> 

      <meta-data 
       android:name="android.appwidget.provider" 
       android:resource="@xml/widget_info" /> 
     </receiver> 
     <activity 
      android:name="com.example.musicplayerforburrito.MusicPlayer" 
      android:label="@string/app_name" 
      android:screenOrientation="portrait" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

말 그대로 시간이 싸우는 봤는데 그것은 심지어 그 큰 아니기 때문에 정말 바보 같다 : 여기

package com.example.musicplayerforburrito; 

import android.app.PendingIntent; 
import android.appwidget.AppWidgetManager; 
import android.appwidget.AppWidgetProvider; 
import android.content.ComponentName; 
import android.content.Context; 
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.widget.RemoteViews; 
import android.widget.Toast; 




public class MyWidgetProvider extends AppWidgetProvider { 

    public static String WIDGET_PLAY_BUTTON = "android.appwidget.action.PLAY_PAUSE_WIDGETS"; 
    public static String NEXT_BUTTON = "android.appwidget.action.NEXT_WIDGETS"; 
    public static String PREVIOUS_BUTTON = "android.appwidget.action.PREVIOUS_WIDGETS"; 
    public static String RELOAD_BUTTON = "android.appwidget.action.RELOAD_WIDGETS"; 


    static MusicPlayerClass mpc; 
    static String CurrentlyPlayingSong = ""; 
    static Context cont; 

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


     mpc = new MusicPlayerClass(context); 
     RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout); 
     ComponentName watchWidget = new ComponentName(context, MyWidgetProvider.class); 

     remoteViews.setOnClickPendingIntent(R.id.playpausewidget, getPendingSelfIntent(context, WIDGET_PLAY_BUTTON)); 
     remoteViews.setOnClickPendingIntent(R.id.forwardwidget, getPendingSelfIntent(context, NEXT_BUTTON)); 
     remoteViews.setOnClickPendingIntent(R.id.backwidget, getPendingSelfIntent(context, PREVIOUS_BUTTON)); 
     remoteViews.setOnClickPendingIntent(R.id.reloadwidget, getPendingSelfIntent(context, RELOAD_BUTTON)); 
     appWidgetManager.updateAppWidget(watchWidget, remoteViews); 
    } 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 
     super.onReceive(context, intent); 
     //if(intent.hasExtra("state")) 
     // Toast.makeText(context, "hi jimmy", 2000).show(); 
     Toast.makeText(context, intent.getAction(), 2000).show(); 

     try 
     { 
      String b = mpc.currentlyPlayingSong; 
     } 
     catch(Exception e) 
     { 
      mpc = new MusicPlayerClass(context); 
     } 
     AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); 
     RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout); 
     ComponentName watchWidget = new ComponentName(context, MyWidgetProvider.class); 
     cont = context; 
     if (WIDGET_PLAY_BUTTON.equals(intent.getAction())) { 

      if(!mpc.hasFolders) 
      { 
       mpc.Initialize(); 
       mpc.mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() 
       {   
        public void onCompletion(MediaPlayer mp) 
        { 
         mpc.songIndex++; 
         CurrentlyPlayingSong = mpc.StartSong(); 
         UpdateTitle(cont); 
        }   
       }); 
      } 
      if(mpc.isPlaying) 
      { 
       mpc.isPlaying = false; 
       mpc.PauseMusic(); 
       remoteViews.setImageViewResource(R.id.playpausewidget, R.drawable.play); 
      } 
      else if(mpc.currentPosition != 0) 
      { 
       mpc.isPlaying = true; 
       mpc.ResumeMusic(); 
       remoteViews.setImageViewResource(R.id.playpausewidget, R.drawable.pause);     
      } 
      else 
      { 
       remoteViews.setTextViewText(R.id.mp3filename, mpc.StartSong()); 
       remoteViews.setImageViewResource(R.id.playpausewidget, R.drawable.pause); 
      } 

     } 
     else if (NEXT_BUTTON.equals(intent.getAction())) { 
      mpc.songIndex++; 
      remoteViews.setTextViewText(R.id.mp3filename, mpc.StartSong()); 
     } 
     else if (PREVIOUS_BUTTON.equals(intent.getAction())) { 
      mpc.songIndex--; 
      remoteViews.setTextViewText(R.id.mp3filename, mpc.StartSong()); 
     } 
     else if (RELOAD_BUTTON.equals(intent.getAction())) { 
      mpc.Reload(); 
      mpc.mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() 
      {   
       public void onCompletion(MediaPlayer mp) 
       { 
       mpc.songIndex++; 
       CurrentlyPlayingSong = mpc.StartSong(); 
       UpdateTitle(cont); 
       }   
      }); 
      remoteViews.setTextViewText(R.id.mp3filename, mpc.StartSong()); 
      remoteViews.setImageViewResource(R.id.playpausewidget, R.drawable.pause); 
     } 
     appWidgetManager.updateAppWidget(watchWidget, remoteViews); 
    } 

private void UpdateTitle(Context context) 
{ 
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); 
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout); 
    ComponentName watchWidget = new ComponentName(context, MyWidgetProvider.class); 
    remoteViews.setTextViewText(R.id.mp3filename, CurrentlyPlayingSong); 
    appWidgetManager.updateAppWidget(watchWidget, remoteViews);  
} 


    protected PendingIntent getPendingSelfIntent(Context context, String action) { 
     Intent intent = new Intent(context, getClass()); 
     intent.setAction(action); 
     return PendingIntent.getBroadcast(context, 0, intent, 0); 
    } 
} 

내 매니페스트입니다 : 여기

내 AppWidgetManager입니다 헤드폰을 뽑을 때 음악을 일시 중지하는 "기능". 공유 할 수있는 모든 지침은 크게 감사하겠습니다.

TIA

답변

0

https://stackoverflow.com/a/9682818/1682419 따옴표에 대답 (핵심 안드로이드 팀의) 다이앤 Hackborn. 이러한 브로드 캐스트 메시지에 관심이있는 한 계속 실행되는 서비스 또는 활동이 필요하며 이러한 메시지를 등록하려면 registerReceiver()으로 전화해야합니다.

주 1 : 당신이 의도 작업을 발명 할 때는, 당신은 이름 충돌을 피하기 위해 자신의 패키지 이름이 아닌 android.intent.action를 사용하기로되어 있습니다.

참고 2 : 위젯 공급자의 <intent-filter>은 공개 동작 android.appwidget.action.APPWIDGET_UPDATE 만 표시해야합니다. 비공개 온 클릭 행동과 android.intent.action.ACTION_HEADSET_PLUG을 포함하면 아무런 이점이 없습니다.