问题描述
我想知道如何检查应用程序是否移动到SD或不通过code。我知道如何让安装的应用程序列表,它是不是在SD卡或没有。
如果ApplicationInfo标志包含ApplicationInfo.FLAG_EXTERNAL_STORAGE则是SD卡,但我怎么能检查应用程序是否可以移动到SD与否。
I want to know how to check whether the app is movable to sd or not through code. I know how to get the installed applications list and is it on sd card or not.
If ApplicationInfo flags contains ApplicationInfo.FLAG_EXTERNAL_STORAGE then it is on sd card but how can I check whether the app is movable to sd or not.
推荐答案
我的第一个猜测是太看看Android源$ C $ C的<一个href="http://$c$csearch.google.com/$c$csearch/p?hl=en#Q_A5W3uqPd4/trunk/Settings/src/com/android/settings/InstalledAppDetails.java&q=WifiEnabler&d=0"相对=nofollow> InstalledAppDetails
活动。这是活动充分体现了移动到手机和移动到SD卡按钮。它有有趣的功能叫做<一个href="http://$c$csearch.google.com/$c$csearch/p?hl=en#Q_A5W3uqPd4/trunk/Settings/src/com/android/settings/InstalledAppDetails.java&q=WifiEnabler&d=0&l=220"相对=nofollow> initMoveButton
:
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);
}
}
虽然不是所有的类在此函数中使用的都可以通过公开的API。具体来说 PackageHelper
是内部类。
这篇关于我怎么可以检查应用程序是移动到SD或不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!