我正在使用警报管理器发送一个消息,但警报没有正常工作。如果时间是18小时,我设置了8小时的警报,那么它会在那时发送消息。 这是代码。
public class Time_Picker extends AppCompatActivity {
TimePicker time_picker;
String tag="pritesh";
static AlarmManager[] alarmManager=new AlarmManager[5];
static PendingIntent[] pendingIntent = new PendingIntent[5];
EditText message;
EditText phone;
Intent myIntent;
// AlarmManager alarmManager;
//PendingIntent pendingIntent;
public static String Phone;
public static String Message;
public int i;
array ob=new array();
static ArrayList<PendingIntent> pendingarray=new ArrayList<>(5);
public void setAlarm(View view) {
// AlarmManager alarmManager[]=new AlarmManager[5];
// if(prevention.timer==0) {
Phone = phone.getText().toString();
Message = message.getText().toString();
phone.setText("");
message.setText("");
//Validating if any field empty
if (Phone.length() >= 10 && Message.length() > 0) {
Toast.makeText(this.getApplicationContext(), "Your sms will be sent soon", Toast.LENGTH_SHORT).show();
//Getting Calender Reference
Calendar cal = Calendar.getInstance();
int currentApiVersion = android.os.Build.VERSION.SDK_INT;
if (currentApiVersion > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
cal.set(Calendar.MINUTE, time_picker.getMinute());
cal.set(Calendar.HOUR_OF_DAY, time_picker.getHour());
} else {
//Setting the date and time from the time picker
cal.set(Calendar.MINUTE, time_picker.getCurrentMinute());
cal.set(Calendar.HOUR_OF_DAY, time_picker.getCurrentHour());
}
int a = (Calendar.getInstance().get(Calendar.SECOND) * 1000);
i=array.select;
myIntent = new Intent(this, MyReceiver.class);
myIntent.putExtra("pos",array.select);
//Pending Intent for sending the intent afterwards
pendingIntent[array.select] = PendingIntent.getBroadcast(this.getApplicationContext(), array.select, myIntent, 0);
alarmManager[array.select] = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE));
alarmManager[array.select].set(AlarmManager.RTC, cal.getTimeInMillis() - a, pendingIntent[array.select]);
pendingarray.add(pendingIntent[array.select]);
sms_list.Phone[array.select]=Phone;
Intent back = new Intent(this, sms_list.class);
back.putExtra("PHONE", Phone);
back.putExtra("Flag",2);
back.putExtra("MSG", Message);
back.putExtra("HOUR", (int) cal.get(Calendar.HOUR_OF_DAY));
back.putExtra("MIN", (int) cal.get(Calendar.MINUTE));
startActivity(back);
//int b;
}
else
{
Toast.makeText(this.getApplicationContext(), "Please Fill Up All The Details", Toast.LENGTH_SHORT).show();
array.a[array.select]=false;
ob.sort();
}
}
}
代码,即使我设置的时间差别很大也不起作用。
我可以使用12时钟设置闹钟吗?
答案 0 :(得分:-1)
您无法将闹钟设置为已经过去的时间。
alarmManager[array.select].set(AlarmManager.RTC, cal.getTimeInMillis() - a, pendingIntent[array.select]);
尝试将来的时间如下。
alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() +
60 * 1000, alarmIntent);