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

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

 

 

Java代码  收藏代码
  1. PackageInfo = ctx.getPackageManager().getPackageInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES)  
  2.   
  3. boolean isSdcard = (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE)!=0 ; //判断当前APP是否安装的SD卡里面  
  4.   
  5. true则,Apk安装的SD卡里  

 

二、

Java代码  收藏代码
  1. /** 
  2. * Checks if the application is installed on the SD card. 
  3. *  
  4. * @return <code>true</code> if the application is installed on the sd card 
  5. */  
  6. public static boolean isInstalledOnSdCard() {  
  7.   
  8. Context context = App.getContext();  
  9. // check for API level 8 and higher  
  10. if (VERSION.SDK_INT > android.os.Build.VERSION_CODES.ECLAIR_MR1) {  
  11.   PackageManager pm = context.getPackageManager();  
  12.   try {  
  13.     PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);  
  14.     ApplicationInfo ai = pi.applicationInfo;  
  15.     return (ai.flags & FLAG_EXTERNAL_STORAGE) == FLAG_EXTERNAL_STORAGE;  
  16.   } catch (NameNotFoundException e) {  
  17.     // ignore  
  18.   }  
  19. }  
  20.   
  21. // check for API level 7 - check files dir  
  22. try {  
  23.   String filesDir = context.getFilesDir().getAbsolutePath();  
  24.   if (filesDir.startsWith("/data/")) {  
  25.     return false;  
  26.   } else if (filesDir.contains("/mnt/") || filesDir.contains("/sdcard/")) {  
  27.     return true;  
  28.   }  
  29. catch (Throwable e) {  
  30.   // ignore  
  31. }  
  32.   
  33. return false;  
  34. }  

 

 

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

 

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

 

Java代码  收藏代码
  1. String path = packageInfo.applicationInfo.sourceDir ;  
  2.   
  3. if(isSdcard && path != null && !new File(path).exists() ){  
  4.     continue ;  
  5. }else if(appInfo!=null){  
  6.     //正常逻辑}  

posted on 2015-10-15 10:59  何林子  阅读(444)  评论(0编辑  收藏  举报

导航