Android之获取默认桌面、默认输入法、默认动态壁纸的接口

获取默认桌面:

public Set<String> getDefaultLaunchers() {
    Set<String> defaultLaunchers = new HashSet<String>();
    PackageManager packageManager = getPackageManager();
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    // 查询所有已安装的桌面应用
    List<ResolveInfo> launcherList = packageManager.queryIntentActivities(intent,
    PackageManager.MATCH_DEFAULT_ONLY);
    // 获取默认的桌面应用,没有指定默认桌面时返回”android”
    ResolveInfo defaultLauncher = packageManager.resolveActivity(intent,
    PackageManager.MATCH_DEFAULT_ONLY);
    if (launcherList != null && defaultLauncher != null) {
        String pkgName = defaultLauncher.activityInfo.packageName;
        System.out.println(“zyf default launcher is : ” + pkgName);
       // 没有指定默认桌面时返回的是“android”,故这里过滤下防止“android”被添加到默认桌面列表中
    for (ResolveInfo info : launcherList) {
        System.out.println(“zyf launcher : ” + info.activityInfo.packageName);
        if (info.activityInfo.packageName.equals(pkgName)) {
             defaultLaunchers.add(pkgName);
           }
        }
    }
    // 有多个桌面应用,且目前没有指定默认桌面时,返回所有的桌面应用
    if (defaultLaunchers.isEmpty() && launcherList != null) {
        for (ResolveInfo resolveInfo : launcherList) {
            defaultLaunchers.add(resolveInfo.activityInfo.packageName);
        }
    }
    System.out.println(“zyf defaultLaunchers : ” + defaultLaunchers);
    return defaultLaunchers;
}

 

获取默认输入法:

public String getDefaultInputMethod() {
    String defaultInputMethod = null;
    String inputComponent = Settings.Secure.getString(getContentResolver(),
    Settings.Secure.DEFAULT_INPUT_METHOD);
    if (!inputComponent.isEmpty()) {
        System.out.println(“zyf input method is : ” + inputComponent);
        // 获取的结果是:com.sohu.inputmethod.sogouoem/.SogouIME,故需要截取一下字符串
        defaultInputMethod = inputComponent.substring(0, inputComponent.indexOf(“/”));

    }
    return defaultInputMethod;
}

 

获取默认动态壁纸:

public String getDefaultLiveWallper() {
     String defaultWallper = null;
     WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
    // 当前设置的是动态壁纸则返回相关信息,静态壁纸则返回null
    WallpaperInfo wallpaperInfo = wallpaperManager.getWallpaperInfo();
    if (wallpaperInfo != null) {
          defaultWallper = wallpaperInfo.getPackageName();
    }
    System.out.println(“zyf default wallpaper is : ” + defaultWallper);
    return defaultWallper;
}

合作联系邮箱:cofface#cofface.com(请将#替换为@)

转载请注明来源:Cofface Blog » Android之获取默认桌面、默认输入法、默认动态壁纸的接口

赞 (29) 打赏

感谢您的支持,我会继续努力的!

支付宝扫一扫打赏