【问题标题】:How to detect if yes or no is pressed in Notification Android [duplicate]如何检测Notification Android中是否按下是或否[重复]
【发布时间】: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


【解决方案1】:

在每个动作中,您需要声明一个 PendingIntent,它将在用户单击该动作后处理该动作。在这个 PendingIntent 中,如果您使用 Intent.putExtra(key,value),您可以轻松地传递数据,然后从您声明的广播接收器中检索此数据。

  //done action button
    Intent doneIntent = new Intent(this, NotificationDoneReceiver.class);
    doneIntent.putExtra("id", id);
    doneIntent.putExtra("notification_id",unique_id);
    PendingIntent donePendingIntent = PendingIntent.getBroadcast(this, (unique_id * 16), doneIntent, 0);

 //create the notification
    Notification n = builder
            //set title
            .setContentTitle(title)

            .setPriority(NotificationCompat.PRIORITY_MAX)

            .setWhen(0)
                    //set content
            .setContentText(getString(R.string.time_for_medicine) + " " + medicine)
                    //set intent to launch on content click
            .setContentIntent(pIntent)
                    //cancel notification on click
            .setAutoCancel(true)
                    //add the actions
            .addAction(R.drawable.ic_done, getString(R.string.took_it), donePendingIntent)

            .build();

完成操作的广播接收器。

public class NotificationDoneReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    int id = intent.getExtras().getInt("id");
    int notification_id = intent.getExtras().getInt("notification_id");

    RealmDatabase db = new RealmDatabase(context);
    MedicationPlanEvent medicationPlanEvent = db.getMedicationPlanEventBasedOnID(id);

    if(medicationPlanEvent!=null){
        Medication medication = new Medication(medicationPlanEvent.getMedicine(), TimeManager.getCurrentDate(),medicationPlanEvent.getDose(),TimeManager.getCurrentTime(),"",medicationPlanEvent.getDose_meter());
        medication.setId(db.getMedicationNextKey());

        db.insertMedication(medication);
    }


    //notification manager
    NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);

    notificationManager.cancel(notification_id);

    Intent overviewIntent = new Intent(context.getApplicationContext(), OverviewActivity.class);
    overviewIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(overviewIntent);


}

}

【讨论】:

    【解决方案2】:

    这不是它的工作原理。您必须使用BroadcastReciever 观察此操作,并在运行ActivityFragmentService 中注册。

    IntentFilter filter = new IntentFilter();
    filter.addAction(YES_ACTION);
    filter.addAction(NO_ACTION);
    
    BroadcastReceiver receiver = new BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            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();
            }
        }
    }
    context.registerReceiver(receiver, filter);
    

    ActivityFragmentService 被销毁时,不要忘记注销它。

    【讨论】:

    • 事情是点击是按钮,我想将详细信息保存在 db..ie(最终上下文 ctx,最终字符串编号,最终日期开始)中,从来电中获得....如果这样做你说什么..我无法在 Onrecive 函数中获得详细信息
    • @Androidss 我不这么认为。在onRecieve(Context context, Intent intent) 你有你需要的一切: 1. 上下文,你可以用它来打开数据库或访问系统服务; 2.intent,通过Intent拥有所有共享数据。尝试使用它们来存储您需要的所有数据。
    猜你喜欢
    • 2020-05-19
    • 2022-10-14
    • 1970-01-01
    • 2022-10-02
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多