简体   繁体   中英

How to make Home_Screen launcher chooser in android?

I have developed a Home_Screen launcher. now, I want to set a button that when is clicked, shows a list of available Home_Screen Launchers. I have used this method:

Intent i = new Intent(Intent.ACTION_MAIN);
            i.addCategory(Intent.CATEGORY_HOME);
            i.addCategory(Intent.CATEGORY_DEFAULT);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(Intent.createChooser(i, "Choose Launcher, Please!"));

it does what I want. but when I choose my Home_Screen Launcher from this chooser, it just opens my Launcher as an activity and not the default Home_Screen Launcher.

so as you have found, I want to make a chooser and set my app as the default Home_Screen Launcher. How can I do this?

Note: I also have tried this method, but it doesn't show my app in the created chooser:

public static void resetPreferredLauncherAndOpenChooser(Context context) {
PackageManager packageManager = context.getPackageManager();
ComponentName componentName = new ComponentName(context, MainLauncher.class);
packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

Intent selector = new Intent(Intent.ACTION_MAIN);
selector.addCategory(Intent.CATEGORY_HOME);
selector.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(selector);

packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP);}

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

EDIT: this is my Manifest. I have an Activity as Home_Launcher and an Activity as category.LAUNCHER. Please Help me guys. I have searched the web and I didn't find the solution.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.ablazephoenix.myappluncher">

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="false"
    tools:replace="android:supportsRtl"
    android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen">
    <activity android:name=".FirstPage"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".Allowed_Apps_Chooser"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen"/>

    <activity android:name=".Wallpaper_Chooser"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen"/>

    <activity android:name=".MainLauncher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen"
        android:launchMode="singleTask"
        android:stateNotNeeded="true">
        <intent-filter>
            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.DEFAULT" />
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
    </activity>

    <service android:name=".LauncherService"/>

    <activity android:name=".Settings"
        android:label="Settings"
        android:theme="@style/AppTheme"/>

</application>

Finnaly, I resolved the problom!


at first, I apologize not to search the web correctly! ok. lets start. first imagine you have created a Home_Launcher named MainLauncher. look at below method:

Intent selector = new Intent(Intent.ACTION_MAIN);
    selector.addCategory(Intent.CATEGORY_HOME);
    selector.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(selector);

this method wouldn't work always. It will just work at first time that you want to set your Home_launcher as default. and if you try this method at the second time, the device wouldn't recognize your app as a new to use Home_Launcher, and this mentioned method will display the default Home launcher of device. so you should make a fake Home_launcher beside your Main Home_Launcher as below:

public class FakeLauncherActivity extends Activity {
/** THIS IS THE FAKE LAUNCHER AND SHOULDN'T CONTAIN ANYTHING! */}

and define it in Manifest as below:

<activity
        android:name=".FakeLauncherActivity"
        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>

...And you could make a chooser as below:

public static void resetPreferredLauncherAndOpenChooser(Context context) {
    PackageManager packageManager = context.getPackageManager();
    ComponentName componentName = new ComponentName(context, FakeLauncherActivity.class);
    packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

    Intent selector = new Intent(Intent.ACTION_MAIN);
    selector.addCategory(Intent.CATEGORY_HOME);
    selector.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(selector);

    packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP);
}

with using this method, any time that you are trying to make a chooser, the user device will think that a new Home_Launcher is available in device and will show all available Home_Launchers of device inside a chooser, contains your MainLauncher, except your FakeLauncherActivity.


I hope my answer could be useful. but for more info, you can take a look at this really nice post: enter link description here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM