我怎么知道在通知中点击了哪个按钮? [英] How can I know which button clicked on it inside notification?

查看:32
本文介绍了我怎么知道在通知中点击了哪个按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在每个通知中有三个按钮(主通知按钮、计时器按钮和禁用按钮).

I have three buttons in each notification (the main notification button, the timer button, and the disable button).

  • 主通知按钮运行前台服务
  • 计时器按钮运行前台服务,但显示吐司消息
  • 禁用按钮从状态栏中删除通知
  • The main notification button runs the foreground service
  • The timer button runs the foreground service, but shows the toast message
  • The disable button removes notification from status bar

有没有办法为所有三个按钮使用一个前台服务?如果是这样,我如何确定在服务中单击了哪个按钮?还是我必须为每个按钮创建三个前台服务?

Is there a way to use one foreground service for all three buttons? If so, how do I determine which button was clicked within the service? Or must I create three foreground services for each button?

我的代码

PendingIntent pendingIntentMain = PendingIntent.getService(context, 0, new Intent(context, ForegroundService.class).putExtra("main", "a"), PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntentTimer = PendingIntent.getService(context, 0, new Intent(context, ForegroundService.class).putExtra("timer", "b"), PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntentDisable = PendingIntent.getService(context, 0, new Intent(context, ForegroundService.class).putExtra("disable", "c"), PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(context, String.valueOf(NOTIFICATION_ID))
    .addAction(R.mipmap.ic_launcher, context.getString(R.string.timer), pendingIntentTimer)
    .addAction(R.mipmap.ic_launcher, context.getString(R.string.disable), pendingIntentDisable)
    .setContentIntent(pendingIntentMain)
    .setOngoing(true)
    .setAutoCancel(false)
    .setShowWhen(false)
    .setSmallIcon(R.mipmap.ic_launcher)
    .setContentTitle(context.getString(R.string.app_name))
    .setContentText(context.getString(R.string.tap_to_enable_service))
    .setPriority(NotificationCompat.PRIORITY_DEFAULT);

前台服务

public class ForegroundService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.w("ABC", "" + intent.getStringExtra("main"));
        Log.w("ABC", "" + intent.getStringExtra("timer"));
        Log.w("ABC", "" + intent.getStringExtra("disable"));
        Log.w("ABC", "" + intent.getExtras());
        return super.onStartCommand(intent, flags, startId);
    }

}

onStartCommand 里面的 Log.w 什么都没显示

The Log.w inside onStartCommand shows nothing

推荐答案

无需创建不同的前台服务,您可以使用 Intent Action 而不是将 Intent 与数据一起传递.喜欢,

No need to create different Foreground Services, You can use Intent Action instead of passing intent with data. Like,

Intent intent = new Intent(this , ClipTextObserverService.class);
intent.setAction("specify_name_of_the_action")

PendingIntent.getService(
            this,
            1464,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT
        );

相应地更改标志.在 addAction() 方法中传递这个 pendingIntent.然后检查Service.Like的onStartCommand()方法中的动作,

Change the Flag accordingly. Pass this pendingIntent in addAction() method. Then Check for the action in onStartCommand() method of Service.Like,

@Override
public int onStartCommand(Intent Intent , Int flags , Int startId) {

switch (intent.getAction()) {
 case "your_action_name":
    // TODO == Perform your action here.
    break;
}

return super.onStartCommand(intent, flags , startId);
}

这篇关于我怎么知道在通知中点击了哪个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆