android - 如何通过代码在android中获取默认设备辅助应用程序?

标签 android android-intent android-6.0-marshmallow android-settings google-voice

我的手机安装了两个语音搜索:google 应用程序和 S-voice 应用程序。默认应用程序是 S-voice 应用程序,如下图所示。我的问题是我们如何在 Android 6.0 中使用编程方式获取默认语音应用程序。提前谢谢你

enter image description here

这是我做的

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 时,上面的函数总是返回 true。另一方面,默认应用总是返回 com.google.android.googlequicksearchbox(表示谷歌语音)

最佳答案

DefaultAssistPreference使用 AssistUtils 的隐藏方法检索当前的辅助。您可以使用反射使用相同的方法:

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;
}

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

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 也有一个 fallback如果设置无效。

关于android - 如何通过代码在android中获取默认设备辅助应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40500143/

相关文章:

android - 已更新至 Android Marshmallow——现在,Android Studio 无法定位设备

java - 目录创建在 Marshmallow Android 中不起作用

android - Gradle 显示支持库错误 : "Module version com.android.support:support-v13:19.0.1 depends on libraries but is not a library itself"

安卓动画

Android:查找动态创建 View 的ID

android - 在 Android 中,我如何指定哪个 Activity 应该处理我的 Intent

android - 在 XMPP 服务器上创建组

android - Android Studio 如何保存和加载 bool 数组

Android - 将 Intent 作为新应用启动

android - Android M版本中如何根据指纹识别用户?