- 我知道之前已经问过这个问题,他们的链接是:
Android: Alarm to be play every 30 minutes and it start from 12:30
Using Alarmmanager to start a service at specific time
Android: Alarm to be play every 30 minutes and it start from 12:30
- 现在,在我的情况下,我想要我的应用程序在一天中的任何时间安装,然后每隔30分钟开始响铃,但不是从安装时开始,而是按照以下示例的顺序...
例如
-->
我在上午11:15安装了应用程序。
-->
现在,当我的手机时钟敲响了上午11:30它应该开始振铃,然后再次在中午12:00它应该响,然后在下午12:30等等......
- 现在我setRepeating
AlarmManager
的{{1}}没有解雇,或者即使解雇它也不行......
- 我尝试使用Threads within Service
,但有效,但整个过程变得沉重,几乎无法使用。
这是我的代码...........
TestService.java
//继续运行的服务。
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.concurrent.TimeUnit;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.widget.Toast;
public class TestService extends Service {
/*
* public static SharedPreferences myPrefs; public static
* SharedPreferences.Editor prefsEditor;
*/
private PendingIntent pendingIntentFri;
private boolean ENABLE;
private boolean isDone = true;
Intent myIntent;
@Override
public void onCreate() {
// TODO Auto-generated method stub
// Toast.makeText(this, "MyAlarmService.onCreate()",
// Toast.LENGTH_LONG).show();
// Toast.makeText(this, "Test.onCreate()", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
// Toast.makeText(this, "Test.onBind()", Toast.LENGTH_LONG).show();
return null;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "Test.onDestroy()", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startId) {
System.out
.println("I reached TEST SERVICE.........................................................");
// TODO Auto-generated method stub
super.onStart(intent, startId);
// Toast.makeText(this, "Test.onStart()", Toast.LENGTH_LONG).show();
/*
* Intent myIntent = new Intent(TestService.this, MyAlarmService.class);
* pendingIntent = PendingIntent.getService(TestService.this, 0,
* myIntent, 0);
*/
// myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
ENABLE = ClockActivity.myPrefs.getBoolean("enable", false);
System.out.println("ALARM SERVICE RECREATED WITH THE VALUE :::::: "
+ ENABLE);
System.out
.println("FROM TESTSERVICE..........................................:"
+ ENABLE);
if (ENABLE) {
System.out
.println("FROM IF ENABLED BLOCK TESTSERVICE..........................................:"
+ ENABLE);
startAlarm();
} else {
System.out.println("FROM ELSE ITS NOT ENABLED: " + ENABLE);
}
}
public void startAlarm() {
// AlarmManager alarmManager =
// (AlarmManager)getSystemService(ALARM_SERVICE);
myIntent = new Intent(TestService.this, MyAlarmService.class);
// pendingIntent = PendingIntent.getService(SettingsActivity.this, 0,
// myIntent, 0);
// Working for the Perfect Time...
Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());// set the current
// time and date for
// this calendar
final Calendar cal = new GregorianCalendar();
// cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR, cur_cal.get(Calendar.HOUR));
cal.set(Calendar.MINUTE, cur_cal.get(Calendar.MINUTE));
cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
// cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, Calendar.DATE);
cal.set(Calendar.MONTH, Calendar.MONTH);
Intent myIntentFri = new Intent(TestService.this, MyAlarmService.class);
pendingIntentFri = PendingIntent.getService(TestService.this, 0,
myIntentFri, 0);
final AlarmManager alarmManagerFri = (AlarmManager) getSystemService(ALARM_SERVICE);
// alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
// pendingIntent2);
alarmManagerFri.setRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), (30 * 60) * 1000, pendingIntentFri);// for
// 30
// minutes
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
// Toast.makeText(this, "MyAlarmService.onUnbind()",
// Toast.LENGTH_LONG).show();
return super.onUnbind(intent);
}
}
MyAlarmService.java
//包含逻辑的文件
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.os.SystemClock;
import android.sax.EndTextElementListener;
import android.widget.Toast;
public class MyAlarmService extends Service {
public static SharedPreferences myPrefs;
public static SharedPreferences.Editor prefsEditor;
private boolean SILENCE;
@Override
public void onCreate() {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
System.out
.println("I AM IN MYALARMSERVICE..............................................");
Calendar ct = Calendar.getInstance();
ct.setTimeInMillis(System.currentTimeMillis());
int Hour = ct.get(Calendar.HOUR_OF_DAY);
int Minute = ct.get(Calendar.MINUTE);
System.out.println("Hour and Minute in Calender is: " + Hour + ":"
+ Minute);
myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
SILENCE = myPrefs.getBoolean("silence", false);
int sHour = myPrefs.getInt("start_hour", 01);
int sMinute = myPrefs.getInt("start_minute", 00);
int eHour = myPrefs.getInt("end_hour", 02);
int eMinute = myPrefs.getInt("end_minute", 00);
System.out.println("======== SILENCE PERIOD ==========");
System.out.println("The Start Time is: " + sHour + ":" + sMinute);
System.out.println("The END Time is: " + eHour + ":" + eMinute);
System.out.println("==================================");
Calendar currentTime = new GregorianCalendar();
// currentTime.set(Calendar.HOUR_OF_DAY, Calendar.HOUR_OF_DAY);
// currentTime.set(Calendar.MINUTE, Calendar.MINUTE);
// currentTime.set(Calendar.SECOND, Calendar.SECOND);
// currentTime.set(Calendar.DATE, Calendar.DATE);
// currentTime.set(Calendar.MONTH, Calendar.MONTH);
Calendar start_time = new GregorianCalendar();
start_time.set(Calendar.HOUR_OF_DAY, sHour);
start_time.set(Calendar.MINUTE, sMinute);
// start_time.set(Calendar.SECOND, Calendar.SECOND);
// start_time.set(Calendar.DATE, Calendar.DATE);
// start_time.set(Calendar.MONTH, Calendar.MONTH);
Calendar end_time = new GregorianCalendar();
end_time.set(Calendar.HOUR_OF_DAY, eHour);
end_time.set(Calendar.MINUTE, eMinute);
// if((sHour>eHour)){ // if the Start == PM and End == AM
// System.out.println("Day is increase. . . .");
// end_time.add(Calendar.DATE, 1);
// // end_time.set(Calendar.SECOND, Calendar.SECOND);
// // end_time.set(Calendar.DATE, Calendar.DATE);
// // end_time.set(Calendar.MONTH, Calendar.MONTH);
// }else{ // if the Start == PM and End == AM
//
// System.out.println("Day is same. . . .");
//
// // end_time.set(Calendar.SECOND, Calendar.SECOND);
// // end_time.set(Calendar.DATE, Calendar.DATE);
// // end_time.set(Calendar.MONTH, Calendar.MONTH);
// }
System.out
.println("=============== CURRENT TIME DETAIL ===============");
System.out.println("CURRENT TIME: " + currentTime.getTime());
System.out
.println("CURRENT DATE: " + currentTime.getTime().getDate());
System.out.println("CURRENT HOUR: "
+ currentTime.getTime().getHours());
System.out
.println("===================================================");
System.out.println("");
System.out.println("");
System.out.println("=============== START TIME DETAIL ===============");
System.out.println("START DATE: " + start_time.getTime().getDate());
System.out.println("START TIME: " + start_time.getTime());
System.out.println("START HOUR: " + start_time.getTime().getHours());
System.out
.println("===================================================");
System.out.println("");
System.out.println("");
System.out.println("=============== END TIME DETAIL ===============");
System.out.println("END TIME: " + end_time.getTime());
System.out.println("END DATE: " + end_time.getTime().getDate());
System.out.println("END HOUR: " + end_time.getTime().getHours());
System.out
.println("===================================================");
System.out.println("");
System.out.println("");
System.out.println(start_time.getTimeInMillis() + " < "
+ System.currentTimeMillis() + " && "
+ System.currentTimeMillis() + " > "
+ end_time.getTimeInMillis());
if (!SILENCE) {
System.out.println("SILENCE in if is :" + SILENCE);
fireAlarm(Hour, Minute);
} else {
System.out.println("SILENCE in else is: " + SILENCE);
if (sHour > eHour) { // FOR START == PM and END == AM
System.out.println("FOR start=PM AND end=AM");
if (((((start_time.getTimeInMillis() <= System
.currentTimeMillis())) || ((System.currentTimeMillis() <= (end_time
.getTimeInMillis()) + ((3 * 60) * 1000)))))) {
System.out.println("You are in Silent mode");
Toast.makeText(getApplicationContext(),
"Ship's Clock is Silent", Toast.LENGTH_SHORT)
.show();
} else {
fireAlarm(Hour, Minute);
}
} else { // FOR START == AM/PM and END == AM/PM
System.out.println("FOR start=AM/PM AND end=AM/PM");
if (((((start_time.getTimeInMillis() <= System
.currentTimeMillis())) && ((System.currentTimeMillis() <= (end_time
.getTimeInMillis()) + ((3 * 60) * 1000)))))) {
System.out.println("You are in Silent mode");
Toast.makeText(getApplicationContext(),
"Ship's Clock is Silent", Toast.LENGTH_SHORT)
.show();
} else {
fireAlarm(Hour, Minute);
}
}
}
}
public void fireAlarm(int Hour, int Minute) {
// if(Hour>12){
// Toast.makeText(getApplicationContext(),
// "Please Set the More the 12 Hour", Toast.LENGTH_SHORT).show();
// }
// 12:30
if ((Hour == 12) && (Minute == 30)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("ONE Bell");
playSound(R.raw.one_bell);
}
// 01:00
if ((Hour == 1) && (Minute == 00)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("TWO Bell");
playSound(R.raw.two_bells);
}
// 01:30
if ((Hour == 1) && (Minute == 30)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("THREE Bell");
playSound(R.raw.three_bells);
}
// 02:00
if ((Hour == 2) && (Minute == 00)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("FOUR Bell");
playSound(R.raw.four_bells);
}
// 02:30
if ((Hour == 2) && (Minute == 30)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("FIVE Bell");
playSound(R.raw.five_bells);
}
// 03:00
if ((Hour == 3) && (Minute == 00)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("SIX Bell");
playSound(R.raw.six_bells);
}
// 03:30
if ((Hour == 3) && (Minute == 30)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("SEVEN Bell");
playSound(R.raw.seven_bells);
}
// 04:00
if ((Hour == 4) && (Minute == 00)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("EIGHT Bell");
playSound(R.raw.eight_bells);
}
// 04:30
if ((Hour == 4) && (Minute == 30)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("ONE Bell");
playSound(R.raw.one_bell);
}
// 05:00
if ((Hour == 5) && (Minute == 00)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("TWO Bell");
playSound(R.raw.two_bells);
}
// 05:30
if ((Hour == 5) && (Minute == 30)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("THREE Bell");
playSound(R.raw.three_bells);
}
// 06:00
if ((Hour == 6) && (Minute == 00)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("FOUR Bell");
playSound(R.raw.four_bells);
}
// 06:30
if ((Hour == 6) && (Minute == 30)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("FIVE Bell");
playSound(R.raw.five_bells);
}
// 07:00
if ((Hour == 7) && (Minute == 00)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("SIX Bell");
playSound(R.raw.six_bells);
}
// 07:30
if ((Hour == 7) && (Minute == 30)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("SEVEN Bell");
playSound(R.raw.seven_bells);
}
// 08:00
if ((Hour == 8) && (Minute == 00)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("EIGHT Bell");
playSound(R.raw.eight_bells);
}
// 08:30
if ((Hour == 8) && (Minute == 30)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("ONE Bell");
playSound(R.raw.one_bell);
}
// 09:00
if ((Hour == 9) && (Minute == 00)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("TWO Bell");
playSound(R.raw.two_bells);
}
// 09:30
if ((Hour == 9) && (Minute == 30)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("THREE Bell");
playSound(R.raw.three_bells);
}
// 10:00
if ((Hour == 10) && (Minute == 00)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("FOUR Bell");
playSound(R.raw.four_bells);
}
// 10:30
if ((Hour == 10) && (Minute == 30)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("FIVE Bell");
playSound(R.raw.five_bells);
}
// 11:00
if ((Hour == 11) && (Minute == 00)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("SIX Bell");
playSound(R.raw.six_bells);
}
// 11:30
if ((Hour == 11) && (Minute == 30)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("SEVEN Bell");
playSound(R.raw.seven_bells);
}
// 12:00
if ((Hour == 12) && (Minute == 00)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("Eight Bell");
playSound(R.raw.eight_bells);
}
// 00:00
if ((Hour == 00) && (Minute == 00)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("Eight Bell");
playSound(R.raw.eight_bells);
}
// 00:30
if ((Hour == 00) && (Minute == 30)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("One Bell");
playSound(R.raw.one_bell);
}
// //////////////////// FOR 24 HOUR
// 13:00
if ((Hour == 13) && (Minute == 00)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("Two Bell");
playSound(R.raw.two_bells);
}
// 13:30
if ((Hour == 13) && (Minute == 30)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("Three Bell");
playSound(R.raw.three_bells);
}
// 14:00
if ((Hour == 14) && (Minute == 00)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("Four Bell");
playSound(R.raw.four_bells);
}
// 14:30
if ((Hour == 14) && (Minute == 30)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("Five Bell");
playSound(R.raw.five_bells);
}
// 15:00
if ((Hour == 15) && (Minute == 00)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("Six Bell");
playSound(R.raw.six_bells);
}
// 15:30
if ((Hour == 15) && (Minute == 30)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("Seven Bell");
playSound(R.raw.seven_bells);
}
// 16:00
if ((Hour == 16) && (Minute == 00)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("Eight Bell");
playSound(R.raw.eight_bells);
}
// 16:30
if ((Hour == 16) && (Minute == 30)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("One Bell");
playSound(R.raw.one_bell);
}
// 17:00
if ((Hour == 17) && (Minute == 00)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("Two Bell");
playSound(R.raw.two_bells);
}
// 17:30
if ((Hour == 17) && (Minute == 30)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("Three Bell");
playSound(R.raw.three_bells);
}
// 18:00
if ((Hour == 18) && (Minute == 00)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("Four Bell");
playSound(R.raw.four_bells);
}
// 18:30
if ((Hour == 18) && (Minute == 30)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("Five Bell");
playSound(R.raw.five_bells);
}
// 19:00
if ((Hour == 19) && (Minute == 00)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("Six Bell");
playSound(R.raw.six_bells);
}
// 19:30
if ((Hour == 19) && (Minute == 30)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("Seven Bell");
playSound(R.raw.seven_bells);
}
// 20:00
if ((Hour == 20) && (Minute == 00)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("Eight Bell");
playSound(R.raw.eight_bells);
}
// 20:30
if ((Hour == 20) && (Minute == 30)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("One Bell");
playSound(R.raw.one_bell);
}
// 21:00
if ((Hour == 21) && (Minute == 00)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("two Bell");
playSound(R.raw.two_bells);
}
// 21:30
if ((Hour == 21) && (Minute == 30)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("Three Bell");
playSound(R.raw.three_bells);
}
// 22:00
if ((Hour == 22) && (Minute == 00)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("Four Bell");
playSound(R.raw.four_bells);
}
// 22:30
if ((Hour == 22) && (Minute == 30)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("Five Bell");
playSound(R.raw.five_bells);
}
// 23:00
if ((Hour == 23) && (Minute == 00)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("Six Bell");
playSound(R.raw.six_bells);
}
// 23:30
if ((Hour == 23) && (Minute == 30)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("Seven Bell");
playSound(R.raw.seven_bells);
}
// 24:00
if ((Hour == 24) && (Minute == 00)) {
System.out.println("The Time is: " + Hour + " : " + Minute);
System.out.println("Eight Bell");
playSound(R.raw.eight_bells);
}
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
// Toast.makeText(this, "MyAlarmService.onUnbind()",
// Toast.LENGTH_LONG).show();
return super.onUnbind(intent);
}
public void playSound(int resources) {
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), resources);
mp.start();
}
}
答案 0 :(得分:2)
嗯,这很糟糕:
cal.set(Calendar.DATE, Calendar.DATE); <-- This sets the day of the month to 5!
cal.set(Calendar.MONTH, Calendar.MONTH); <-- This sets the month to 2 (March)!
你想在这做什么?
为什么不用您想要的参数创建一个日历,如下所示:
Calendar cal = new GregorianCalendar(); // Current date and time
cal.set(Calendar.MILLISECOND, 0);
cal.set(Calendar.SECOND, 0);
// Round minutes up to next multiple of 30
int minutes = cal.get(Calendar.MINUTE);
if (minutes < 30) {
cal.set(Calendar.MINUTE, 30);
} else {
cal.set(Calendar.MINUTE, 0);
cal.add(Calendar.HOUR, 1); // Next hour (will roll the day/month/year over if needed)
}
答案 1 :(得分:1)
另一种方法是花费当前时间并在ms中添加30分钟:
30 mins (30 mins to ms = 60s * 1000ms * 30) = 1800000 ms
long Offset = Calendar.getInstance().getTimeInMillis() + 1800000;
您应该打印为 DURING 调试设置闹钟的时间,以确保它在正确的时间设置为关闭。
在Calendar Instance上使用getTime()方法,然后在本地时间打印它。