当屏幕被唤醒并且锁定屏幕未禁用时,Android活动不显示

时间:2012-10-19 17:49:49

标签: android android-activity android-screen android-windowmanager

我有BroadcastReceiver开始Activity。如果在屏幕打开时启动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在屏幕被手动解锁时没有显示?

2 个答案:

答案 0 :(得分:3)

以下是我用来实现此目的的基本实现。它适用于2.3-4.3的用户。你可以从这里建立起来我很有信心。我创建了这个非常短暂的Activity,只是为了处理这个事件 - 如果需要,你可以将它用作帮助类:

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>

与其他相关帖子相反,半透明主题不会导致问题,也不需要将布局添加到活动中。

希望它也适合你。

答案 1 :(得分: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"/>

希望这会有所帮助。