Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

android - Bring app to front, turn on display and unlock from AlarmManager?

I want to turn on the display, unlock the phone and bring my app to the front, when the alarm I've set activates.

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 doesn't 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's 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 a side note, I want to play the "Positive" alarm sound. How do I do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should acquire wake lock with PowerManager.ACQUIRE_CAUSES_WAKEUP and PowerManager.FULL_WAKE_LOCK.

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

Bear also in mind that if you release wake lock right after startActivity() is called, the activity might not start because it is asynchronous call. I suggest to use WakefulServiceIntent or PowerManager.WakeLock.acquire(long timeout)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...