在通知中添加具有不同功能的另一个按钮

时间:2017-05-28 21:03:23

标签: android notifications broadcastreceiver

所以我有一个警报应用程序...当警报响起时,我收到一个通知,上面有一个按钮,它会同时解除通知和警报声......我想在通知中添加另一个按钮相同的事情,但也发送一个未决的意图接收器5分钟后警报响起(基本上我想在5分钟内打盹警报)但它只是不工作..任何帮助woou; d非常感激

这是我的接收器

public class AlarmReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent arg1) {

    Toast.makeText(context, ALARM RECEIVED, Toast.LENGTH_LONG).show();
 createNotification(context);
 }   
    public void createNotification(Context context){

    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.message2))
                    .setSubText(context.getResources().getString(R.string.message))
                    .setPriority(NotificationCompat.PRIORITY_HIGH);

    Intent intent = new Intent(context,MainActivity.class);
    PendingIntent pendingIntent=PendingIntent.getActivity(context,123,intent,PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(pendingIntent);


    Intent dismiss_intent=new Intent(context, MediaPlayingService.class);
    dismiss_intent.setAction(MediaPlayingService.ACTION_DISMISS);
 Intent dismissing_intent=new Intent(context, MediaPlayingService.class);
    dismissing_intent.setAction(MediaPlayingService.DISMISSING_ACTION);

    PendingIntent pending=PendingIntent.getService(context,123,dismiss_intent,PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent pending2=PendingIntent.getService(context,123,dismissing_intent,PendingIntent.FLAG_UPDATE_CURRENT);

    PendingIntent pending=PendingIntent.getService(context,123,dismiss_intent,PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Action action= new NotificationCompat.Action(android.R.drawable.ic_lock_idle_alarm,
            "DISMISS",pending);
    mBuilder.addAction(action);
   NotificationCompat.Action action2= new NotificationCompat.Action(R.drawable.bell,"SNOOZE",pending);
    mBuilder.addAction(action2);


    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification=mBuilder.build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    mNotificationManager.notify(321,notification);
}

这是我的服务类

public class MediaPlayingService extends Service {
private MediaPlayer mp;
public static final String URI_BASE=MediaPlayingService.class.getName() + ".";
public static final String ACTION_DISMISS = URI_BASE + "ACTION_DISMISS";
 public static final String DISMISSING_ACTION = URI_BASE + "DISMISSING_ACTION";

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

public int onStartCommand(Intent intent, int flags, int startId) {


    String action = intent.getAction();
    if (ACTION_DISMISS.equals(action)) {
 dismiss();
 }else if(DISMISSING_ACTION.equals(action)){

        dismiss();
        snooze();
    }
           return START_NOT_STICKY;
    }


private void dismiss() {

    Intent intent=new Intent(this,MediaPlayingService.class);
    stopService(intent);

    intent = new Intent(this, AlarmReceiver.class);
    PendingIntent pendingIntent= PendingIntent.getBroadcast(this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager alarmM= (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmM.cancel(pendingIntent);

    NotificationManager notiMana= (NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
    notiMana.cancel(321);

}
  public void snooze(){ Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, 5*60*1000, pendingIntent);}

@Override
public void onDestroy() {
    super.onDestroy();
mp.stop();
}

}

我已经准备好了我的贪睡功能我只需要知道如何为第二个按钮调用dismiss()和snooze(),我只是不能......任何帮助都会非常感激

1 个答案:

答案 0 :(得分:0)

action2应该通过SNOOZE操作获得一个新的待处理意图。