【问题标题】:Bring app to front, turn on display and unlock from AlarmManager?将应用程序放在前面,打开显示并从 AlarmManager 解锁?
【发布时间】:2011-12-20 08:11:29
【问题描述】:

当我设置的闹钟启动时,我想打开显示屏、解锁手机并将我的应用程序放在前面。

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


    【解决方案1】:

    您应该使用 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)

    【讨论】:

    • 谢谢!现在显示屏打开了,但我该如何解锁屏幕呢?
    • 你可以尝试解除键锁stackoverflow.com/questions/4352548/…
    • @bart,你能解释一下如何走 PowerManager.WakeLock.acquire(long timeout) 路线吗?还是有点迷茫。
    • 嗨@bart,我想在锁定屏幕上显示我的活动视图,我必须在调用我的活动时从backgroundReciever打开显示器......但它只在屏幕解锁时出现...... ..我根本不想解锁屏幕,但是希​​望用户采取行动,例如来电,viber也可以......
    • PowerManager.FULL_WAKE_LOCK 已弃用,请考虑改用 WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
    【解决方案2】:

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

    【讨论】:

    • 这很棒! FLAG_KEEP_SCREEN_ON 将保持屏幕亮着,直到当前窗口对用户可见。调用完成();以编程方式或允许用户手动完成以恢复正常模式。希望这对某人有所帮助。更多信息developer.android.com/reference/android/view/…
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      正如我所见,onReceive 是用 pendingIntent 间隔调用的。 在我的设备上,只有第一次调用 onReceive 是获取 WakeLock。 如果我同时按下暂停按钮,则 wl.acquire() 的第二次调用无法启动系统。我需要一个 release() 先跟一个 acquire()

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-27
        相关资源
        最近更新 更多