【发布时间】:2017-12-25 22:35:24
【问题描述】:
下面是我的代码。我已经使用 addaction 将操作(是或否)添加到通知中。但问题是我无法检测到它是否按下。请帮助.. 谢谢
@SuppressWarnings("deprecation")
@Override
protected void onIncomingCallReceived(final Context ctx, final String number, final Date start) {
// Create Intent for notification
Intent intent = new Intent(ctx, CallReceiver.class);
PendingIntent pi = PendingIntent.getActivity(ctx, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
//Yes intent
Intent yesReceive = new Intent();
yesReceive.setAction(YES_ACTION);
PendingIntent pendingIntentYes = PendingIntent.getBroadcast(ctx, 0, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT);
//No intent
Intent noReceive = new Intent();
noReceive.setAction(NO_ACTION);
PendingIntent pendingIntentNo = PendingIntent.getBroadcast(ctx, 0, noReceive, PendingIntent.FLAG_UPDATE_CURRENT);
// Defining notification
NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(
ctx).setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Sample text 1 ?")
.setContentText("Sample text 2").setContentIntent(pi)
.addAction(R.drawable.ic_done_white_36dp, "Yes", pendingIntentYes)
.addAction(R.drawable.ic_close_white_36dp, "No", pendingIntentNo);
// Allows notification to be cancelled when user clicks it
nBuilder.setAutoCancel(true);
// Issuing notification
int notificationId = 0;
NotificationManager notifyMgr = (NotificationManager) ctx.getSystemService(NOTIFICATION_SERVICE);
String action = intent.getAction();
if (CallReceiver.YES_ACTION.equals(action)) {
Toast.makeText(ctx, "YES CALLED", Toast.LENGTH_SHORT).show();
} else if (CallReceiver.NO_ACTION.equals(action)) {
Toast.makeText(ctx, "STOP CALLED", Toast.LENGTH_SHORT).show();
}
notifyMgr.notify(notificationId, nBuilder.build());
}
此代码在我的 CallReciever.class 中,它扩展了 Phonecallreciever 其中,phonecallreciever 扩展了 broadcastreciver。
【问题讨论】:
标签: android android-intent broadcastreceiver android-toast