2012-07-03 237 views
8

我正在爲我的大學開展一項研究項目。該應用程序將永遠不會投放市場,只用於研究。在主屏幕應用程序中禁用主頁按鈕?

我使用Google Homescreen示例代碼製作了主屏幕應用程序。 在那裏,我做了一個鎖屏的活動。 在那裏,用戶不應該能夠通過按主頁,返回等來退出鎖定。 後退按鈕似乎被禁用,但主頁按鈕不是。 我已經嘗試了幾種互聯網和計算器的解決方案,這些都不起作用。

這裏是重要的代碼:

注意:logcat中顯示「按鈕按下:4!」的後退按鈕,但沒有對home鍵)

在我的鎖屏活動:

@Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     Log.v(TAG, "BUTTON PRESSED: " + new Integer(keyCode).toString()); 

     if ((keyCode == KeyEvent.KEYCODE_BACK)) { 
      return true; 
     } else if ((keyCode == KeyEvent.KEYCODE_CALL)) { 
      return true; 
     } 
     else if ((keyCode == KeyEvent.KEYCODE_HOME)){ 
      return true; 
     } 
     return super.onKeyDown(keyCode, event); 
    } 

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

這似乎是onAttachedToWindow()方法,因爲Android的版本不工作4. 我如何禁用爲homeButton?

編輯:清單文件:

<?xml version="1.0" encoding="utf-8"?> 

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.android.home" > 

    <uses-permission android:name="android.permission.CALL_PHONE" /> 
    <uses-permission android:name="android.permission.GET_TASKS" /> 
    <uses-permission android:name="android.permission.READ_CONTACTS" /> 
    <uses-permission android:name="android.permission.SET_WALLPAPER" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.EXPAND_STATUS_BAR" /> 
    <uses-permission android:name="android.permission.NFC" /> 
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" /> 

    <permission android:name="android.permission.WRITE_SECURE_SETTINGS" > 
    </permission> 

    <application 
     android:icon="@drawable/ic_launcher_home" 
     android:label="@string/home_title" > 
     <service android:name=".MyService" > 
     </service> 

     <activity 
      android:name="Home" 
      android:launchMode="singleInstance" 
      android:stateNotNeeded="true" 
      android:theme="@style/Theme" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

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


      </intent-filter> 

      <receiver android:name=".ScreenReceiver" > 
       <intent-filter> 
        <action android:name="android.intent.action.SCREEN_ON" /> 
        <action android:name="android.intent.action.SCREEN_OFF" /> 
        <action android:name="android.intent.action.USER_PRESENT" /> 
       </intent-filter> 
      </receiver> 
     </activity> 
     <activity 
      android:name="Wallpaper" 
      android:icon="@drawable/bg_android_icon" 
      android:label="Wallpaper" > 
      <intent-filter> 
       <action android:name="android.intent.action.SET_WALLPAPER" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".LockPage" > 
      <intent-filter> 
       <action android:name="android.intent.action.SCREEN_ON" /> 
       <action android:name="android.intent.action.SCREEN_OFF" /> 
       <action android:name="android.intent.action.USER_PRESENT" /> 
       <action android:name="android.nfc.action.NDEF_DISCOVERED" /> 
       <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" /> 
       <category android:name="android.intent.category.DEFAULT" /> 

       <action android:name="android.intent.action.MAIN" /> 

       <data android:mimeType="text/plain" /> 
      </intent-filter> 
      <intent-filter> 
       <action android:name="android.nfc.action.NDEF_DISCOVERED" /> 

       <category android:name="android.intent.category.DEFAULT" /> 

       <data android:scheme="http" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 
+0

http://stackoverflow.com/questions/3898876/how-to -disable-home-key/8889913#8889913 – user370305

+1

這似乎指向我發佈的完全相同的東西?或者我錯過了你想要創造的點? – hamena314

+0

你有沒有試過這種[一個?(http://stackoverflow.com/questions/10025660/override-home-and-back-button-is-case-a-boolean-is-true/10025904#10025904) –

回答

5

這可能會有點晚,我一直在有點類似的情況。我的問題是,我不希望用戶在打電話時離開通話屏幕,但我無法阻止它,所以每次他們離開時都只是簡單地將它放回原處。

你的情況,你可以簡單地把你的應用程序後到前的暫停:

@Override 
protected void onPause() { 
    super.onPause(); 
     // Close and reopen app or bringToFront() 
} 

所以離開會再次自動打開應用程序。你應該嘗試重新開放你的活動,或者把它放在前面,看看什麼效果最好。如果刪除所有動畫並添加FLAG_ACTIVITY_NO_ANIMATION,重新打開可能並不明顯。

1

這是不可能的,而不改變Android的源:[這裏所說] [1]。

此外,這將打破Android Activity Cycle,這是不推薦的。

+0

-1,這問題是鎖屏,而不是普通的應用程序。 – Matsemann

+0

在代碼的早期版本中,該程序只是一項活動。我能夠將home按鈕指向活動本身,因此僅用於此目的。也許有辦法解決? – hamena314

2

看起來主頁按鈕按鈕不會被轉發到主屏幕應用程序中的應用程序。 因此,我做了一個正常的應用程序,把我的broadcastReceiver和我的服務,現在我可以禁用homebutton和後退按鈕。

仍然最近的應用程序按鈕可以用來跳出我的鎖屏。你可以用可能工作的虛擬條目填充它。

希望能幫助別人!

2
@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; 
} 
1

這個代碼在我的應用程序

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