2017-05-25 226 views
0

因此,我有一個鬧鐘應用程序...當接收器從報警類獲取意圖,它會創建一個通知,並建立它..但我似乎無法弄清楚如何到onclick事件添加到button..i希望它實現的功能不只是得到一個意向添加點擊事件通知按鈕

這是我的接收機

public class AlarmReceiver extends BroadcastReceiver { 


@Override 
public void onReceive(Context arg0, Intent arg1) { 
    Context context= arg0; 
    Intent intent = new Intent(context,MainActivity.class); 
    PendingIntent pendingIntent=PendingIntent.getActivity(context,0,intent,0); 

    NotificationCompat.Builder mBuilder = 
      (NotificationCompat.Builder) new NotificationCompat.Builder(context) 
        .setSmallIcon(R.drawable.mini) 
        .setContentTitle(context.getResources().getString(R.string.message_box_title)) 
        .setContentText(context.getResources().getString(R.string.message_timesheet_not_up_to_date)) 
        .addAction(R.drawable.bell,"snooze",pendingIntent); 
    Intent resultIntent = new Intent(context, MainActivity.class); 
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
    stackBuilder.addParentStack(MainActivity.class); 
    stackBuilder.addNextIntent(resultIntent); 
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 
    mBuilder.setContentIntent(resultPendingIntent); 
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    mNotificationManager.notify(1, mBuilder.build()); 







    Toast.makeText(arg0, "Alarm received!", Toast.LENGTH_LONG).show(); 

    Integer get_your_alarm_choice = arg1.getExtras().getInt("alarm_choice"); 
    Log.e("alarm choice is",get_your_alarm_choice.toString()); 
} 

任何幫助將非常感激

+0

添加resultIntent .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);讓我知道它的工作與否 –

+0

但我應該在哪裏添加我希望按鈕所做的代碼? @ msh,nayan – spyder3anz

+0

Intent intent = new Intent(context,MainActivity.class);在此行後添加intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); 稍後您可以編寫Intent resultIntent = new Intent(context,MainActivity.class);在此行後添加resultIntent .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); –

回答

0

我想你想要在通知中添加自定義按鈕並且想要點擊它。

請嘗試以下代碼,如果你需要的一樣:

你必須使用RemoteViews這一點。

我創建了一個名爲notification_normal_view.xml的自定義佈局。

在我的notification_normal_view,我有一個TextView ietxtSnooze和點擊我想打開SnoozeActivity,如果我點擊通知的任何其他部分我想打開MainActivity。

因此,在您接收器:

// Using RemoteViews to bind custom layouts into Notification 
RemoteViews notificationView = new RemoteViews(context.getPackageName(), R.layout.notification_normal_view); 

Intent snoozeIntent = new Intent(context, SnoozeActivity.class); 
snoozeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
       | Intent.FLAG_ACTIVITY_CLEAR_TASK | Notification.FLAG_AUTO_CANCEL); 

PendingIntent pSnoozeIntent = PendingIntent.getBroadcast(context,NOTIFICATION_ID,snoozeIntent,PendingIntent.FLAG_UPDATE_CURRENT); 

Intent intent = new Intent(context, ExoVideoPlayer.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK | Notification.FLAG_AUTO_CANCEL); 
PendingIntent pIntent = PendingIntent.getActivity(context, NOTIFICATION_ID,intent, PendingIntent.FLAG_UPDATE_CURRENT); 

notificationView.setOnClickPendingIntent(R.id.txt_snooze, pSnoozeIntent); 

Notification notificationBuilder = new Notification.Builder(context) 
       .setSound(soundUri) 
       .setSmallIcon(icon) 
       .setAutoCancel(true) 
       .build(); 

//set your view to notification 
notificationBuilder.contentView = notificationView; 
notificationBuilder.flags = Notification.FLAG_AUTO_CANCEL; 
notificationBuilder.icon = R.mipmap.ic_launcher; 
notificationBuilder.contentIntent = pIntent; 

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

mNotificationManager.notify(NOTIFICATION_ID, notificationBuilder); 
+0

,您認爲它非常有幫助......最後一件事情..你知道我怎麼可以讓通知按鈕打盹鬧鐘嗎?你的代碼可以讓我在使用按鈕時發送一個待處理的意圖...我希望能夠點擊它並讓它像一個功能一樣......在我的情況下打盹鬧鐘@PatrickR – spyder3anz