How can I check the app is movable to sd or not

≡放荡痞女 提交于 2019-12-01 14:32:10

My first guess is too look into Android Source code for InstalledAppDetails activity. This is activity which shows "Move to phone" and "Move to SD card" buttons. It has interesting function called initMoveButton:

private void initMoveButton() {
    String pkgName = mAppInfo.packageName;
    boolean dataOnly = false;
    ApplicationInfo info1 = null;
    PackageInfo pkgInfo = null;

    try {
        info1 = mPm.getApplicationInfo(pkgName, 0);
        pkgInfo = mPm.getPackageInfo(mAppInfo.packageName,
                PackageManager.GET_UNINSTALLED_PACKAGES);
    } catch (NameNotFoundException e) {
    }
    dataOnly = (info1 == null) && (mAppInfo != null);
    boolean moveDisable = true;
    if (dataOnly) {
        mMoveAppButton.setText(R.string.move_app);
    } else if ((mAppInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
        mMoveAppButton.setText(R.string.move_app_to_internal);
        // Always let apps move to internal storage from sdcard.
        moveDisable = false;
    } else {
        mMoveAppButton.setText(R.string.move_app_to_sdcard);
        if ((mAppInfo.flags & ApplicationInfo.FLAG_FORWARD_LOCK) == 0 &&
                (mAppInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0 &&
                pkgInfo != null) {
            if (pkgInfo.installLocation == PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL ||
                    pkgInfo.installLocation == PackageInfo.INSTALL_LOCATION_AUTO) {
                moveDisable = false;
            } else if (pkgInfo.installLocation == PackageInfo.INSTALL_LOCATION_UNSPECIFIED) {
                IPackageManager ipm  = IPackageManager.Stub.asInterface(
                        ServiceManager.getService("package"));
                int loc;
                try {
                    loc = ipm.getInstallLocation();
                } catch (RemoteException e) {
                    Log.e(TAG, "Is Pakage Manager running?");
                    return;
                }
                if (loc == PackageHelper.APP_INSTALL_EXTERNAL) {
                    // For apps with no preference and the default value set
                    // to install on sdcard.
                    moveDisable = false;
                }
            }
        }
    }
    if (moveDisable) {
        mMoveAppButton.setEnabled(false);
    } else {
        mMoveAppButton.setOnClickListener(this);
        mMoveAppButton.setEnabled(true);
    }
}

Though not all classes used in this functions are available through public API. Specifically PackageHelper is internal class.

You can get all aplications filter by user and try if packetInfo set filter 'INSTALL_LOCATION_INTERNAL_ONLY'

    List<ApplicationInfo> packagesAll = pm.getInstalledApplications(PackageManager.GET_META_DATA);

    List<ApplicationInfo> packagesByUser = Stream.of(packagesAll)
        .filter((p -> (p.flags & ApplicationInfo.FLAG_SYSTEM) == 0))
        .filter((this::tryMoveSDPackage))
        .map(ApplicationInfo::new)
        .collect(Collectors.toList());

private boolean tryMoveSDPackage(ApplicationInfo applicationInfo) {
    try {
        PackageInfo packageInfo = getPackageManager().getPackageInfo(applicationInfo.packageName, 0);
        if (packageInfo.installLocation == PackageInfo.INSTALL_LOCATION_AUTO) {
            //Log.i(TAG, "INSTALL_LOCATION_AUTO");
            return true;
        } else if (packageInfo.installLocation == PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL) {
            //Log.d(TAG, "INSTALL_LOCATION_PREFER_EXTERNAL");
            return true;
        } else {
            //Log.w(TAG, "INSTALL_LOCATION_INTERNAL_ONLY");
            return false;
        }

    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return false;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!