0

I am building a custom launcher for a kid mode app. I have followed this link to create a launcher picker to let user choose my app as their default launcher.

The problem is if there is a default launcher in their devices, the code in the link above doesn't work. I am testing on Zenphone5, Android 4.4.2. I have seen some people have the same problem create launcher picker on Android 4.4.2.

I have searched everywhere on how to create a launcher picker like these: Nester, iWawa. How can they do that?

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ttb.com.kidmode">

<!-- Hide status bar -->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

<!-- Hide the recent apps -->
<uses-permission android:name="android.permission.REORDER_TASKS" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Holo.Light">
    <activity android:name=".activity.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".activity.KidModeActivity"
        android:enabled="false">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

Create launcher picker dialog:

if (isMyLauncherDefault() == false) { // enable fake home to force system to clear default

    Log.v(">>>", "false");
    ComponentName launcherActivity = new ComponentName(this, KidModeActivity.class);
    getPackageManager().setComponentEnabledSetting(launcherActivity,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
    goHome();

} else {
    // Go home
    goHome();
}

Pressing the Home button:

private void goHome() {
    Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(startMain);
}

EDIT

The full story is: "I open my app, and click a button to go to my launcher -> nothing happens (because another launcher is set as default). If I open that launcher setting (Setting -> Apps -> ..) and click "Clear Default", then open my app again, click button to go to my launcher -> The dialog will show up. From now, the code works well". I dont want to open setting to click "Clear Defalt"

Daniel
  • 2,355
  • 9
  • 23
  • 30
  • post your code and your manifest, there is probably something you are missing – njzk2 Oct 05 '16 at 14:16
  • Hi @njzk2 just added code. Please check it –  Oct 05 '16 at 14:23
  • so what happens when `goHome` gets called? – njzk2 Oct 05 '16 at 14:32
  • If use have chosen my launcher as default, goHome will bring user to my custom launcher @njzk2. It mean press home button –  Oct 05 '16 at 14:33
  • OK the full story is: "I open my app, and click a button to go to my launcher -> nothing happens (because another launcher is set as default). If i open that launcher setting (Setting -> Apps -> ..) and click "Clear Defalt", then open my app again, click button to go to my launcher -> The dialog will show up". I dont want to open setting to click "Clear Defalt". @njzk2 –  Oct 05 '16 at 14:38
  • if another launcher is already the default, you can't override that yourself. You have to send the user to the settings page for the default app to let the user clear the "default" status – njzk2 Oct 05 '16 at 15:09
  • @njzk2 I want to show a launcher picker dialog if my launcher is not default. Nester, iWawa (kidmode apps I have listed above) can show a launcher picker dialog. How can they do this? –  Oct 05 '16 at 15:12
  • you can use an intent chooser. – njzk2 Oct 05 '16 at 17:21
  • @njzk2 yes. I can create an intent choose but, after user click my launcher, how can i process to make my launcher default? –  Oct 06 '16 at 08:10
  • What is a `launcher picker ` would u mind to explain? Thank you! – NoWar Feb 09 '18 at 02:25

2 Answers2

0

you need to give "<category android:name="android.intent.category.HOME" />" in Intent-filter of Androidmanifest.xml file.

please refer below link. http://arnab.ch/blog/2013/08/how-to-write-custom-launcher-app-in-android/

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
shripal
  • 1,222
  • 4
  • 19
  • 40
  • Do you mean ? I have these lines. If I open my default launcher setting and clear default. Then run again, the launcher picker dialog will show up. –  Oct 05 '16 at 14:18
  • have added category as HOME? – shripal Oct 05 '16 at 14:31
  • ".activity.KidModeActivity" is my custom launcher @shripal –  Oct 05 '16 at 14:32
  • OK the full story is: "I open my app, and click a button to go to my launcher -> nothing happens (because another launcher is set as default). If i open that launcher setting (Setting -> Apps -> ..) and click "Clear Defalt", then open my app again, click button to go to my launcher -> The dialog will show up". I dont want to open setting to click "Clear Defalt". @shripal –  Oct 05 '16 at 14:38
0

If the you have the same problem as me, here is the answer that I have found for 2 months. Hope it help someone.

If this link doesn't work.

Here is the solution: Use activity-alias

AndroidManifest.xml

<activity
        android:name=".activity.KidModeActivity"
        android:exported="true"
        android:launchMode="singleTask">
    </activity>
    <activity-alias
        android:name="LauncherAlias1"
        android:label="Kids Mode Launcher"
        android:targetActivity=".activity.KidModeActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity-alias>

    <activity-alias
        android:name="LauncherAlias2"
        android:enabled="false"
        android:targetActivity=".activity.KidModeActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity-alias>

somewhere create the launcher picker:

    public static void createLauncherPicker(Context context) {
    Log.v(">>>", "create launcher picker");
    PackageManager pm = context.getPackageManager();
    ComponentName cn1 = new ComponentName(GlobalValue.APP_NAME, GlobalValue.LAUNCHER_ALIAS_1); // Ex: "com.kidmod.android", "com.kidmod.android.LauncherAlias1"
    ComponentName cn2 = new ComponentName(GlobalValue.APP_NAME, GlobalValue.LAUNCHER_ALIAS_2);
    int dis = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
    if(pm.getComponentEnabledSetting(cn1) == dis) dis = 3 - dis;
    pm.setComponentEnabledSetting(cn1, dis, PackageManager.DONT_KILL_APP);
    pm.setComponentEnabledSetting(cn2, 3 - dis, PackageManager.DONT_KILL_APP);
}

Thanks to this greate answer: link

Community
  • 1
  • 1