7

我有一个BroadcastReceiverActivity. 如果在Activity屏幕打开时启动,它会显示并且一切都很好。但是,在 ICS 和 JB 设备上(我尚未测试 GB 或 HC,但 Froyo 不存在该问题)如果在Activity屏幕关闭时启动,则不会禁用锁屏,并且当手机已解锁(通过手动解锁或使用我为后 Froyo 设备输入的代码解锁)。

为什么,至少在 ICS 和 JB 设备上,如果没有下面提到的代码,锁屏不会被禁用,为什么在Activity启动时屏幕关闭时活动不显示?

这是代码:

BroadcastReceiver

Intent alarmAlert = new Intent(context, AlarmGoneOffActivity.class);
alarmAlert.putExtra(MyAlarmManager.ALARM_NUM_ID, alarm.ID);
alarmAlert.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
                    Intent.FLAG_ACTIVITY_NO_USER_ACTION);
context.startActivity(alarmAlert);

AlarmGoneOffActivity.onCreate()

setContentView(R.layout.alarm_gone_off);

final Window win = getWindow();
win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | 
             WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | 
             WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | 
             WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
             WindowManager.LayoutParams.FLAG_FULLSCREEN | 
             WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | 
             WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | 
             WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}

//so far all of my post froyo devices (ICS and JB no more GB)
// don't bypass the lockscreen unless we use this
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) {
    KeyguardManager  myKeyGuard = 
                    (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
    KeyguardLock myLock = myKeyGuard.newKeyguardLock("ShabbosAlarm");
    myLock.disableKeyguard();
}

编辑:我真的不想使用KeyguardLock.disableKeyguard(),因为它会导致键盘保护被禁用,直到KeyguardLock.reenableKeyguard()不方便为止。有什么解决办法吗?

Edit2:我现在可以确认该问题仅存在于 ICS 及更高版本上。是否进行了某些更改以防止键盘锁被禁用?即使有,为什么Activity手动解锁屏幕时我的不显示?

4

2 回答 2

3

这是我用来实现此目的的基本实现。它适用于 2.3-4.3 的用户。我敢肯定,您可以从这里开始构建它。我创建了这个非常短暂的活动,只是为了处理这个事件 - 如果需要,您可以将它用作帮助类:

import android.app.Activity;
import android.os.Bundle;
import android.view.WindowManager;

public class BlankActivity extends Activity {

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                        | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
                WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                        | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);


        // Your code here - Or if it doesn't trigger, see below
    }

    @Override
    public void onAttachedToWindow() {

        // You may need to execute your code here

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {

        }
    }

    @Override
    public void onPause() {
        super.onPause();

        finish();
    }
}

在清单中:

    <activity
        android:name="com.your.package.BlankActivity"
        android:clearTaskOnLaunch="true"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:excludeFromRecents="true"
        android:launchMode="singleInstance"
        android:noHistory="true"
        android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" >
    </activity>

与其他相关帖子相反,半透明主题没有引起问题,也不需要将布局添加到 Activity。

希望它也适合你。

于 2013-08-22T16:49:56.630 回答
0

这段代码对我有用。我把它放在 OnCreate 活动中

private fun wakeScreen() {
    val window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON or
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD or
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)
}

在活动清单上,我把这个

<activity android:name=".Alarm.AlarmNotifActivity"
        android:showOnLockScreen="true"
        android:showWhenLocked="true"
        android:launchMode="singleInstance"/>

希望它会有所帮助。

于 2020-03-01T15:28:42.313 回答