Android:更改默认家庭应用程序

发布于 2024-09-25 19:33:13 字数 1745 浏览 3 评论 0原文

对于某些特定要求 我需要更改 Android 默认主页应用程序 使用我的自定义主页应用程序(我的应用程序内的一个设置,它将切换默认主页=我的应用程序或以前的主页)

我不希望用户使用非常复杂的android设置。

任何人都可以帮我看看它在哪里注册 launcher.apk 为默认值 home 应用程序或如何更改它

我唯一能找到的是那个老问题: 如何更改默认的 Android 桌面应用程序?

但根本没有答案。

我在市场上看到 HomeSwitcher 可以做到这一点,但对于可能很忙的开发人员来说没有答案。

编辑

我在网上找到了这个 http://www.mail-archive.com/[email protected]/msg74167.html

但我得到了同样的问题:

这是我的代码:

    private void makePreferred() {
            PackageManager pm = getPackageManager();
            IntentFilter f = new IntentFilter("android.intent.action.MAIN");
            f.addCategory("android.intent.category.HOME");
            f.addCategory("android.intent.category.DEFAULT");
            ComponentName cn = new ComponentName("com.example.android.home", "com.example.android.home.Home");
            pm.addPreferredActivity(f, IntentFilter.MATCH_CATEGORY_EMPTY, null, cn);

我在以下位置设置了 android.permission.SET_PREFERRED_APPLICATIONS 显现。执行上面的代码后,日志声称事情已经发生 按预期添加(与我勾选“设为默认”时相同的日志) 来自 IntentResolver 的列表)。然而,当我继续点击主页时, 该列表仍然显示,并且日志显示:

INFO/PackageManager(52): Result set changed, dropping preferred
activity for Intent { act=android.intent.action.MAIN cat=
[android.intent.category.HOME] flg=0x10200000 } type null

因此解析程序似乎删除了默认条目。我在做吗 出了什么问题,或者这是一种安全措施?有什么想法 这背后?

for some specific requirement
I am required to change Android Default Home application
with my customized Home application ( a setting inside my app that will toggle default home = my application or previous home)

I don't want the user to travel android settings that are very complicated.

Can any one help me out like where it registers launcher.apk for default
home application or how to change that

The only thing I could find was that old question: How to change default Android's Desktop application?

but no answers at all.

I have seen HomeSwitcher in the Market that do the trick, but no answer for the developer that might certainly be busy.

EDIT

I found this on the web http://www.mail-archive.com/[email protected]/msg74167.html

But I got the same issue:

this is my code:

    private void makePreferred() {
            PackageManager pm = getPackageManager();
            IntentFilter f = new IntentFilter("android.intent.action.MAIN");
            f.addCategory("android.intent.category.HOME");
            f.addCategory("android.intent.category.DEFAULT");
            ComponentName cn = new ComponentName("com.example.android.home", "com.example.android.home.Home");
            pm.addPreferredActivity(f, IntentFilter.MATCH_CATEGORY_EMPTY, null, cn);

I have the android.permission.SET_PREFERRED_APPLICATIONS set in the
manifest. After executing the code above, the logs claim things have
been added like expected (same logs as when I tick off "Make default"
from IntentResolver's list). However, when I proceed by clicking home,
the list still shows up and the logs say:

INFO/PackageManager(52): Result set changed, dropping preferred
activity for Intent { act=android.intent.action.MAIN cat=
[android.intent.category.HOME] flg=0x10200000 } type null

So it seems the resolver deletes the default entry. Am I doing
something wrong, or is this a security measure? What are the ideas
behind this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

掀纱窥君容 2024-10-02 19:33:13

我对此进行了广泛的研究,从 2.2 开始,没有办法做到这一点。唯一的方法是使用幼儿锁应用程序所做的一些黑客攻击,但该应用程序最近使三星手机陷入无限循环,因此这是一种危险的方法。

如果您查看 froyo 源代码 这里的packagemanager类中,你会在addPreferredActivity方法中看到这个小情况:

if (getUidTargetSdkVersionLockedLP(Binder.getCallingUid())
                     < Build.VERSION_CODES.FROYO) {
                 Slog.w(TAG, "Ignoring addPreferredActivity() from uid"
                        + Binder.getCallingUid());
                 return;
             }

HomeSwitcher不工作正确地在 2.2 上,因为它使用这种方法和开发人员在应用程序页面上发表了评论“不支持 Froyo(2.2)”
由于 API 更改”

I did an extensive research on that and starting from 2.2 there is no way to do that. The only way is using some hacking that toddler lock app does but this app put samsung phones recently in the infinite loop, so it is a risky approach.

if you look at the froyo source code here of packagemanager class, you will see this small condition in the addPreferredActivity method:

if (getUidTargetSdkVersionLockedLP(Binder.getCallingUid())
                     < Build.VERSION_CODES.FROYO) {
                 Slog.w(TAG, "Ignoring addPreferredActivity() from uid"
                        + Binder.getCallingUid());
                 return;
             }

HomeSwitcher does not work properly on 2.2 since it uses this very method and developer made a comment on app page "Froyo(2.2) is not supported
due to the API change
"

英雄似剑 2024-10-02 19:33:13

“结果集已更改”意味着与该意图匹配的包集已从您创建默认值时指定的集更改为 - 因此默认值不再有效。您的组件列表(当前设置为空)需要包含设备上存在的所有主屏幕应用程序,而不仅仅是您的。

这是我测试过的示例代码(使用 adb shell am start http://www.google.co.uk/< /a> )并用于设置默认浏览器。 XXX 代表一个客户名称,我不得不将其涂黑。

请注意,为了调用 addPreferredActivity,您必须针对最小 sdk 版本 8 (2.2) 进行编译,并且必须指定 SET_PREFERRED_APPLICATIONS 权限。该权限的保护级别为 2,因此您需要使用与 PackageManager 相同的证书进行签名。

IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.VIEW");
filter.addCategory("android.intent.category.DEFAULT");
filter.addDataScheme("http");
Context context = getApplicationContext();
ComponentName component = new ComponentName("com.opera.mini.XXX", "com.opera.mini.XXX.CustomerBrowser");
ComponentName[] components = new ComponentName[] {new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"),
                                                  component};
pm.addPreferredActivity(filter, IntentFilter.MATCH_CATEGORY_SCHEME, components, component);

ETA - 如果您将此回复标记下来,请告诉我原因。我上面发布的代码已经过测试并且可以工作......

"Result set changed" means that the set of packages matching that intent has changed from the set you specified when you created the default - - so the default is no longer valid. Your list of components (which you are currently setting to null) needs to contain all homescreen apps present on device, not just yours.

Here's example code that I have tested (using adb shell am start http://www.google.co.uk/ ) and used to set the default browser. XXX represents a customer name that I had to black out.

Note that in order to call addPreferredActivity you must have compiled against a minimum-sdk version of 8 (2.2) and you must have specified the SET_PREFERRED_APPLICATIONS permission. That permission is protection level 2, so you need to be signed with the same certificate as PackageManager.

IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.VIEW");
filter.addCategory("android.intent.category.DEFAULT");
filter.addDataScheme("http");
Context context = getApplicationContext();
ComponentName component = new ComponentName("com.opera.mini.XXX", "com.opera.mini.XXX.CustomerBrowser");
ComponentName[] components = new ComponentName[] {new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"),
                                                  component};
pm.addPreferredActivity(filter, IntentFilter.MATCH_CATEGORY_SCHEME, components, component);

ETA - if you marked this response down, could you let me know why. The code I posted above is tested and working...

︶ ̄淡然 2024-10-02 19:33:13
startActivity(new Intent(Settings.ACTION_HOME_SETTINGS));
startActivity(new Intent(Settings.ACTION_HOME_SETTINGS));
此刻的回忆 2024-10-02 19:33:13

此代码适用于我的 ICS 设备:我使用一项对某些调用敏感的服务,其中一个称为 SET_PREFERRED_LAUNCHER,将新的默认启动程序包 (PREFERRED_PACKAGE_KEY) 及其活动 (PREFERRED_ACTIVITY_KEY) 放入捆绑包中,

Method installPackageMethod = null;
Method deletePackageMethod = null;
Method setPreferredActivityMethod = null;
Method replacePreferredActivityMethod = null;
Object pm = null;


    @Override
public void onCreate() {
    super.onCreate();
        if (pm == null)
        pm = getPackageManager();
    try {
        if (setPreferredActivityMethod == null)
            setPreferredActivityMethod = pm.getClass().getMethod(
                    "addPreferredActivity", IntentFilter.class, int.class,
                    ComponentName[].class, ComponentName.class);
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
}


private final class ServiceHandler extends Handler {
    private Context context;

    public ServiceHandler(Looper looper, Context ctx) {
        super(looper);
        context = ctx;
    }

    @Override
    public void handleMessage(Message msg) {
        Intent intent = (Intent) msg.getData().getParcelable(
                UPDATER_SERVICE_ACTION);
        int request = intent.getIntExtra(
                REQUEST_KEY,
                REQUEST_UNKNOWN);
        Bundle bundle = intent.getExtras();

        switch (request) {
        case INSTALL_APPLICATION: {
            if (bundle != null) {
                String appPath = bundle
                        .getString(APP_PATH_KEY);
                if (appPath != null) {
                    LogUtil.e(TAG, "try to install " + appPath);

                    try {
                        am.installPackage(appPath);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    LogUtil.e(TAG, "install of " + appPath + " done");
                }
            }
            break;
        }
        case UNISTALL_PACKAGE: {
            if (bundle != null) {
                String packagename = bundle
                        .getString(PACKAGE_NAME_KEY);
                if (packagename != null) {
                    LogUtil.e(TAG, "unistall " + packagename);

                    try {
                        deletePackageMethod
                                .invoke(pm, packagename, null, 0);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            break;
        }
        case SET_PREFERRED_LAUNCHER: {
            if (bundle != null) {
                String package_name = bundle
                        .getString(PREFERRED_PACKAGE_KEY);
                if (package_name == null) {
                    LogUtil.e(TAG,
                            "WARNING: setDefaultActivity cannot continue, package is NULL");
                    return;
                }
                String activity_name = bundle
                        .getString(PREFERRED_ACTIVITY_KEY);
                if (activity_name == null) {
                    LogUtil.e(TAG,
                            "WARNING: setDefaultActivity cannot continue, activity is NULL");
                    return;
                }

                LogUtil.e(TAG, "setDefaultActivity activity="
                        + activity_name + " package=" + package_name);

                IntentFilter filter = new IntentFilter(
                        "android.intent.action.MAIN");
                filter.addCategory("android.intent.category.HOME");
                filter.addCategory("android.intent.category.DEFAULT");
                ComponentName[] components = new ComponentName[] {
                        new ComponentName("com.android.launcher",
                                "com.android.launcher2.Launcher"),
                        new ComponentName(package_name, activity_name) };
                ComponentName activity = new ComponentName(package_name,
                        activity_name);
                try {
                    setPreferredActivityMethod.invoke(pm, filter,
                            IntentFilter.MATCH_CATEGORY_EMPTY, components,
                            activity);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        }

    }

}

请记住添加清单文件此权限:

    <uses-permission android:name="android.permission.SET_PREFERRED_APPLICATIONS"/>

用法:

    public void setPreferredLauncher(String activity_name,String package_name)
{
    Intent intent = new Intent(UPDATER_SERVICE_ACTION);
    intent.putExtra(REQUEST_KEY, SET_PREFERRED_LAUNCHER);
    intent.putExtra(PREFERRED_ACTIVITY_KEY, activity_name);
    intent.putExtra(PREFERRED_PACKAGE_KEY, package_name);
    context.startService(intent);
}

其中:

    public static final String _UPDATER_SERVICE_ACTION = "com.android.updaterservice.ACTION";
public static final String REQUEST_KEY = "com.android.updaterservice.REQUEST_KEY";
public static final String PACKAGE_NAME_KEY = "com.android.updaterservice.PACKAGE_NAME_KEY";
public static final String APP_PATH_KEY = "com.android.updaterservice.APP_PATH_KEY";
public static final String PREFERRED_ACTIVITY_KEY = "com.android.updaterservice.PREFERRED_ACTIVITY_KEY";
public static final String PREFERRED_PACKAGE_KEY = "com.android.updaterservice.PREFERRED_PACKAGE_KEY";
public static final String INSTALL_PACKAGE_RESULT = "com.android.updaterservice.INSTALL_PACKAGE_RESULT";
public static final String PACKAGE_NAME = "PACKAGE_NAME";
public static final String INSTALL_SUCCEEDED = "INSTALL_SUCCEEDED";
public static final int REQUEST_UNKNOWN = -1;
public static final int INSTALL_APPLICATION = 1;
public static final int UNISTALL_PACKAGE = 2;
public static final int SET_PREFERRED_LAUNCHER = 3;

This code works on my ICS device: I use a service that is sensible to some call, one of them is called SET_PREFERRED_LAUNCHER, the put in a bundle your new default Launcher package (PREFERRED_PACKAGE_KEY) and it's activity (PREFERRED_ACTIVITY_KEY)

Method installPackageMethod = null;
Method deletePackageMethod = null;
Method setPreferredActivityMethod = null;
Method replacePreferredActivityMethod = null;
Object pm = null;


    @Override
public void onCreate() {
    super.onCreate();
        if (pm == null)
        pm = getPackageManager();
    try {
        if (setPreferredActivityMethod == null)
            setPreferredActivityMethod = pm.getClass().getMethod(
                    "addPreferredActivity", IntentFilter.class, int.class,
                    ComponentName[].class, ComponentName.class);
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
}


private final class ServiceHandler extends Handler {
    private Context context;

    public ServiceHandler(Looper looper, Context ctx) {
        super(looper);
        context = ctx;
    }

    @Override
    public void handleMessage(Message msg) {
        Intent intent = (Intent) msg.getData().getParcelable(
                UPDATER_SERVICE_ACTION);
        int request = intent.getIntExtra(
                REQUEST_KEY,
                REQUEST_UNKNOWN);
        Bundle bundle = intent.getExtras();

        switch (request) {
        case INSTALL_APPLICATION: {
            if (bundle != null) {
                String appPath = bundle
                        .getString(APP_PATH_KEY);
                if (appPath != null) {
                    LogUtil.e(TAG, "try to install " + appPath);

                    try {
                        am.installPackage(appPath);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    LogUtil.e(TAG, "install of " + appPath + " done");
                }
            }
            break;
        }
        case UNISTALL_PACKAGE: {
            if (bundle != null) {
                String packagename = bundle
                        .getString(PACKAGE_NAME_KEY);
                if (packagename != null) {
                    LogUtil.e(TAG, "unistall " + packagename);

                    try {
                        deletePackageMethod
                                .invoke(pm, packagename, null, 0);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            break;
        }
        case SET_PREFERRED_LAUNCHER: {
            if (bundle != null) {
                String package_name = bundle
                        .getString(PREFERRED_PACKAGE_KEY);
                if (package_name == null) {
                    LogUtil.e(TAG,
                            "WARNING: setDefaultActivity cannot continue, package is NULL");
                    return;
                }
                String activity_name = bundle
                        .getString(PREFERRED_ACTIVITY_KEY);
                if (activity_name == null) {
                    LogUtil.e(TAG,
                            "WARNING: setDefaultActivity cannot continue, activity is NULL");
                    return;
                }

                LogUtil.e(TAG, "setDefaultActivity activity="
                        + activity_name + " package=" + package_name);

                IntentFilter filter = new IntentFilter(
                        "android.intent.action.MAIN");
                filter.addCategory("android.intent.category.HOME");
                filter.addCategory("android.intent.category.DEFAULT");
                ComponentName[] components = new ComponentName[] {
                        new ComponentName("com.android.launcher",
                                "com.android.launcher2.Launcher"),
                        new ComponentName(package_name, activity_name) };
                ComponentName activity = new ComponentName(package_name,
                        activity_name);
                try {
                    setPreferredActivityMethod.invoke(pm, filter,
                            IntentFilter.MATCH_CATEGORY_EMPTY, components,
                            activity);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        }

    }

}

Remember to add in your manifest file this permission:

    <uses-permission android:name="android.permission.SET_PREFERRED_APPLICATIONS"/>

Usage:

    public void setPreferredLauncher(String activity_name,String package_name)
{
    Intent intent = new Intent(UPDATER_SERVICE_ACTION);
    intent.putExtra(REQUEST_KEY, SET_PREFERRED_LAUNCHER);
    intent.putExtra(PREFERRED_ACTIVITY_KEY, activity_name);
    intent.putExtra(PREFERRED_PACKAGE_KEY, package_name);
    context.startService(intent);
}

where:

    public static final String _UPDATER_SERVICE_ACTION = "com.android.updaterservice.ACTION";
public static final String REQUEST_KEY = "com.android.updaterservice.REQUEST_KEY";
public static final String PACKAGE_NAME_KEY = "com.android.updaterservice.PACKAGE_NAME_KEY";
public static final String APP_PATH_KEY = "com.android.updaterservice.APP_PATH_KEY";
public static final String PREFERRED_ACTIVITY_KEY = "com.android.updaterservice.PREFERRED_ACTIVITY_KEY";
public static final String PREFERRED_PACKAGE_KEY = "com.android.updaterservice.PREFERRED_PACKAGE_KEY";
public static final String INSTALL_PACKAGE_RESULT = "com.android.updaterservice.INSTALL_PACKAGE_RESULT";
public static final String PACKAGE_NAME = "PACKAGE_NAME";
public static final String INSTALL_SUCCEEDED = "INSTALL_SUCCEEDED";
public static final int REQUEST_UNKNOWN = -1;
public static final int INSTALL_APPLICATION = 1;
public static final int UNISTALL_PACKAGE = 2;
public static final int SET_PREFERRED_LAUNCHER = 3;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文