判断apk是否安装的sd卡,还是手机内存

一、判断apk是否安装的sd卡,还是手机内存

 

 

PackageInfo = ctx.getPackageManager().getPackageInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES)

boolean isSdcard = (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE)!=0 ; //判断当前APP是否安装的SD卡里面

为true则,Apk安装的SD卡里

 

二、

/**
* Checks if the application is installed on the SD card.
* 
* @return <code>true</code> if the application is installed on the sd card
*/
public static boolean isInstalledOnSdCard() {

Context context = App.getContext();
// check for API level 8 and higher
if (VERSION.SDK_INT > android.os.Build.VERSION_CODES.ECLAIR_MR1) {
  PackageManager pm = context.getPackageManager();
  try {
    PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
    ApplicationInfo ai = pi.applicationInfo;
    return (ai.flags & FLAG_EXTERNAL_STORAGE) == FLAG_EXTERNAL_STORAGE;
  } catch (NameNotFoundException e) {
    // ignore
  }
}

// check for API level 7 - check files dir
try {
  String filesDir = context.getFilesDir().getAbsolutePath();
  if (filesDir.startsWith("/data/")) {
    return false;
  } else if (filesDir.contains("/mnt/") || filesDir.contains("/sdcard/")) {
    return true;
  }
} catch (Throwable e) {
  // ignore
}

return false;
}

 

 

三、当SD卡除掉时,获得已安装在SD卡上面的apk的时候,图标不能显示,

 

这时判断是否应用不能用的方法:

 

String path = packageInfo.applicationInfo.sourceDir ;

if(isSdcard && path != null && !new File(path).exists() ){
	continue ;
}else if(appInfo!=null){
	//正常逻辑}

 

 

你可能感兴趣的:(apk)