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

问题描述 投票:6回答:4

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

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

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

android alarmmanager wakelock
4个回答
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)


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
投票

转到要在onReceive()中启动的Activity。将其粘贴到该Activity的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);

1
投票

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

wl.release();
wl.acquire();
© www.soinside.com 2019 - 2024. All rights reserved.