在android中自定义通知中处理三个按钮单击事件

问题描述 投票:0回答:3

我有一个带有三个按钮的自定义通知(“ok”,“取消”,“稍后”)我只能在有一个按钮时才能捕获点击事件,但是有三个按钮我无法知道它们中的哪个按钮被点击。我的问题是“如何知道点击了哪个按钮?”

// I use this code in My "createNotification" function to add event to buttons
Intent intent = new Intent(Context.NOTIFICATION_SERVICE);
PendingIntent pendinghomeIntent = 
            PendingIntent.getBroadcast(getBaseContext(), 0, intent, 0);
notificationView.setOnClickPendingIntent(R.id.okButt, pendingrecentAppIntent);
notificationView.setOnClickPendingIntent(R.id.cancelButt, pendingrecentAppIntent);
notificationView.setOnClickPendingIntent(R.id.laterButt, pendingrecentAppIntent);

我想要一项注册和取消注册广播接收器的服务,只需一个按钮就可以完成。使用这三个按钮,当我点击每个按钮时,会发生相同的结果(在onReceive函数中实现),但我希望每个按钮都有不同的结果。

请帮助我,提前谢谢

android android-notifications buttonclick
3个回答
2
投票

你需要为每个按钮使用单独的PendingIntents(由Intents制作,具有不同的动作)。稍后在onReceive()你只需要检查传入的Intent的动作并根据它执行不同的代码。


1
投票

正如Sam所说,“你需要为每个按钮使用单独的PendingIntents(由Intents制作,具有不同的动作)。”我发现我必须为每个按钮使用单独的PendingIntents,但“不同动作”的概念对我不起作用。我使用setOnClickPendingIntent的第二个参数来区分三个PendingIntents,并使用FLAG_UPDATE_CURRENT作为第四个参数。我的代码是

Intent homeIntent = new Intent(Context.NOTIFICATION_SERVICE);
    Bundle yesBundle = new Bundle();            
    yesBundle.putInt("userAnswer", 1);
    homeIntent.putExtras(yesBundle);
    PendingIntent pendinghomeIntent = 
            PendingIntent.getBroadcast(getBaseContext(), 0, homeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notificationView.setOnClickPendingIntent(R.id.homeButt, pendinghomeIntent);

    Intent recentAppIntent = new Intent(Context.NOTIFICATION_SERVICE);
    Bundle recentAppBundle = new Bundle();   
    recentAppBundle.putInt("userAnswer", 2);
    recentAppIntent.putExtras(recentAppBundle);
    PendingIntent pendingrecentAppIntent = 
            PendingIntent.getBroadcast(getBaseContext(), 1, recentAppIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notificationView.setOnClickPendingIntent(R.id.recentAppButt, pendingrecentAppIntent);

    Intent settingsIntent = new Intent(Context.NOTIFICATION_SERVICE);
    Bundle settingsBundle = new Bundle();            
    settingsBundle.putInt("userAnswer", 3);
    settingsIntent.putExtras(settingsBundle);
    PendingIntent pendingsettingsIntent = 
            PendingIntent.getBroadcast(getBaseContext(), 2, settingsIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notificationView.setOnClickPendingIntent(R.id.settingsButt, pendingsettingsIntent);
    notificationManager.notify(1, notification);

和onReceive()代码是

public void onReceive(Context context, Intent intent) {
        Bundle answerBundle = intent.getExtras();
        int userAnswer = answerBundle.getInt("userAnswer");
        if(userAnswer == 1) {
            Log.v("shuffTest","Pressed Home");
        }else if(userAnswer == 2) {
            Log.v("shuffTest","Pressed Recent App");
        }else if(userAnswer == 3) {
            Log.v("shuffTest","Pressed Settings");
        }
    }

0
投票

在两天之前,我遇到了同样的问题,我发现我没有为每个未决的意图指定单独的行动,比如

  Intent recentApIntent = new Intent(Context.NOTIFICATION_SERVICE);
   recentApIntent.setAction("Action 1"); 
    Bundle recentAppBundle = new Bundle();   
    recentAppBundle.putInt("userAnswer", 2);
    recentAppIntent.putExtras(recentAppBundle);
    PendingIntent pendingrecentAppIntent = 
            PendingIntent.getBroadcast(getBaseContext(), 1, recentAppIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notificationView.setOnClickPendingIntent(R.id.recentAppButt, pendingrecentAppIntent);

类似地,每个意图与动作分开,并在接收广播时识别intent.getAction()并执行任务

© www.soinside.com 2019 - 2024. All rights reserved.