2012-01-19 13 views
2

私はその日の特定の時刻にアラームを設定しようとしています:例えば:20:15 これは私と一緒に作業していても消えないコードです午前20時15分今日の特定の時刻にアラームを設定する

 Intent intent = new Intent(AlarmActivity.this, MyBroadcastReceiver.class); 
     intent.putExtra("Hekma", "One better than none"); 
     PendingIntent pintent = PendingIntent.getService(AlarmActivity.this, 0,intent, 0); 
     AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE); 
     Calendar cal = Calendar.getInstance(); 



     cal.set(Calendar.HOUR_OF_DAY, 20); 
     cal.set(Calendar.MINUTE, 15); 
     cal.set(Calendar.SECOND, 0); 
     cal.set(Calendar.MILLISECOND, 0); 
     alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pintent); 

答えて

0

でこれは、2つの時刻の差を取得する必要があり、したがって、あなたはそれがオフに行きたい時に前に行くためにどのくらい

alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis() - System.currentTimeMillis(), pintent); 

を試してみてください。したがって、あなたはそれが消えたい時に消えるべきです。

+0

はそれが何もちょうどアラームの私が持っている私のコードに再びそれを比較し、[OK]を –

+0

が起こっていない動作しませんでしたこれを試してみました。私が見ることができる唯一の他の違いは、 "PendingIntent.getService(....;")があるということです。 –

+0

私はPendingIntent.getBroadcast(... –

4

私のプロジェクトでAlarmManagerを使用していますが、それは完全に機能します。それはあなたを助けることがあり、これを試してみてください:

Intent myIntent = new Intent(MainActivity.this, MyAlarmService.class); 
pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0); 
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

Calendar calendar = Calendar.getInstance(); 
calendar.setTimeInMillis(System.currentTimeMillis()); 
calendar.set(Calendar.HOUR_OF_DAY, 23); 
calendar.set(Calendar.MINUTE, 59); 
calendar.set(Calendar.SECOND, 0); 
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 
関連する問題