背景情報: チャイルド ロック アプリケーションを作成したいと考えています。Homeしたがって、ユーザーがアプリケーションにいるときは、すべてのキー (キーを含む) を無効にする必要があります。私は基本的にこれに似たアプリケーションが欲しいです。
問題: 別のアプリケーションをデフォルトのホーム ランチャーとして設定した場合 (つまり、HTC ではホーム センス UI、または Samsung の携帯電話で同様のもの)、自分のアプリケーションをデフォルトのホーム ランチャーとして設定した場合、Homeキーを押すと、ホーム画面 (アプリケーションをデフォルトのホーム ランチャーとして設定していますが!)。ここで、他のアプリケーションをデフォルトのホームランチャーとして設定せず、自分のアプリケーションのみを設定しても問題はありません。アプリケーション内でHomeキーを押しても、アプリケーションにとどまります。
アプリケーションをデフォルトとして設定する前にデフォルトのホーム アプリケーションを設定すると、Homeキーが機能しない (つまり、アプリケーションから離れてしまう) のはなぜですか? ただし、アプリケーションをデフォルトとしてのみ設定すると、Homeキーが機能します (つまり、アプリに残ります)。
以下は、私のテスト アプリケーションのサンプル コードです。
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>