在通知按钮上单击"意图"以启动新活动,而不是继续



我正在尝试为我的媒体播放器实现一个自定义通知,通知栏只有一个按钮,可以作为停止和播放控件。

到目前为止,我们已经成功地实现了通知,按钮正在进行函数调用,但问题是在Receive上,当我创建一个意向并调用该活动时,该活动会在旧活动的基础上被重新创建,并且我会在后台播放糟糕的双回声媒体播放器。

我曾尝试将launchMode设置为Single,但当我将其设置为Single时,单击按钮没有任何区别,这意味着如果我将启动模式设置为Single而不是STANDARD,则不会进行函数调用。

主活动代码段

//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();
}
}

通知按钮点击的广播监听器通过INTENT 调用ACTIVTY

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);
}
}

我的清单

<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:在STACK 上尝试了所有可能的答案

问题在于您的意图标志

您已经添加了标志CCD_ 1,将其更改为intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

欲了解更多参考,请阅读https://developer.android.com/reference/android/content/Intent.html

最新更新