0

我发现了很多关于这个主题的主题,但是我无法解决我的问题。这是代码:

清单文件:

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

1 回答 1

1

与其以这种方式播放媒体,不如将 Service 绑定到 Activity,而不是使用广播接收器进行消息传递。

此外,您不应该使用 @string/VAR1 (我不确定意图过滤器是否适用于这种字符串定义)作为您的意图操作,它始终应该是常量字符串,例如:

android.intent.action.BLAH
于 2013-08-04T22:06:18.760 回答