我正在制作音乐播放器小部件(主屏幕小部件).. 它只需要播放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);
}
}
答案 0 :(得分:4)
已添加<service android:name=".MusicService" android:enabled="true" />
到清单!
答案 1 :(得分:0)