具有多个操作的通知

Rod*_*zke 7 notifications android android-pendingintent

我有这个代码来做这些动作:

Intent playIntent = new Intent(Intent.ACTION_VIEW);
playIntent.setDataAndType(uri, path.contains(".jpg") ? "image/jpeg" : "video/mp4");
PendingIntent play = PendingIntent.getActivity(context, 1, playIntent, 0);
mBuilder.addAction(R.mipmap.ic_play_arrow_black_48dp, "", play);

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType(path.contains(".jpg") ? "image/jpeg" : "video/mp4");
PendingIntent share = PendingIntent.getActivity(context, 2, shareIntent, 0);
mBuilder.addAction(R.mipmap.ic_share_white_48dp, "", share);

Intent doneIntent = new Intent(context, NotificationCloser.class);
doneIntent.putExtra("notificationId", notificationId);
PendingIntent done = PendingIntent.getBroadcast(context, 3, doneIntent, 0);
mBuilder.addAction(R.mipmap.ic_done_black_48dp, "", done);
Run Code Online (Sandbox Code Playgroud)

这是我的广播接收器

public class NotificationCloser extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        int id = intent.getIntExtra("notificationId", 0);
        Log.i("MyInfo", "NotificationCloser.onReceive(" + id + ")");
        MainActivity.mNotifyManager.cancel(id);
    }
}
Run Code Online (Sandbox Code Playgroud)

当我点击播放或分享按钮时,它会根据功能打开默认应用程序以查看图像或视频,但不会关闭通知.当我点击完成按钮时,仅在我第一次正确收到ID时,第一次获取第一次发送的ID

有人能帮我吗?

编辑:

通过使用随机请求代码解决了广播问题,还添加了标志PendingIntent.FLAG_UPDATE_CURRENT.全部使用,因为有时共享窃听并使用另一个图像.

PendingIntent play = PendingIntent.getActivity(context, new Random().nextInt(150) * 37, playIntent, 0);
PendingIntent share = PendingIntent.getActivity(context, new Random().nextInt(150) * 37, shareIntent, 0);
PendingIntent done = PendingIntent.getBroadcast(context, new Random().nextInt(150) * 37, doneIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Run Code Online (Sandbox Code Playgroud)

vin*_*zee 0

我通过使用随机请求代码解决了广播的问题,还添加了标志PendingIntent.FLAG_UPDATE_CURRENT。我全部使用,因为有时共享会出错并使用另一个图像。

PendingIntent play = PendingIntent.getActivity(context, new Random().nextInt(150) * 37, playIntent, 0);
PendingIntent share = PendingIntent.getActivity(context, new Random().nextInt(150) * 37, shareIntent, 0);
PendingIntent done = PendingIntent.getBroadcast(context, new Random().nextInt(150) * 37, doneIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Run Code Online (Sandbox Code Playgroud)

此答案是由 OP Rodrigo Butzke在 CC BY-SA 3.0 下作为对问题“具有多个操作的通知”的编辑而发布的。