import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import java.io.File;
public class PackageUtils {
protected static final String TAG = PackageUtils.class.getSimpleName(); /**
* 安装apk应用
*/
public static void installAPK(Context context, File apkFile) {
if (apkFile.isFile()) {
String fileName = apkFile.getName();
String postfix = fileName.substring(fileName.length() - 4, fileName.length());
if (postfix.toLowerCase().equals(".apk")) {
String cmd = "chmod 755 " + apkFile.getAbsolutePath();
try {
Runtime.getRuntime().exec(cmd);
} catch (Exception e) {
Logger.e(TAG, e.getLocalizedMessage());
}
Uri uri = Uri.fromFile(apkFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
context.startActivity(intent);
}
} else if (apkFile.isDirectory()) {
File[] files = apkFile.listFiles();
int fileCount = files.length;
for (int i = 0; i < fileCount; i++) {
installAPK(context, files[i]);
}
}
} /**
* 安装apk应用
*/
public static void installDirApk(Context context, String filePath) {
File file = new File(filePath);
installAPK(context, file);
} /**
* 卸载apk文件
*/
public static void uninstallPackage(Context context, Uri packageUri) {
Intent intent = new Intent(Intent.ACTION_DELETE, packageUri);
context.startActivity(intent);
} /**
* 根据包得到应用信息
*/
public static ApplicationInfo getApplicationInfoByName(Context context, String packageName) {
if (null == packageName || "".equals(packageName)) {
return null;
}
try {
return context.getPackageManager().getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
} catch (NameNotFoundException e) {
Logger.e("EspanceUtils", packageName + " NameNotFoundException");
return null;
}
} /**
* 通过包名获取包信息
*/
public static PackageInfo getPackageInfoByName(Context context, String packageName) {
if (null == packageName || "".equals(packageName)) {
return null;
}
try {
return context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
} catch (NameNotFoundException e) {
Logger.e(TAG, e.getLocalizedMessage());
return null;
}
} /**
* 判断apk包是否安装
*/
public static boolean isApkIntalled(Context context, String packageName) {
if (null == getApplicationInfoByName(context, packageName)) {
return false;
} else {
return true;
}
}
}

最新文章

  1. JavaScript 正则表达式语法
  2. iOS界面开发
  3. oracle10g在rh6上安装缺少libXtst*的包导致不能出OUI
  4. Unity3D多人协作开发环境搭建
  5. js选中当前菜单后高亮显示的导航条
  6. Linux共享内存(一)
  7. C#文件对话框,一次多选文件设置
  8. 在网上浏览.NET的所有代码,并且让你的Visual Studio的go to definition(F12)指向在线代码
  9. myeclipse2014新感悟
  10. 【转】 各种 基于Unity3d 引擎的Android游戏优化 (drawcall)
  11. 【HDOJ】2405 Marbles in Three Baskets
  12. 从一个PHP数据生成 CSV 文件
  13. HDU 1085 Holding Bin-Laden Captive! (母函数)
  14. Android Studio 修改主题
  15. Coursera 机器学习笔记(八)
  16. Linux: curl
  17. Python生产者producer和consumer消费者案例写法,含有多线程,包含队列queue、JoinableQueue队列的用法
  18. electron培训 ppt
  19. Linux中Root密码破解
  20. C#编程经验-选择结构和循环结构

热门文章

  1. JPA自带findOne方法报错原因
  2. Centos7安装dig命令
  3. STM32——CAN协议帧的标准格式和扩展格式与优先级的关系
  4. linux常用的操作命令
  5. java 中的 Math.round(-1.5) 等于多少?(未完成)
  6. 洛谷 P2746 [USACO5.3]校园网Network of Schools (Tarjan,SCC缩点,DAG性质)
  7. CentOS下更改yum源
  8. centos 解决 mysql command not found
  9. 洛谷P1372 又是毕业季I【数论】
  10. 【DES加密解密】 C#&amp;JAVA通用