Android:点击动作后取消通知(如通话)

时间:2012-12-27 11:59:23

标签: android notifications action

我有一个通过

拨号的动作
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()函数

以便在点击通话操作时解除通知!

2 个答案:

答案 0 :(得分:0)

请参阅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();

答案 1 :(得分:0)

我遇到了同样的情况,我设法通过创建一个广播接收器来解决它,该广播接收器在按下操作按钮时被调用。然后,广播接收器会收到一个意图,其中包含您要取消的通知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);

这是广播接收器代码,按下操作按钮时将被调用。此处收到的意图是我们在通知中准备的未决意图内的意图:

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

在应用标签中的应用清单中注册BroadcastReceiver

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

MyApplication是扩展默认Application的类,因此我可以在某个地方存储所需的上下文。

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"

即使应用程序关闭且没有后台服务,此代码也可以正常工作。