Chcę, aby aktywność była wyświetlana o określonej godzinie. Do tego używam programu AlarmManager. Działa dobrze, gdy urządzenie nie śpi, ale nie budzi go, jeśli śpi.Android AlarmManager nie budzi telefonu
Mój kod do ustawiania alarmu:
Calendar alarmTime = Calendar.getInstance();
alarmTime.set(Calendar.HOUR_OF_DAY, alarm.hour);
alarmTime.set(Calendar.MINUTE, alarm.minute);
alarmTime.set(Calendar.SECOND, 0);
if (alarmTime.before(now))
alarmTime.add(Calendar.DAY_OF_MONTH, 1);
Intent intent = new Intent(ctxt, AlarmReceiver.class);
intent.putExtra("alarm", alarm);
PendingIntent sender = PendingIntent.getBroadcast(ctxt, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(), sender);
Mój odbiornik transmisji:
@Override
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
final Alarm alarm = (Alarm) bundle.getSerializable("alarm");
Intent newIntent;
if (alarm.type.equals("regular")) {
newIntent = new Intent(context, RegularAlarmActivity.class);
} else if (alarm.type.equals("password")) {
newIntent = new Intent(context, PasswordAlarmActivity.class);
} else if (alarm.type.equals("movement")) {
newIntent = new Intent(context, MovementAlarmActivity.class);
} else {
throw new Exception("Unknown alarm type");
}
newIntent.putExtra("alarm", alarm);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(newIntent);
} catch (Exception e) {
Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
Log.e("AlarmReceiver", Log.getStackTraceString(e));
}
}
Ten kod nie obudzić urządzenie. Jednak po ponownym włączeniu są wyświetlane. Muszę sprawić, żeby włączali ekran. Czy możesz mi pomóc w tym problemie?
iść do działalności, które chcesz rozpocząć w onReceive(). Wklej to w onCreate() tego działania final Okno win = getWindow(); win.addFlags (WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); win.addFlags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); – Junaid