Android:单击“操作(如致电)后取消通知”

问题描述 投票:4回答:2

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

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()函数

以便在单击通话操作时取消通知!

android notifications action
2个回答
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();

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"

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

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