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

このようにメディアの再生にアプローチする代わりに、メッセージ パッシングにブロードキャスト レシーバーを使用する代わりに、サービスをアクティビティにバインドする必要があります。

また、インテント アクションに @string/ VAR1 (インテント フィルターがその種の文字列定義で機能するかどうかはわかりません) を使用しないでください。常に次のような定数文字列にする必要があります。

android.intent.action.BLAH
于 2013-08-04T22:06:18.760 に答える