我发现了很多关于这个主题的主题,但是我无法解决我的问题。这是代码:
清单文件:
<service
android:name="com.xxx.player.MediaPlayerService.MediaPlayerService"
android:enabled="true" >
<intent-filter>
<action android:name="android.media.AUDIO_BECOMING_NOISY" />
</intent-filter>
<receiver android:name="com.xxx.player.MediaPlayerService.MediaPlayerService$ServiceBroadcastReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="@string/ACTION_PREPARE_AND_PLAY_NEW_FILE"/>
<action android:name="@string/ACTION_PREPARE_NEW_FILE"/>
<action android:name="@string/ACTION_START"/>
<action android:name="@string/ACTION_STOP"/>
<action android:name="@string/ACTION_PAUSE"/>
<action android:name="@string/ACTION_SEEK_TO"/>
<action android:name="@string/ACTION_RELEASE"/>
</intent-filter>
</receiver>
</service>
广播类:
public class MediaPlayerService extends Service implements
MediaPlayer.OnErrorListener,
AudioManager.OnAudioFocusChangeListener,
Runnable,
SeekBar.OnSeekBarChangeListener {
...
public class ServiceBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Toast.makeText(getApplicationContext(), "action: " + action, 30000000).show();
if (action.equals(Resources.getSystem().getString(R.string.ACTION_PREPARE_AND_PLAY_NEW_FILE))) {
prepareAndPlayNewFile(intent.getStringExtra("mediaData"));
}
}
}
}
我提交意图的方式:
private void prepareAndPlayNewFile(String mediaData) {
Intent myIntent = new Intent();
myIntent.setAction(context.getString(R.string.ACTION_PREPARE_AND_PLAY_NEW_FILE));
myIntent.putExtra("mediaData", mediaData);
context.sendBroadcast(myIntent);
}