如何通过代码获取Android中的默认设备帮助应用程序? [英] How to get default device assistance app in android by code?

查看:259
本文介绍了如何通过代码获取Android中的默认设备帮助应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的手机安装了两个语音搜索:google app和S-voice app。默认应用程序是S-voice应用程序,如下图所示。我的问题是,我们如何才能在Android 6.0中使用programmingcally获得默认的语音应用程序。预先谢谢您

my phone installed two voice searches: google app and S-voice app. The default app is S-voice app as figure bellow. My question is that how can we get the default voice application using programmingcally in Android 6.0. Thank you in advance

这就是我的意思

private boolean isMyAppLauncherDefault(String myPackageName) {
    final IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
    filter.addCategory(Intent.CATEGORY_HOME);

    List<IntentFilter> filters = new ArrayList<IntentFilter>();
    filters.add(filter);
    List<ComponentName> activities = new ArrayList<ComponentName>();
    final PackageManager packageManager = (PackageManager) getPackageManager();

    packageManager.getPreferredActivities(filters, activities, null);
    for (ComponentName activity : activities) {

        Log.d(TAG,"======packet default:==="+activity.getPackageName());
    }
    for (ComponentName activity : activities) {

        if (myPackageName.equals(activity.getPackageName())) {
            return true;
        }
    }
    return false;
}

当我的输入为 com.samsung.voiceserviceplatform 。换句话说,默认应用始终返回 com.google.android.googlequicksearchbox (表示Google语音)

The above function is alway return true when my input is com.samsung.voiceserviceplatform. In other hands, the default app always returns com.google.android.googlequicksearchbox (indicates google voice)

推荐答案

DefaultAssistPreference 使用 AssistUtils 来检索当前的Assist。您可以通过反射使用相同的方法:

The DefaultAssistPreference uses an hidden method of AssistUtils to retrieve the current Assist. You can use the same method using reflection:

public ComponentName getCurrentAssistWithReflection(Context context) {
    try {
        Method myUserIdMethod = UserHandle.class.getDeclaredMethod("myUserId");
        myUserIdMethod.setAccessible(true);
        Integer userId = (Integer) myUserIdMethod.invoke(null);

        if (userId != null) {
            Constructor constructor = Class.forName("com.android.internal.app.AssistUtils").getConstructor(Context.class);
            Object assistUtils = constructor.newInstance(context);

            Method getAssistComponentForUserMethod = assistUtils.getClass().getDeclaredMethod("getAssistComponentForUser", int.class);
            getAssistComponentForUserMethod.setAccessible(true);
            return (ComponentName) getAssistComponentForUserMethod.invoke(assistUtils, userId);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

如果您不想使用反射,则可以直接检查系统设置:

If you don't want to use reflection you can directly check the system settings:

public ComponentName getCurrentAssist(Context context) {
    final String setting = Settings.Secure.getString(context.getContentResolver(), "assistant");

    if (setting != null) {
        return ComponentName.unflattenFromString(setting);
    }

    return null;
}

与读取 AssistUtils相同的设置,但 AssistUtils 也具有后备(如果设置无效)。

It is the same setting that reads AssistUtils, but AssistUtils has also a fallback if the setting is not valid.

这篇关于如何通过代码获取Android中的默认设备帮助应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆