因此,我有一個鬧鐘應用程序...當接收器從報警類獲取意圖,它會創建一個通知,並建立它..但我似乎無法弄清楚如何到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());
}
任何幫助將非常感激
添加resultIntent .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);讓我知道它的工作與否 –
但我應該在哪裏添加我希望按鈕所做的代碼? @ msh,nayan – spyder3anz
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); –