Android Question find and start the default launcher

peacemaker

Expert
Licensed User
Longtime User
HI, All

If we have our app as the default launcher, but we need to make some setup or another apps - is it possible to find the system device's launcher package, and then start its activity ?
 
Last edited:

User242424

Member
Licensed User
If we have our app as the default launcher, but we need to make some setup or another apps - is it possible to find the default launcher package, and then start its activity ?
Hey,
you don't need to find the launcher activity. you can start the app by this code [haven't test it before] :
B4X:
Dim Jo As JavaObject
Jo.InitializeContext
Jo = Jo.RunMethod("getPackageManager",Null)
Jo = Jo.RunMethod("getLaunchIntentForPackage",Array("APPLICATION_PACKAGENAME"))
Try
Jo = Jo.RunMethod("makeRestartActivityTask",Null)
Catch 'Ignore
End Try
StartActivity(Jo)
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Thanks, but how to initially find the system device's launcher package ?
 
Upvote 0

User242424

Member
Licensed User
Thanks, but how to initially find the system device's launcher package ?
Try this
B4X:
Dim Jo As JavaObject
Jo.InitializeContext
Jo = Jo.RunMethod("getPackageManager",Null)
Dim intent As Intent
intent.Initialize("android.intent.action.MAIN","")
intent.AddCategory("android.intent.category.HOME")
Jo = Jo.RunMethod("resolveActivity",Array(intent,0x00010000))
Jo = Jo.GetFieldJO("activityInfo")
Log(Jo.GetField("packageName"))
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User

Yes, thanks, works, but:
1) if to start in debug mode - result is just "android".
2) if to start in release mode - result is correct launcher package "com.google.android.apps.nexuslauncher" (on Emulator) or "com.android.launcher3" on a real device, but ... it cannot be started.
The intent is not initialized
B4X:
    Dim pm As PackageManager
    In = pm.GetApplicationIntent(package)

((RuntimeException) java.lang.RuntimeException: Object should first be initialized (Intent).)

Maybe it's some global limit.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
What are you tryin to start?

Trying to start second launcher.
1) Initially get the system default launcher package name and save it.
2) Then set my app as the default launcher.
3) And start first system's launcher from my app.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
The only thing that you can do, you can open settings for launcher.

Sure, it's done, manual control. But how to be if some setup is needed, or other apps ?
Yes, i have made the menu to start SYSTEM_SETTINGS

B4X:
    Dim in As Intent
    in.Initialize("android.settings.HOME_SETTINGS", "")    'in.Initialize("android.settings.SETTINGS", "")
    StartActivity(in)


but ... maybe more universal way just to start default launcher from my launcher ?
 
Upvote 0

User242424

Member
Licensed User
Based on https://stackoverflow.com/a/41311319

This should work

B4X:
Sub SetDefaultLauncherNow
    Dim Jo As JavaObject
    Jo.InitializeContext
    Jo.RunMethod("SetDefaultLauncherNow",Null)
End Sub

#if java
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build.VERSION;
import android.text.SpannableString;
import android.text.style.TtsSpan.TextBuilder;


   enum HomeState {
        GEL_IS_DEFAULT, OTHER_LAUNCHER_IS_DEFAULT, NO_DEFAULT
    }
    
    public void SetDefaultLauncherNow(){
    new SetDefaultLauncher(this).launchHomeOrClearDefaultsDialog();
    }

public class SetDefaultLauncher {

    public static final String LAUNCHER_CLASS = "com.android.launcher.launcher3.Launcher";
    public static final String LAUNCHER_PACKAGE = "com.android.launcher";
    
    private static final String change_default_home_dialog_body = "Change default home luncher";
    private static final String change_default_home_dialog_body_settings = "Change default home luncher settings";
    private static final String change_default_home_dialog_body_settings_tts = "Change default home luncher settings tts";
    
    Activity activity;
    public SetDefaultLauncher(Activity activity){
        this.activity=activity;
    }

    public boolean launchHomeOrClearDefaultsDialog() {
        Intent intent = new Intent("android.intent.action.MAIN");
        intent.addCategory("android.intent.category.HOME");
        ResolveInfo resolveActivity = activity.getPackageManager().resolveActivity(
                intent, 0);
        HomeState homeState = (LAUNCHER_PACKAGE
                .equals(resolveActivity.activityInfo.applicationInfo.packageName) && LAUNCHER_CLASS
                .equals(resolveActivity.activityInfo.name)) ? HomeState.GEL_IS_DEFAULT
                : (resolveActivity == null
                        || resolveActivity.activityInfo == null || !inResolveInfoList(
                        resolveActivity, activity.getPackageManager()
                                .queryIntentActivities(intent, 0))) ? HomeState.NO_DEFAULT
                        : HomeState.OTHER_LAUNCHER_IS_DEFAULT;
        switch (homeState) {
        case GEL_IS_DEFAULT:
        case NO_DEFAULT:
            intent = new Intent("android.intent.action.MAIN");
            intent.addCategory("android.intent.category.HOME");
            intent.setFlags(268435456);
            activity.startActivity(intent);
            return true;
        default:
            showClearDefaultsDialog(resolveActivity);
            return false;
        }
    }
    @SuppressLint("NewApi") private void showClearDefaultsDialog(ResolveInfo resolveInfo) {
        CharSequence string;
        final Intent intent;
        CharSequence loadLabel = resolveInfo.loadLabel(activity.getPackageManager());
        if (VERSION.SDK_INT < 21
                || activity.getPackageManager().resolveActivity(
                        new Intent("android.settings.HOME_SETTINGS"), 0) == null) {
      
            intent = new Intent(
                    "android.settings.APPLICATION_DETAILS_SETTINGS",
                    Uri.fromParts("package",
                            resolveInfo.activityInfo.packageName, null));
        } else {
            intent = new Intent("android.settings.HOME_SETTINGS");
          
        }


        
                                try {
                                    intent.setFlags(276856832);
                                    activity.startActivity(intent);
                                } catch (Exception e) {
                                    setDefLauncher(activity);
                                }
                          
    }

    private boolean inResolveInfoList(ResolveInfo resolveInfo,
            List<ResolveInfo> list) {
        for (ResolveInfo resolveInfo2 : list) {
            if (resolveInfo2.activityInfo.name
                    .equals(resolveInfo.activityInfo.name)
                    && resolveInfo2.activityInfo.packageName
                            .equals(resolveInfo.activityInfo.packageName)) {
                return true;
            }
        }
        return false;
    }

    private void setDefLauncher(Context c) {
        PackageManager p = c.getPackageManager();
        ComponentName cN = new ComponentName(c, this.activity.getClass());
        p.setComponentEnabledSetting(cN,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);

        Intent selector = new Intent(Intent.ACTION_MAIN);
        selector.addCategory(Intent.CATEGORY_HOME);
        c.startActivity(selector);
        p.setComponentEnabledSetting(cN,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
    }
}
#End If
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
This should work

No idea why this is, question was about starting the launcher itself. But thanks anyway for try.
So, starting another launcher from my default launcher is not possible, let's finish on this.
 
Upvote 0
Top