【问题标题】:Android : Cancel Notification after click on Action (Like Call)Android:点击操作后取消通知(喜欢通话)
【发布时间】:2012-12-27 11:59:22
【问题描述】:

我有一个通过以下方式拨打号码的操作

uri = Uri.parse("tel:" + address);
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(uri);
PendingIntent pd = PendingIntent.getActivity(context, 0,intent, 
       PendingIntent.FLAG_UPDATE_CURRENT);
notif.addAction(R.drawable.ic_menu_call, "Call", pd);

但问题是我不知道

如何/何时调用 NotificationManager 的 manager.cancel() 函数

以便在点击呼叫动作时关闭通知!

【问题讨论】:

  • 处理手机状态广播action.and call notification.cancel by notification manager
  • 可能提供示例代码?

标签: android notifications action


【解决方案1】:

我遇到了同样的情况,我设法通过创建一个在按下操作按钮时调用的广播接收器来解决它。然后,广播接收器会收到一个带有您要关闭的通知 ID 和您要拨打的号码的意图。

这是创建通知的代码:

NotificationManager notificationManager =
  (NotificationManager)MyApplication.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE);
//for some versions of android you may need to create a channel with the id you want
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel chan = new NotificationChannel("your_channel_id", "ChannelName", NotificationManager.IMPORTANCE_DEFAULT);
    notificationManager.createNotificationChannel(chan);
}
Intent intent = new Intent(MyApplication.getAppContext(), ActionReciever.class);
intent.putExtra("phoNo", phoneNumber);

// num is the notification id
intent.putExtra("id", num);

PendingIntent myPendingIntent = PendingIntent.getBroadcast(
                  MyApplication.getAppContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT
                );
Notification n = new NotificationCompat.Builder(MyApplication.getAppContext(),
                        "your_channel_id")
                        .setSmallIcon(R.drawable.app_pic)
                        .addAction(R.drawable.app_pic, "Dial now", myPendingIntent)
                        .setAutoCancel(true)
                        .build();
notificationManager.notify(num, n);

这是广播接收器代码,当按下操作按钮时调用它。这里接收到的intent就是我们在通知中准备的pending intent里面的intent:

public class ActionReciever extends BroadcastReceiver {
    @SuppressLint("MissingPermission")
    @Override
    public void onReceive(Context context, Intent intent) {
        String phoneNumber = intent.getStringExtra("phoNo");
        int id = intent.getIntExtra("id",0);
        Intent i = new Intent(Intent.ACTION_DIAL);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.setData(Uri.parse("tel:" + phoneNumber));
        NotificationManager notificationManager =
                (NotificationManager) MyApplication.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(id);
        context.startActivity(i);
    }
}

在应用标签内的应用清单中注册广播接收器

<receiver android:name=".ActionReciever" />

MyApplication 是一个扩展默认应用程序的类,因此我可以有一个地方来存储我需要的上下文。

public class MyApplication extends Application {
    private static Context context;

    public void onCreate() {
        super.onCreate();
        MyApplication.context = getApplicationContext();
    }

    public static Context getAppContext() {
        return MyApplication.context;
    }

}

请注意,您需要更新清单以运行 MyApplication 类,如下所示:

android:name="com.example.yourpackage.MyApplication"

即使应用已关闭且没有后台服务,此代码也能正常工作。

【讨论】:

    【解决方案2】:

    参见Android READ PHONE STATE? - 关于手机状态。

    case TelephonyManager.CALL_STATE_RINGING:
        notificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.cancel(100); // cancel notification by ID
                        break;
    

    // 构建您的通知。

    intent notificationIntent = new Intent(context,
                        YourPhoneActivity.class);
                notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                PendingIntent intent = PendingIntent.getActivity(context, 0,
                        notificationIntent, 0);
    
                Bitmap bm = BitmapFactory.decodeResource(context.getResources(),
                        iconLarge);
                NotificationCompat.Builder builder = new NotificationCompat.Builder(
                        context).setSmallIcon(iconSmall).setLargeIcon(bm)
                        .setContentTitle(title).setContentText(message)
                        .setAutoCancel(false).setContentIntent(intent).setWhen(when)
                        .setTicker(message);
                 builder.getNotification();
    

    【讨论】:

    • 以上,我得发起一个活动,对吧?我希望在不启动任何创建 phonestatelistener 的活动的情况下关闭通知。只需:接收通知->单击呼叫->打开电话应用程序,然后关闭通知。还有其他像广播接收器的方式吗?
    • 首先 - 你应该在某处创建通知(活动,服务)
    • 我在 BroadcastReceiver 中创建通知。有没有办法注册一个拨号意图接收器或在发送拨号意图时可以简单地启动的东西?或任何其他不涉及要启动的单独活动或在后台运行服务以完成此类小任务的任何其他方式!
    • 在服务中创建通知
    • 即使您在服务中创建通知:您如何关闭通知,例如启动蓝牙(使用BluetoothAdapter.ACTION_REQUEST_ENABLE)?如果用户说不,你不能知道它被点击了。
    猜你喜欢
    • 1970-01-01
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多