即使设备正在睡眠,AlarmManager.RTC也会运行

anu*_*nuj 3 android

设备未插入电缆进行充电设备在15秒不活动后设置为休眠状态

奇怪的结果是当我将i设置为60时,振动器每60秒运行一次,这是我的警报代码.

public void startAlert(View view) {
        EditText text = (EditText) findViewById(R.id.time);
        int i = Integer.parseInt(text.getText().toString());
        Intent intent = new Intent(this, MyBroadcastReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                this.getApplicationContext(), 234324243, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), i*1000, pendingIntent);

        Toast.makeText(this, "Alarm set in " + i + " seconds",
                Toast.LENGTH_LONG).show();
    }
Run Code Online (Sandbox Code Playgroud)

BroadcastReceiver

public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Time is up!!!!.",
                Toast.LENGTH_LONG).show();
        // Vibrate the mobile phone
        Vibrator vibrator = (Vibrator) context
                .getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(1000);
    }
Run Code Online (Sandbox Code Playgroud)

Lov*_*vis 5

屏幕关闭让你感到困惑.您无法告诉设备何时进入深度睡眠模式.

只要处理器处于活动状态,RTC就可以正常工作.如果设备进入睡眠状态(在不活动10-30分钟后,取决于设备),您将需要RTC_WAKEUP.

因此,在您的情况下,您可能希望在屏幕关闭时取消警报.请参阅如何判断Android中的屏幕是否显示?了解更多信息.

更新

试一试:播放一些音乐并等待15秒.它会停止吗?没有. - >设备没有睡觉.

您无法设置设备进入休眠状态的时间,因为系统会决定.它实际上是"显示睡眠".想象一下,你从Play商店下载了一个apk.下载是否应该因为你告诉设备在X秒后"休眠"而停止?当然不是.

菜单中的设置称为"睡眠",因为普通用户理解它.可通过此方式访问此设置Settings.System.putInt(contentResolver, Settings.System.SCREEN_OFF_TIMEOUT, <int>).