0

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>
Mace
  • 141
  • 1
  • 1
  • 4

2 Answers2

0

You can check this project on Github: https://github.com/Neamar/Summon

It's an alternative Home Application that works properly.

I don't see what can create your described behavior. It's perhaps a device-specific issue.

Did you try a reboot after setting up your app?

Hartok
  • 2,147
  • 20
  • 37
  • I actually have 4 phones that I test on: HTC Desire (Android 2.2), HTC One S (Android 4), Samsung Galaxy S2 X, and Samsung Galaxy S3. All these phones have the same issue. I know, the issue is weird and doesn't make sense. I'll take a look at the project on Github. – Mace Mar 05 '13 at 03:24
  • I took a look at the Summon application and that one is slightly different...it's got even more issues. If during the initial startup, I don't set it as the home launcher, the prompt to set it as the home launcher never pops up on subsequent startups so you only get one chance to set it as your default launcher and after that you can't..... – Mace Mar 05 '13 at 03:34
0

If you are using certain versions of Android, you actually have to clear the defaults of the app that has been set as default. I was having the same problem as you. Then I checked out the app called Home Switcher for Froyo on the app market. In that app, if a default has been set, Home Switcher brings up the default launcher's app settings screen and tells the user to select the button "clear defaults." This is the only way that I know of.

So in your app, you have to check a few different things.

  1. If no default launcher is set, set yours (like you said above that it works already)

  2. If a default is set, launch its app settings, prompt user to select "clear defaults", then have them press the home button and select your app

I hope this helps, I don't know if I am the best person to ask for code examples as my app is still a little buggy, but I can point you to some stack overflow answers:

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

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

Good luck, email me if you're stuck (lus u s_v [i] r at yahoo) - ignore brackets and spaces

Community
  • 1
  • 1
mnearents
  • 646
  • 1
  • 6
  • 31
  • Thanks for the helpful hints! Although, I'd rather avoid doing this with my app for now, if that's the only way to go about it then I'll have to do this. I'll be doing some more digging for a few more days and if I find another workaround I'll definitely share! – Mace Mar 05 '13 at 15:22