android判断自己的程序是否是默认桌面

最近做程序,用到了辅助功能设置桌面,以下是设置进入桌面的代码。
public static void setDefaultL(Activity activity) {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory("android.intent.category.HOME");
    //这里就是为了处置华为手机
    try {
        //华为手机
        startHuaweiSettingActOfDefLauncher(activity);//开启桌面设置
    } catch (Exception e1) {
        try {
            //华为手机
            intent.setComponent(new ComponentName("com.huawei.android.internal.app", "com.huawei.android.internal.app.HwResolverActivity"));//这个类有些华为手机找不到
            activity.startActivity(intent);
        } catch (Exception e2) {
            try {
                intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.Settings$HomeSettingsActivity"));//这个类有些华为手机找不到
                activity.startActivity(intent);
            } catch (Exception e3) {
                try {
                    activity.startActivity(new Intent(Settings.ACTION_HOME_SETTINGS));
                } catch (Exception e4) {
                    try {
                        intent.setComponent(new ComponentName("android", "com.android.internal.app.ResolverActivity"));
                        activity.startActivity(intent);
                    } catch (Exception e5) {

                    }
                }
            }
        }
    }
}

/**
 * 设置为华为手机
 */
private static void startHuaweiSettingActOfDefLauncher(Activity activity) {
    IntentFilter localIntentFilter = new IntentFilter();
    localIntentFilter.addAction(Intent.ACTION_MAIN);//"android.intent.action.MAIN"
    localIntentFilter.addCategory(Intent.CATEGORY_HOME);//"android.intent.category.HOME"
    Intent localIntent3 = new Intent(localIntentFilter.getAction(0));
    localIntent3.addCategory(localIntentFilter.getCategory(0));
    Intent localIntent4 = new Intent();
    localIntent4.setClassName("com.android.settings", "com.android.settings.Settings$PreferredSettingsActivity");
    localIntent4.putExtra("preferred_app_package_name", activity.getPackageName());
    localIntent4.putExtra("preferred_app_class_name", activity.getClass().getName());
    localIntent4.putExtra("is_user_confirmed", true);
    localIntent4.putExtra("preferred_app_intent", localIntent3);
    localIntent4.putExtra("preferred_app_intent_filter", localIntentFilter);
    localIntent4.putExtra("preferred_app_label", "默认桌面设置");
    activity.startActivity(localIntent4);
}
然后用辅助功能设置,当设置到com.android.internal.app.ResolverActivity的时候,我是这么做的:进行点击。
if (packageName.equals("android")&& className.equals("com.android.internal.app.ResolverActivity")){
    AccessibityUtils.clickTextViewByText(accessibilityService,accessibilityService.getString(R.string.tyfly_launcher));
    return true;
}
但是这个未能真正的点击。
/**
 * 判断自己的应用程序是否为默认桌面
 */
public final boolean isDefaultHome() {
    Intent intent = new Intent(Intent.ACTION_MAIN);//Intent.ACTION_VIEW
    intent.addCategory("android.intent.category.HOME");
    intent.addCategory("android.intent.category.DEFAULT");
    PackageManager pm = getPackageManager();
    ResolveInfo info = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
    boolean isDefault = getPackageName().equals(info.activityInfo.packageName);
    return isDefault;
}
但是我在实践中发现:当用辅助功能设置桌面的时候,分明已经设置了默认桌面,但是这个判断还是处于false状态,该怎么办呢?
策略1:设置判断多样化,如果是进入到ResolverActivity时进行标记,那么这样的话,也就在isDefaultHome()判断完之后,进行标记判断,如果进入过启动ResolverActivity,则返回true。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值