Android 在通知按钮上,单击开始新活动而不是继续

Android 在通知按钮上,单击开始新活动而不是继续,android,android-intent,android-activity,android-mediaplayer,android-notifications,Android,Android Intent,Android Activity,Android Mediaplayer,Android Notifications,我正在尝试为我的mediaplayer实现一个自定义通知,通知栏只有一个按钮可以作为停止和播放控件 到目前为止,我已经成功地实现了通知,按钮正在进行函数调用,但问题是,当我创建意图并调用活动时,onReceive会在旧活动的基础上重新创建活动,并且我会在后台播放坏的双回显媒体播放器 已尝试使launchMode=Single,但当我将其设置为Single时,按钮单击没有任何区别,这意味着如果我将启动模式改为Single,而不是STANDARD,则不会进行函数调用 主活动代码段 //NOTIFIC

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

到目前为止,我已经成功地实现了通知,按钮正在进行函数调用,但问题是,当我创建意图并调用活动时,onReceive会在旧活动的基础上重新创建活动,并且我会在后台播放坏的双回显媒体播放器

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

问题在于您的意图标志

您已经添加了标志
intent.setFlags(intent.flag\u ACTIVITY\u NEW\u TASK),,
将其更改为
intent.setFlags(intent.FLAG\u ACTIVITY\u CLEAR\u TOP)


有关更多参考信息,请阅读此

您是否尝试删除
活动的
android:noHistory=“true”
?当您的应用程序位于前台并从通知中播放时,还是当您通过按“上一步”按钮关闭应用程序并从通知中播放时,会出现此问题?@VyshnavRamesh是的。。我只是找到了一个解决方法,而不是依赖于活动,我现在依赖于后台服务,现在我的通知完全独立于主UI。很高兴您找到了解决方案。这在新API中不再有效:“调用startActivity()从活动上下文外部需要标志“活动”新任务标志。这真的是你想要的吗?@Talu请看我发布这篇文章的日期,可能有很多变化。我从Android堆栈开始,所以不确定现在发生了什么。如果您有任何建议,请以单独的答案发布。快乐的CodingHi,我不是想纠正你,我只是想把这个澄清留给从现在开始阅读这个答案的人。顺致敬意,