2013-03-05 34 views
0

背景信息:我想創建一個兒童鎖應用程序。因此,當用戶在應用程序中時,我需要禁用所有密鑰(包括主頁密鑰)。我基本上想要一個類似的應用程序this one設置多個家庭啓動器應用程序不起作用

問題:如果我已將其他應用程序設置爲默認主頁啓動程序(即在HTC Home Sense UI或三星手機中的類似內容),然後將我的應用程序設置爲默認主頁啓動程序,然後按主頁鍵帶我回到主屏幕(即使我將我的應用程序設置爲默認主頁啓動器!)。現在,如果我沒有將任何其他應用程序設置爲默認主頁啓動器,只有我自己的應用程序,那麼沒有問題,並且當我進入我的應用程序並按下主鍵時,我仍留在應用程序中。

爲什麼當我在將應用程序設置爲默認設置之前設置默認的家庭應用程序時,主頁鍵不起作用(即離開我的應用程序)?但是,當我只將我的應用程序設置爲默認時,關鍵作品(即留在我的應用程序中)。

下面是我的測試應用程序的示例代碼:

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.testspinner" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="16" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.testspinner.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <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.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 
</manifest> 

MainActivity.java:

public class MainActivity extends Activity { 

    private PackageManager pm; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     this.pm = getPackageManager(); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    public boolean isHomeActivity() { 
     final IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN); 
     filter.addCategory(Intent.CATEGORY_HOME); 

     List<IntentFilter> filters = new ArrayList<IntentFilter>(); 
     filters.add(filter); 

     final String myPackageName = getPackageName(); 
     List<ComponentName> activities = new ArrayList<ComponentName>(); 
     final PackageManager packageManager = (PackageManager) getPackageManager(); 

     packageManager.getPreferredActivities(filters, activities, null); 

     for (ComponentName activity : activities) { 
      if (myPackageName.equals(activity.getPackageName())) { 
       return true; 
      } 
     } 
     return false; 
    } 

    protected void onResume() { 
     super.onResume(); 
     if (!isHomeActivity()) { 
      Intent localIntent = new Intent(Intent.ACTION_MAIN); 
      localIntent.addCategory(Intent.CATEGORY_HOME); 
      localIntent.setComponent(new ComponentName("android", 
        "com.android.internal.app.ResolverActivity")); 
      startActivity(localIntent); 
     } 
    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     switch (keyCode) { 
     case KeyEvent.KEYCODE_BACK: { 
      finish(); 
     } 
     } 
     return false; 
    } 
} 

activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

</RelativeLayout> 

回答

0

你可以檢查Github上的這個項目:https://github.com/Neamar/Summon

這是一個可以正常工作的替代家庭應用程序。

我不明白什麼可以創建您描述的行爲。這可能是一個設備特定的問題。

設置您的應用程序後,您嘗試重新啓動嗎?

+0

我居然有4部手機,我測試上:HTC Desire(Android 2.2),HTC One S(Android 4),三星Galaxy S2 X和三星Galaxy S3。所有這些手機都有相同的問題。我知道,這個問題很奇怪,沒有道理。我會看看Github上的項目。 – Mace 2013-03-05 03:24:52

+0

我看了一下Summon應用程序,其中一個稍有不同......它有更多的問題。如果在初始啓動期間,我沒有將它設置爲主啓動器,將其設置爲主啓動器的提示從未在隨後的啓動時彈出,因此您只有一次機會將其設置爲默認啓動器,然後可以't ..... – Mace 2013-03-05 03:34:39

0

如果您使用某些版本的Android,則實際上必須清除已被設置爲默認值的應用程序的默認值。我遇到了和你一樣的問題。然後我在應用程序市場上查看了名爲Home Switcher for Froyo的應用程序。在該應用程序中,如果設置了默認值,則Home Switcher將啓動默認啓動器的應用程序設置屏幕,並通知用戶選擇「清除默認值」按鈕。這是我知道的唯一途徑。

所以在你的應用程序中,你必須檢查一些不同的東西。

  1. 如果沒有缺省啓動程序設置,設置你的

  2. (就像你在上面,它的作品已經說過)如果默認設置,推出其應用程序設置,提示用戶選擇「清除默認值」 ,然後讓他們按home鍵,然後選擇您的應用程序

我希望這可以幫助,我不知道如果我要問的代碼示例爲我的應用程序仍然是一個小馬車的最佳人選,但我可以指點你一些堆棧溢出的答案:

https://stackoverflow.com/a/4772481/1817139

https://stackoverflow.com/a/10344985/1817139

運氣好,給我發電子郵件,如果你被卡住(在雅虎的LUüS_V [I] R) - 忽略括號和空格

+0

感謝您的幫助提示!雖然,現在我寧願避免使用我的應用程序來執行此操作,但如果這是解決此問題的唯一方法,那麼我必須執行此操作。我會再多挖幾天,如果我找到另一種解決方法,我一定會分享! – Mace 2013-03-05 15:22:27

相關問題