0
所以我有一個鬧鐘應用程序......當鬧鐘響起時,我收到一個通知,其中包含通知和鬧鐘聲音的按鈕...另一個按鈕的功能也一樣並調用另一個函數打盹這是應該暫停鬧鐘5分鐘......但在打盹,當點擊報警觸發再次右鍵away..i不知道爲什麼通知按鈕中的特定貪睡功能
這是我的接收機
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分鐘?爲什麼我馬上得到它?