Bring the app to the foreground, turn on the display and unlock AlarmManager?

I want to turn on the display, unlock the phone and bring my application to the fore when the set alarm is activated.

public class CountDownAlarm extends BroadcastReceiver { public CountDownAlarm(){ } public CountDownAlarm(Context context, int timeoutInSeconds){ AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, CountDownAlarm.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); Calendar time = Calendar.getInstance(); time.setTimeInMillis(System.currentTimeMillis()); time.add(Calendar.SECOND, timeoutInSeconds); alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pendingIntent); } @Override public void onReceive(Context context, Intent intent) { PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP, "TRAININGCOUNTDOWN"); wl.acquire(); Intent i = new Intent(context, MyActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); context.startActivity(i); wl.release(); } } 

The vibrator from my CountDownTimer is activated, but the display does not turn on ...

 public class MyActivity extends Activity implements OnClickListener { @Override public void onClick(View arg0) { timer = new CountDownTimer(countDown*1000, 1000) { public void onTick(long millisUntilFinished) { activeBtn.setText(String.valueOf(millisUntilFinished / 60000) + ":" + String.format("%02d", (millisUntilFinished % 60000) / 1000)); } public void onFinish() { activeBtn.setText("0:00"); Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); v.vibrate(1000); ringtone = RingtoneManager.getRingtone(getApplicationContext(), RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); if (ringtone != null) { ringtone.play(); } new AlertDialog.Builder(MyActivity.this) .setMessage("Time up!") .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { } }).show(); } }.start(); new CountDownAlarm(this, countDown); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } 

On the side note, I want to play the โ€œPositiveโ€ sound. How to do it?

+5
android alarmmanager wakelock
source share
4 answers

You must acquire a wake lock using PowerManager.ACQUIRE_CAUSES_WAKEUP and PowerManager.FULL_WAKE_LOCK.

 WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TRAININGCOUNTDOWN"); 

Remember that if you release the lock after pressing the startActivity () button, the action may not start because it is an asynchronous call. I suggest using WakefulServiceIntent or PowerManager.WakeLock.acquire(long timeout)

+11
source share

In DescClock, this is done as follows:

  final Window win = getWindow(); win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); // Turn on the screen unless we are being launched from the AlarmAlert // subclass. if (!getIntent().getBooleanExtra(SCREEN_OFF, false)) { win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON); } 
+5
source share

Go to the action you want to run in onReceive (). Paste this into the onCreate () of this action

 final Window 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); 
+2
source share

As I can see, onReceive is called at pendingIntent interval. On my device, only the first onReceive call was acquired by WakeLock. If I press the suspend button, then the second call to wl.acquire () could not finish the system. I need a release (), followed by a get () function

 wl.release(); wl.acquire(); 
+1
source share

All Articles