cjb*_*s12 18 notifications android broadcastreceiver
我有一个应用程序,持续通知,以帮助记忆.我希望能够通过其中一个操作按钮关闭此通知,但我不想在按下按钮时打开应用程序.我更喜欢使用内置的通知操作按钮,而不是创建一个RemoteViews对象来填充通知.我在我的应用程序中看到了一个在这个按钮上使用BroadcastReceiver的帖子提及,虽然他的教程非常无益,但听起来这是朝着正确的方向发展.
Intent resultIntent = new Intent(getBaseContext(), Dashboard.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getBaseContext());
stackBuilder.addParentStack(Dashboard.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
Intent cancel = new Intent(getBaseContext(), CancelNotification.class);
stackBuilder.addParentStack(Dashboard.class);
stackBuilder.addNextIntent(cancel);
PendingIntent pendingCancel =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder mb = new NotificationCompat.Builder(getBaseContext());
mb.setSmallIcon(R.drawable.cross_icon);
mb.setContentTitle(ref);
mb.setContentText(ver);
mb.setPriority(NotificationCompat.PRIORITY_LOW);
mb.setOngoing(true);
mb.setStyle(new NotificationCompat.BigTextStyle().bigText(ver));
mb.setContentIntent(resultPendingIntent);
mb.addAction(R.drawable.ic_cancel_dark, "Dismiss", pendingCancel);
manager.notify(1, mb.build());
Run Code Online (Sandbox Code Playgroud)
ram*_*ral 69
从这开始:
int final NOTIFICATION_ID = 1;
//Create an Intent for the BroadcastReceiver
Intent buttonIntent = new Intent(context, ButtonReceiver.class);
buttonIntent.putExtra("notificationId",NOTIFICATION_ID);
//Create the PendingIntent
PendingIntent btPendingIntent = PendingIntent.getBroadcast(context, 0, buttonIntent,0);
//Pass this PendingIntent to addAction method of Intent Builder
NotificationCompat.Builder mb = new NotificationCompat.Builder(getBaseContext());
.....
.....
.....
mb.addAction(R.drawable.ic_Action, "My Action", btPendingIntent);
manager.notify(NOTIFICATION_ID, mb.build());
Run Code Online (Sandbox Code Playgroud)
创建BroadcastReceiver:
public class ButtonReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int notificationId = intent.getIntExtra("notificationId", 0);
// Do what you want were.
..............
..............
// if you want cancel notification
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(notificationId);
}
}
Run Code Online (Sandbox Code Playgroud)
如果您不希望在用户单击通知时显示任何活动,请setContentIntent以这种方式定义意图传递:
PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
......
......
mb.setContentIntent(resultPendingIntent);
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
接受的解决方案不适用于 Android 8.1 及更高版本。
按照已接受答案中的相同步骤进行操作,但更新此行:
//Create the PendingIntent
PendingIntent btPendingIntent = PendingIntent.getBroadcast(context, 0, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Run Code Online (Sandbox Code Playgroud)
也可以看看PendingIntent.FLAG_UPDATE_CURRENT
| 归档时间: |
|
| 查看次数: |
24717 次 |
| 最近记录: |