Background Information: I want to create a child lock application. Therefore I need to disable all the keys (including the Home key) when the user is in the application. I basically want a similar application to this one.
Problem: If I have set another application as the default home launcher (ie. in HTC the Home Sense UI, or something similar in Samsung phones) and then set my application as the default home launcher, then pressing the Home key takes me back to the home screen (even though I set my application as the default home launcher!). Now, if I don't set any other applications as the default home launcher, only my own application, then there is no problem, and when I'm inside my application and press the Home key I stay in the application.
Why is it that when I set a default home application before I set my application as the default, the Home key doesn't work (ie. leaves my app)? But when I only set my application as the default, the Home key works (ie. stays in my app).
Below is sample code for my test application:
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>