2017-07-20 98 views
0

以下是我的代碼。我已經把行動(是或否)使用的addAction到notification.But的問題是,如果它按我無法檢測或not.Plz幫助..謝謝如何檢測是否按通知Android

@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。

回答

0

那不是它是如何工作的。您必須使用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被破壞。

+0

事情是在點擊Yes按鈕,我想保存在db..ie從來電中獲得的細節(最後語境環磷酰胺,最終串號,最後的日期開始)...如果你說我不能在Obregive功能中使用詳細信息 – Androidss

+0

@Androidss我不這麼認爲。在'onRecieve(Context context,Intent intent)'中,您擁有所有您需要的內容:1. context,您可以使用它打開數據庫或訪問系統服務; 2. intent,它通過'Intent'擁有所有共享數據。嘗試使用它們來存儲所有你需要的數據。 –

0

在每一個動作中,你需要聲明一個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); 


} 

}