通知按钮

时间:2017-05-28 22:41:32

标签: android notifications alarmmanager

所以我有一个警报应用程序...当警报响起时,我收到一个通知,上面有一个按钮,同时解除通知和警报声......另一个按钮也是如此,并调用另一个功能打盹应该将闹钟暂停5分钟......但是当点击暂停闹钟时,警报会立即再次触发......我不知道为什么

这是我的接收器

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();
}
}

不是5 * 60 * 1000应该是5分钟?为什么我会立刻得到它?

1 个答案:

答案 0 :(得分:0)

您必须以毫秒为单位输入纪元时间,因此您的贪睡方法应如下所示:

public void snooze(){ 
    Intent intent = new Intent(this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MINUTE, 5);
    Date date = calendar.getTime();
    alarmManager.set(AlarmManager.RTC_WAKEUP, date.getTime(), pendingIntent);
    }

如果这有帮助,请告诉我。