警报不在同一日期触发 [英] Alarm is not triggering on the same Date
问题描述
我想在特定时间重启设备,所以我为此使用了警报管理器。以下是我的活动代码。
I want to reboot device on particular time so i am using alarm manager for that.below is the code of my activity.
public class MainActivity extends AppCompatActivity {
private static int timeHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
private static int timeMinute = Calendar.getInstance().get(Calendar.MINUTE);
AlarmManager alarmManager;
private PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 11);
calendar.set(Calendar.MINUTE, 02);
alarmManager.cancel(pendingIntent);
// if(Calendar.getInstance().after(calendar)){
// // Move to tomorrow
// calendar.add(Calendar.DATE, 1);
// }
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);
//
// alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
// SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY,
// AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
这是我的接收者
public class AlarmReceiver extends BroadcastReceiver {
public static void rebootDevice() {
try {
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("reboot \n");
} catch (Throwable t) {
t.printStackTrace();
}
}
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Triggered", Toast.LENGTH_SHORT).show();
Log.d("Gajanand", "onReceive: Triggered");
rebootDevice();
}
}
代码可以正常工作,但不能正常工作在确切的日期。例如,如果我现在运行相同的代码。如果我更改触发日期,警报不会触发。我没有收到代码的问题,触发警报有10秒的延迟。任何帮助
Yes code is working fine but not on the exact date.for example if i run the same code now. alarm not triggers if i change the date it triggers. i am not getting what is the problem with code and there is 10 seconds delay in triggering alarm. any help
推荐答案
您的代码有两个问题:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 11);
calendar.set(Calendar.MINUTE, 02);
此处您要设置 HOUR_OF_DAY
和 MINUTE
,但 SECOND
字段仍具有其初始值,因此不会在$ b分钟一刻触发警报$ b(除非您确切地在分钟内调用了 calendar.setTimeInMillis(System.currentTimeMillis())
,这不太可能。)
Here you're setting HOUR_OF_DAY
and MINUTE
but the SECOND
field still has its initial value, so the alarm won't be triggered exactly on the minute
(unless you called calendar.setTimeInMillis(System.currentTimeMillis())
exactly on the minute, which is quite unlikely).
应该是这样的:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 11);
calendar.set(Calendar.MINUTE, 2);
calendar.set(Calendar.SECOND, 0); // setting seconds to 0
setRepeating()
不精确,无法与Doze配合使用。
setRepeating()
is inexact and doesn't work with Doze.
要获取确切的触发时间,您应该使用确切的警报,请检查示例。
For exact trigger times you should use exact alarms, check this answer for an example.
这篇关于警报不在同一日期触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!