我正在尝试为我的媒体播放器实现自定义通知,其中通知栏只有一个按钮,可用作停止和播放控件。
到目前为止已经成功实现了通知并且按钮正在进行函数调用,但问题是onReceive在创建intent并调用activity时,活动在重新创建之上旧的,我得到了糟糕的双重回应媒体播放器在后台播放。
尝试使launchMode = Single,但是当我单击按钮时单击按钮没有任何区别,这意味着如果我将启动模式设置为 SINGLE ,则不会进行函数调用而不是 STANDARD 。
MAIN_ACTIVITY CODE SNIPPET
//NOTIFICATION RELATED CLASSES
private NotificationCompat.Builder builder;
private NotificationManager notificationManager;
//private int notification_id;
private RemoteViews remoteViews;
Context context;
Intent notification_intent;
final int notification_id =545816666;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean attachMedia = getIntent().getBooleanExtra("attachMedia",false);
if (attachMedia) {
attachMediaActivity();
}
//CODE DE NOTIFICATION
context =this;
notification_intent=new Intent(context,MainActivity.class);
notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
remoteViews=new RemoteViews(getPackageName(),R.layout.custom_notification);
remoteViews.setImageViewResource(R.id.notif_icon,R.mipmap.ic_launcher);
remoteViews.setTextViewText(R.id.notif_title,"BOOM");
remoteViews.setTextViewText(R.id.button,"Button");
Intent button_intent= new Intent("player_control_clicked");
button_intent.putExtra("id",notification_id);
PendingIntent p_button_intent=PendingIntent.getBroadcast(context,123,button_intent,0);
remoteViews.setOnClickPendingIntent(R.id.button,p_button_intent);
tview=(TextView) findViewById(R.id.playerText);
btn=(Button) findViewById(R.id.playerButton);
if(!mediaPlayer.isPlaying())
{
tview.setText("Press play");
btn.setText("Play");
}
else
{
tview.setText("Playing");
btn.setText("Stop");
}
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Intent notification_intent=new Intent(context,MainActivity.class);
//THIS CODE UPDATES OLD NOTIFICATION
PendingIntent pendingIntent=PendingIntent.getActivity(context,0,notification_intent,PendingIntent.FLAG_UPDATE_CURRENT);
builder =new NotificationCompat.Builder(context);
builder.setSmallIcon(R.mipmap.ic_launcher)
.setCustomBigContentView(remoteViews)
.setContentIntent(pendingIntent)
.setOngoing(true);
notificationManager.notify(notification_id,builder.build());
if(!mediaPlayer.isPlaying())
{
//tview.setText("Playing");
btn.setText("Stop");
playStream();
}
else
{
tview.setText("Stopped");
btn.setText("Play");
mediaPlayer.stop();
mediaPlayer.reset();
}
}
});
}
public void attachMediaActivity()
{
//CODE DE NOTIFICATION
context =this;
notification_intent=new Intent(context,MainActivity.class);
notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
remoteViews=new RemoteViews(getPackageName(),R.layout.custom_notification);
remoteViews.setImageViewResource(R.id.notif_icon,R.drawable.stream_icon);
remoteViews.setTextViewText(R.id.notif_title,"Stopped");
remoteViews.setTextViewText(R.id.button,"STOPA");
Intent button_intent= new Intent("player_control_clicked");
button_intent.putExtra("id",notification_id);
/*PendingIntent p_button_intent=PendingIntent.getBroadcast(context,123,button_intent,0);
remoteViews.setOnClickPendingIntent(R.id.button,p_button_intent);*/
PendingIntent pendingIntent=PendingIntent.getActivity(context,0,notification_intent,PendingIntent.FLAG_UPDATE_CURRENT);
builder =new NotificationCompat.Builder(context);
builder.setSmallIcon(R.mipmap.ic_launcher)
.setCustomBigContentView(remoteViews)
.setContentIntent(pendingIntent)
.setOngoing(true);
notificationManager.notify(notification_id,builder.build());
if (mediaPlayer.isPlaying())
{
mediaPlayer.stop();
}
else
{
playStream();
}
}
用于通知按钮的广播听众点击通过直接调用活动
public class Button_listener extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(intent.getExtras().getInt("id"));
Toast.makeText(context, "GENERATED BY NOTIFICATION", Toast.LENGTH_SHORT).show();
intent= new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//intents.addExtra("attachMedia",true); // Extra info
intent.putExtra("attachMedia",true);
context.startActivity(intent);
}
}
MY MANIFEST
<activity android:name=".MainActivity"
android:screenOrientation="portrait"
android:launchMode="singleTop"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="Button_listener">
<intent-filter>
<action android:name="player_control_clicked"/>
</intent-filter>
</receiver>
提前致谢 PS:在堆叠上尝试每一个可能的答案
答案 0 :(得分:0)
问题在于你的意图标志
你添加了标志intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
,
将其更改为intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
如需更多参考,请阅读此https://developer.android.com/reference/android/content/Intent.html