简体   繁体   English

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

[英]On notification button click intent starting new activity instead of resuming

I am trying to implement a custom notification for my mediaplayer with notification bar that has just one button that will act as stop and play control. 我正在尝试通过通知栏为我的媒体播放器实现自定义通知,该通知栏只有一个按钮,可以用作停止和播放控件。

So far have implemented the notification successfully and the button is making the function call, but the problem is that onReceive when I create an intent and call the activity, the activity gets recreated on top of the old one and I get bad double echoing media player playing in the background. 到目前为止,已经成功实现了通知,并且按钮正在调用函数,但是问题是,当我创建一个意图并调用活动时,onReceive会在旧的活动之上重新创建活动,并且我得到了不好的双重回声媒体播放器在后台播放。

Have tried to make the launchMode= Single, but when I make it single the button click makes no difference, it means that the function call is not getting made if I turn the launch mode to SINGLE instead of STANDARD . 尝试将launchMode = Single设置为单个,但是当我将其设置为Single时,单击按钮没有区别,这意味着如果我将启动模式设置为SINGLE而不是STANDARD,则不会进行函数调用。

MAIN_ACTIVITY CODE SNIPPET MAIN_ACTIVITY代码片段

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

THE BROADCAST LISTENER FOR THE NOTIFICATION BUTTON CLICK THAT CALLS THE ACTIVTY VIA INTENT 用于通知按钮的广播级收听者,可通过意图调用该活动

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>

Thanks in advance PS: TRIED EVERY POSSIBLE ANSWER ON STACK 在此先感谢PS:在堆栈上尝试了所有可能的答案

The problem lies within your intent Flag 问题出在您的意图标志之内

you have added the flag intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 您已经添加了标志intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); , change that to intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); ,将其更改为intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

For more reference read this https://developer.android.com/reference/android/content/Intent.html 有关更多参考,请阅读此https://developer.android.com/reference/android/content/Intent.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM