我想锁定屏幕。我想禁用主钥匙,只使用后键。我该如何完成?

有帮助吗?

解决方案

使用此方法禁用Android中的主键

@Override
public void onAttachedToWindow() {
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    super.onAttachedToWindow();
}

其他提示

我找到了解决主机的方法。对于您的应用程序,将清单设置为

<action android:name="android.intent.action.MAIN" />              
<category android:name="android.intent.category.HOME" />                 
<category android:name="android.intent.category.DEFAULT" />               
<category android:name="android.intent.category.MONKEY"/>

现在,您的应用程序是替代启动器应用程序。

使用ADB,并使用软件包管理器禁用启动器应用程序

pm disable com.android.launcher2

现在,主钥匙按始终将留在同一屏幕上。

该解决方案最多可用3.x。

好的,这应该是一个棘手的问题。但是,这是一种破解它的方法。

覆盖您的活动中的以下方法,

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);           
}

现在处理这样的关键事件,

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
   if(keyCode == KeyEvent.KEYCODE_HOME)
    {
     Log.i("Home Button","Clicked");
    }
   if(keyCode==KeyEvent.KEYCODE_BACK)
   {
        finish();
   }
 return false;
}

将此代码添加到您的 MainActivity 班级:

Timer timer;
MyTimerTask myTimerTask;

@Override
protected void onResume() {
    super.onResume();

    if (timer != null) {
        timer.cancel();
        timer = null;
    }
}

@Override
protected void onPause()
{
    if (timer == null) {
        myTimerTask = new MyTimerTask();
        timer = new Timer();
        timer.schedule(myTimerTask, 100, 100);
    }

    super.onPause();
}

private void bringApplicationToFront()
{
    KeyguardManager myKeyManager = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
    if( myKeyManager.inKeyguardRestrictedInputMode())
        return;

    Log.d("TAG", "====Bringging Application to Front====");

    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    try
    {
        pendingIntent.send();
    }
    catch (PendingIntent.CanceledException e)
    {
        e.printStackTrace();
    }
}

public void onBackPressed() {
    // do not call super onBackPressed.
}

class MyTimerTask extends TimerTask {
    @Override
    public void run() {
        bringApplicationToFront();
    }
}

这不是“家庭”按钮的锁定,但是用户不能长时间切换到另一个应用程序(超过100毫秒),也许这就是您想要的。

要禁用主页按钮,请尝试以下操作:

@Override
public void onAttachedToWindow() {
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    super.onAttachedToWindow();
}

拉下通知栏的问题可以通过隐藏通知栏,如下所示:

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.xxxx);
    getWindow().addFlags(LayoutParams.FLAG_FULLSCREEN);
    ....
}

或为您的活动或应用程序设置全屏主题:

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top