7

私は音楽プレーヤーウィジェット(ホーム画面ウィジェット)に取り組んでいます。1曲だけ再生する必要があります(できればMediaPlayerクラスを使用)。しかし、それを実装する方法がわかりません。私はAndroid開発に少し不慣れです、それは言及されています。

私がこれまでに持っているクラスは拡張されておりAppWidgetProvider、このクラスに音楽の演奏部分を処理させるのは良い考えではなく、むしろService。もしそうなら、どのように?

さらに、再生、一時停止、停止の3つのボタンがあり、どちらが押されたかを識別できonReceive(...)ます。

前もって感謝します!


これがクラスです。

public class MusicManager extends AppWidgetProvider {

    private final String ACTION_WIDGET_PLAY = "PlaySong";
    private final String ACTION_WIDGET_PAUSE = "PauseSong";
    private final String ACTION_WIDGET_STOP = "StopSong";   
    private final int INTENT_FLAGS = 0;
    private final int REQUEST_CODE = 0;

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {

        RemoteViews controlButtons = new RemoteViews(context.getPackageName(),
                R.layout.main);

        Intent playIntent = new Intent(context, MusicService.class);

        Intent pauseIntent = new Intent(context, MusicService.class);

        Intent stopIntent = new Intent(context, MusicService.class);


        PendingIntent playPendingIntent = PendingIntent.getService(
                context, REQUEST_CODE, playIntent, INTENT_FLAGS);
        PendingIntent pausePendingIntent = PendingIntent.getService(
                context, REQUEST_CODE, pauseIntent, INTENT_FLAGS);
        PendingIntent stopPendingIntent = PendingIntent.getService(
                context, REQUEST_CODE, stopIntent, INTENT_FLAGS);

        controlButtons.setOnClickPendingIntent(
                R.id.btnPlay, playPendingIntent);
        controlButtons.setOnClickPendingIntent(
                R.id.btnPause, pausePendingIntent);
        controlButtons.setOnClickPendingIntent(
                R.id.btnStop, stopPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetIds, controlButtons);         
    }
}
4

2 に答える 2

4

<service android:name=".MusicService" android:enabled="true" /> マニフェストに追加されました!

于 2010-11-29T00:24:17.423 に答える
0

onUpdate(...)メソッドで、AppWidgetProvider次のようなものを使用してサービスを開始します(この例では、サービスをボタンクリックイベントに関連付けます)。

Intent intent = new Intent(context, MusicService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);

// Get the layout for the App Widget and attach an on-click listener to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);
views.setOnClickPendingIntent(R.id.button, pendingIntent);

詳細については、こちらをご覧ください

于 2010-11-28T21:41:21.603 に答える