Я нашел много потоков по этой теме, однако я не могу решить свою проблему. Вот код:BroadcastReceiver не называется
Файл манифеста:
<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);
}
Я буду искать связывание :) – user1315621
Отправка команд службе через 'startService()' еще проще, чем опция привязки. Если вы действительно хотите использовать шину сообщений, не используйте регулярные трансляции, поскольку они являются общедоступными. Используйте 'LocalBroadcastManager' или стороннюю шину сообщений (например, Square's Otto, EventBus greenrobot). – CommonsWare
Вы предлагаете лучше использовать startService() или привязку? Какой самый чистый и умный способ? – user1315621