gpt4 book ai didi

android - 从 Android AppWidget 播放/控制歌曲

转载 作者:行者123 更新时间:2023-11-29 23:20:02 25 4
gpt4 key购买 nike

我创建了服务类来播放/暂停 Activity 中的歌曲。但是我无法从 AppWidget 运行该服务,也无法像我在 Activity 中处理的那样处理点击事件。非常感谢宝贵的评论/答案。谢谢。

服务等级

public class MusicService extends Service implements MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener,
MediaPlayer.OnCompletionListener {

//media player
private MediaPlayer player;
//song list
private ArrayList<Song> songs;
//current position
private int songPosn;
//binder
private final IBinder musicBind = new MusicBinder();

@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.e("***", "MusicService - onBind");
return musicBind;
}

@Override
public boolean onUnbind(Intent intent) {
Log.e("***", "MusicService - onUnbind");
player.stop();
player.release();
return false;
}

@Override
public void onDestroy() {
Log.e("***", "MusicService - onDestroy");
super.onDestroy();
}

@Override
public void onCreate() {
super.onCreate();
Log.e("***", "MusicService - onCreate");
//initialize position
songPosn = 0;
//create player
player = new MediaPlayer();
//initialize
initMusicPlayer();
}

public void initMusicPlayer() {
//set player properties
player.setWakeMode(getApplicationContext(),
PowerManager.PARTIAL_WAKE_LOCK);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
//set listeners
player.setOnPreparedListener(this);
player.setOnCompletionListener(this);
player.setOnErrorListener(this);
}

@Override
public void onCompletion(MediaPlayer mp) {

}

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return false;
}

@Override
public void onPrepared(MediaPlayer mp) {
//start playback
mp.start();
}

public void setList(ArrayList<Song> theSongs) {
songs = theSongs;
}

public class MusicBinder extends Binder {
public MusicService getService() {
return MusicService.this;
}
}

public void playSong() {
//play a song
player.reset();

//get song
Song playSong = songs.get(songPosn);
//get id
long currSong = playSong.getID();
//set uri
Uri trackUri = ContentUris.withAppendedId(
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
currSong);
//set the data source
try {
player.setDataSource(getApplicationContext(), trackUri);
} catch (Exception e) {
Log.e("MUSIC SERVICE", "Error setting data source", e);
}

player.prepareAsync();
}

public void stopSong() {
player.stop();
player.reset();
}

public void pauseSong() {
player.pause();
}

public void resumeSong() {
player.start();
}

public void setSong(int songIndex) {
songPosn = songIndex;
}
}

AppWidget 类

 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);

// //views.setTextViewText(R.id.appwidget_text, String.valueOf(appWidgetId));

// Create an Intent to launch ExampleActivity
// Intent intent = new Intent(context, MainActivity.class);
// PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

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

// Get the layout for the App Widget and attach an on-click listener
// to the button
views.setOnClickPendingIntent(R.id.appwidget_text, pendingIntent);

// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}

最佳答案

您的服务已绑定(bind),所以问题是您无法绑定(bind)到来自 AppWidgetProvider 的服务(因为它是一个 BroadcastReceiver ).您已经对按钮点击做出了待处理的 Intent ,只需将此 Intent 设为唯一并确定当前在 onStartCommand() 调用服务中执行的操作。

准备远程 View 时:

Intent intent = new Intent(context, MusicService.class);
intent.setAction("TEXT_CLICK");
PendingIntent pendingIntent = PendingIntent.getForegroundService(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.appwidget_text, pendingIntent);

所以现在在 onStartCommand 中检查这个 Action 是这样的:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent == null || intent.getAction() == null) {
return super.onStartCommand(null, flags, startId);
}

if (intent.getAction().equals("TEXT_CLICK")) {
//make whatever you want (play/stop song, next/previous etc)
}

return super.onStartCommand(intent, flags, startId);
}

关于android - 从 Android AppWidget 播放/控制歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54490651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com