背景信息:我想創建一個兒童鎖應用程序。因此,當用戶在應用程序中時,我需要禁用所有密鑰(包括主頁密鑰)。我基本上想要一個類似的應用程序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>
我居然有4部手機,我測試上:HTC Desire(Android 2.2),HTC One S(Android 4),三星Galaxy S2 X和三星Galaxy S3。所有這些手機都有相同的問題。我知道,這個問題很奇怪,沒有道理。我會看看Github上的項目。 – Mace 2013-03-05 03:24:52
我看了一下Summon應用程序,其中一個稍有不同......它有更多的問題。如果在初始啓動期間,我沒有將它設置爲主啓動器,將其設置爲主啓動器的提示從未在隨後的啓動時彈出,因此您只有一次機會將其設置爲默認啓動器,然後可以't ..... – Mace 2013-03-05 03:34:39