将应用程序带到前面,打开显示屏并从AlarmManager解锁?

时间:2011-12-20 08:11:30

标签: android alarmmanager wakelock

当我设置的闹钟激活时,我想打开显示屏,解锁手机并将我的应用程序带到前面。

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(); 
    }
}

我的CountDownTimer振动器已激活,但显示屏无法开启...

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);
    }
}

另一方面,我想播放“正面”警报声。我该怎么做?

4 个答案:

答案 0 :(得分:11)

您应该使用PowerManager.ACQUIRE_CAUSES_WAKEUP和PowerManager.FULL_WAKE_LOCK获取唤醒锁定。

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

请记住,如果在调用startActivity()之后立即释放唤醒锁定,则活动可能无法启动,因为它是异步调用。我建议使用WakefulServiceIntentPowerManager.WakeLock.acquire(long timeout)

答案 1 :(得分:6)

在DescClock中,它按以下方式完成:

    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);
    }

答案 2 :(得分:2)

转到要在 onReceive()中启动的活动。将其粘贴到该活动的 onCreate()

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);

答案 3 :(得分:1)

正如我所看到的,onReceive被调用pendingIntent间隔。 在我的设备上,只有第一次调用onReceive才能获得WakeLock。 如果我在此期间按下暂停按钮,wl.acquire()的第二次调用无法启动系统。我需要一个release(),然后是acquire()

wl.release();
wl.acquire();