通知操作在Android中不起作用

0aydgbwb  于 2023-02-02  发布在  Android
关注(0)|答案(2)|浏览(157)

我有这个密码

Intent launchIntent = new Intent(context,WidgetAlarmManagerActivity.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, launchIntent, 0);

            Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("Nicilis")
                    .setTicker(messageBody)
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .addAction(android.R.drawable.ic_menu_close_clear_cancel, "Cancel Upload", pendingIntent)
                    .setSound(defaultSoundUri);

            NotificationManager notificationManager =
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

            notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

操作在通知中,但当我按下它时,没有任何React
多谢了

j0pj023g

j0pj023g1#

Intent launchIntent = new Intent(context,WidgetAlarmManagerActivity.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, launchIntent, 0);

getBroadcast()返回一个PendingIntent,该PendingIntent将在 Package 的Intent上调用sendBroadcast()getActivity()返回一个PendingIntent,该PendingIntent将在 Package 的Intent上调用startActivity()WidgetAlarmManagerActivity看起来像是一个活动的名称,建议您应将getBroadcast()替换为getActivity()

xa9qqrwz

xa9qqrwz2#

我觉得与其

pendingIntent = PendingIntent.getBroadcast(context, 0, launchIntent, 0);

你应该用

pendingIntent = PendingIntent.getActivity(context,
                0, launchIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);

相关问题