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