如何以编程方式设置默认启动器?

时间:2016-12-20 09:36:46

标签: android default android-launcher

我想将我的启动器作为默认启动器。我的代码适用于许多人,但没有在LeTtv设备上工作。 因为它提供了默认应用设置的默认启动器设置。

运行此代码时,它会在默认的Launcher上移动,但不会在LeTv设备中显示Launcher选择器弹出窗口。

如何打开默认应用选择设置?

我的选择默认启动器的代码

private void setDefLauncher(Context c) {



    PackageManager p = c.getPackageManager();
    ComponentName cN = new ComponentName(c, FakeLauncher.class);
    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);
}

的AndroidManifest.xml

        <activity
            android:name="com.android.launcher.launcher3.Launcher"
            android:clearTaskOnLaunch="true"
            android:enabled="true"
            android:excludeFromRecents="true"
            android:launchMode="singleTask"
            android:resumeWhilePausing="true"
            android:screenOrientation="nosensor"
            android:stateNotNeeded="true"
            android:taskAffinity=""
            android:theme="@style/Theme"
            android:windowSoftInputMode="adjustPan" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.HOME" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.MONKEY" />
            </intent-filter>
        </activity>
  <activity
            android:name="com.launcher2.activitys.FakeLauncher"
            android:enabled="false"
            android:excludeFromRecents="true" >
            <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>

手动设置默认启动器 设置 - &gt;应用程序管理器 - &gt;默认应用程序设置 - &gt;设置启动器 enter image description here

1 个答案:

答案 0 :(得分:2)

我在 Google StackOverFlow &amp; Github ,但我没找到, 我的运气一些Luncher帮助我重定向Levt&amp ;;的HomeStting Activity。 MI手机,按照我的期望工作。 在一些它完美的工作中,我只是对它进行反编译并获取使用过的代码。

用于设置默认启动器

new SetDefaultLauncher(activity).launchHomeOrClearDefaultsDialog();

package com.android.launcher;

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;
import com.example.launcher.R;
import com.launcher2.activitys.FakeLauncher;

public class SetDefaultLauncher {

    public static final String LAUNCHER_CLASS = "com.android.launcher.launcher3.Launcher";
    public static final String LAUNCHER_PACKAGE = "com.android.launcher";

    Activity activity;
    SetDefaultLauncher(Activity activity){
        this.activity=activity;
    }
    enum HomeState {
        GEL_IS_DEFAULT, OTHER_LAUNCHER_IS_DEFAULT, NO_DEFAULT
    }
    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) {
            string = activity.getString(R.string.change_default_home_dialog_body,
                    new Object[] { loadLabel });
            intent = new Intent(
                    "android.settings.APPLICATION_DETAILS_SETTINGS",
                    Uri.fromParts("package",
                            resolveInfo.activityInfo.packageName, null));
        } else {
            intent = new Intent("android.settings.HOME_SETTINGS");
            string = new SpannableString(activity.getString(
                    R.string.change_default_home_dialog_body_settings,
                    new Object[] { loadLabel }));
            ((SpannableString) string)
                    .setSpan(
                            new TextBuilder(
                                    activity.getString(
                                            R.string.change_default_home_dialog_body_settings_tts,
                                            loadLabel)).build(), 0, string
                                    .length(), 18);
        }



        new AlertDialog.Builder(activity)
                .setIcon(R.drawable.ic_launcher)
                .setMessage(string)
                .setNegativeButton(
                        activity.getString(R.string.change_default_home_dialog_cancel),
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub
                                activity.finish();
                            }
                        })
                .setOnCancelListener(new DialogInterface. OnCancelListener() {

                    @Override
                    public void onCancel(DialogInterface dialog) {
                        // TODO Auto-generated method stub
                        activity.finish();
                    }
                })
                .setPositiveButton(
                        activity.getString(R.string.change_default_home_dialog_proceed),
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub
                                try {
                                    intent.setFlags(276856832);
                                    activity.startActivity(intent);
                                } catch (Exception e) {
                                    setDefLauncher(activity);
                                }
                            }
                        }).create().show();
    }

    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, FakeLauncher.class);
        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);
    }
}