码迷,mamicode.com
首页 > 移动开发 > 详细

判断Android应用是否安装、运行

时间:2015-12-05 21:02:36      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

 

本文介绍3个方法,判断手机上是否安装了某应用、该应用是否正在运行、获取手机某个APK文件的安装Intent.启动该Intent就可以直接安装该APK。

/**
* 判断应用是否已安装
*
* @param context
* @param packageName
* @return
*/
private boolean isInstalled(Context context, String packageName) {
boolean hasInstalled = false;
PackageManager pm = context.getPackageManager();
List<PackageInfo> list = pm
.getInstalledPackages(PackageManager.PERMISSION_GRANTED);
for (PackageInfo p : list) {
if (packageName != null && packageName.equals(p.packageName)) {
hasInstalled = true;
break;
}
}
return hasInstalled;
}

/**
* 获取文件安装的Intent
*
* @param file
* @return
*/
private Intent getFileIntent(File file) {
Uri uri = Uri.fromFile(file);
String type = "application/vnd.android.package-archive";
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri, type);
return intent;
}

/**
* 判断应用是否正在运行
*
* @param context
* @param packageName
* @return
*/
private boolean isRunning(Context context, String packageName) {
ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> list = am.getRunningAppProcesses();
for (RunningAppProcessInfo appProcess : list) {
String processName = appProcess.processName;
if (processName != null && processName.equals(packageName)) {
return true;
}
}
return false;
}

判断Android应用是否安装、运行

标签:

原文地址:http://www.cnblogs.com/qingchen1984/p/5022133.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!