我有一个以Service
播放音乐的应用。还有一个Widget来控制音乐播放器。一切正常。
现在,在用户关闭应用程序或崩溃后,或者如果我使用System.exit();
或getActivity().finish();
关闭应用程序,则存在一些问题。一切似乎都运行良好,但不管怎样,即使另一个Activity
仍然收到广告,小部件也不会再接收广播。
小部件仍然能够发送广播,它只能接收它们。
有人可以帮帮我吗?
我的小部件中的代码:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
ma = (GlobalSongList)context.getApplicationContext();
context.getApplicationContext().registerReceiver(broadcastCoverReceiver, new IntentFilter(Music_service.BROADCAST_COVER));
context.getApplicationContext().registerReceiver(broadcastPlayPause, new IntentFilter(BROADCAST_PLAYPAUSE));
context.getApplicationContext().registerReceiver(checkagain, new IntentFilter("xyz"));
context.getApplicationContext().registerReceiver(intentshuttingdown, new IntentFilter("shutdown"));
RemoteViews controlButtons = new RemoteViews(context.getPackageName(),R.layout.widget);
Intent playIntent = new Intent(BROADCAST_PLAYPAUSE);
PendingIntent playPendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, playIntent, PendingIntent.FLAG_UPDATE_CURRENT);
controlButtons.setOnClickPendingIntent(R.id.bPlay, playPendingIntent);
Intent previousIntent = new Intent(why);
PendingIntent previousPendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, previousIntent, PendingIntent.FLAG_UPDATE_CURRENT);
controlButtons.setOnClickPendingIntent(R.id.bprev, previousPendingIntent);
if(ma.NP_List.size()>0) {
controlButtons.setTextViewText(R.id.song, ma.NP_List.get(ma.position).song);
controlButtons.setTextViewText(R.id.artist, ma.NP_List.get(ma.position).getArtist());
Intent open_activityIntent = new Intent(BROADCAST_open_activity);
PendingIntent openactivityPendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, open_activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
controlButtons.setOnClickPendingIntent(R.id.parent, openactivityPendingIntent);
if(ma.boolMusicPlaying1 == true) {
controlButtons.setImageViewResource(R.id.bPlay, R.drawable.pause2);
} else {
controlButtons.setImageViewResource(R.id.bPlay, R.drawable.play);
}
} else if(ma.NP_List.size() == 0) {
controlButtons.setTextViewText(R.id.song, "");
controlButtons.setTextViewText(R.id.artist, "");
}
Intent nextIntent = new Intent(BROADCAST_SWAP);
nextIntent.putExtra("nextprev", 1);
PendingIntent nextPendingIntent = PendingIntent.getBroadcast(context, 0, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
controlButtons.setOnClickPendingIntent(R.id.bNext, nextPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, controlButtons);
}
public BroadcastReceiver broadcastCoverReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent serviceIntent) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext());
ComponentName thisWidget = new ComponentName(context.getApplicationContext(), MyWidget.class);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
if (appWidgetIds != null && appWidgetIds.length > 0) {
onUpdate(context, appWidgetManager, appWidgetIds);
}
}
};
答案 0 :(得分:1)
你应该使用Android应用程序小部件,而不是使用广播,因为它是最常规的做法....这也在文档中提到
答案 1 :(得分:0)
您应该在活动中bind
和unbind
分别提供onResume
和onPause
后台服务。
访问Android Activity and Service relationship - after Pause, after Stop以了解服务与活动之间的关系