[英]How to disable the home key
我想鎖定屏幕。 我想禁用Home鍵,而只使用Back鍵。 我該如何完成?
使用此方法在android中禁用Home鍵
@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。
好的,這應該是一個很難的問題。 但是,這是一種破解方法。
在您的活動中覆蓋以下方法,
@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 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.