我想锁定屏幕。我希望禁用主页键,只使用返回键。如何实现?
我想锁定屏幕。我希望禁用主页键,只使用返回键。如何实现?
@Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
我找到了一种解决HOME键的方法。 对于您的应用程序,请将清单设置为
<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
现在按下Home键将始终停留在同一个屏幕上。这个解决方案仅适用于3.x及以下版本。
好的,本来这应该是一个难题。但是下面是一种破解方法。
在您的Activity中覆盖以下方法:
@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毫秒)无法切换到其他应用程序,也许这正是您想要的。
timer.schedule(myTimerTask, 1, 100);
,而不是 timer.schedule(myTimerTask, 100, 100);
。 - Manthan Patel禁用主页按钮,请尝试以下方法:
@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"