7

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

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

4 回答 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)

于 2011-12-20T08:27:07.087 回答
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);
    }
于 2011-12-20T16:04:34.460 回答
3

转到要在onReceive()中启动的 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);
于 2015-07-30T14:00:45.200 回答
1

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

wl.release();
wl.acquire();
于 2012-06-05T15:56:51.710 回答