user1315621
user1315621

Reputation: 3372

BroadcastReceiver not called

I found lots of threads about this topic, however I'm not able to solve my problem. Here is the code:

The manifest file:

<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>

The broadcast class:

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"));
            }

        }

    }

}

The way I submit the intent:

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);
    }

Upvotes: 0

Views: 432

Answers (1)

jtt
jtt

Reputation: 13541

Instead of approaching your playing of media this way, you should instead bind your Service to your Activity instead of using a broadcast receiver for message passing.

Also you shouldn't be using @string/VAR1 (which I'm not sure if intent filters work with that kind of string definition) for your intent actions it ALWAYS should be the constant string such as:

android.intent.action.BLAH

Upvotes: 1

Related Questions