方法一:调用ActivityManager
代码如下
ActivityManager 游扮态am = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE)
List<RunningAppProcessInfo> list = _am.getRunningAppProcesses()
for (int i = 0 i < list.size() i++) 缺散{
Log.i("tag", list.get(i).pid)
}
方法二:调用Process类获取
只神源需要一句代码即可
int pid = android.os.Process.myPid()还可以使用android.os.Process.myTid()获取调用进程的线程ID
和android.os.Process.myUid():获取该进程的用户ID
在Android中,我们可以通晌埋过Process.myPid()和RunningAppProcessInfo来取得当前氏耐的进程名。
示例宴核蚂1
String getCurProcessName(Context context) {
int pid = android.os.Process.myPid()
ActivityManager mActivityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE)
for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager
.getRunningAppProcesses()) {
if (appProcess.pid == pid) {
return appProcess.processName
}
}
return null
private boolean isAvilible( Context context, String packageName ){
final PackageManager packageManager = context.getPackageManager()
// 获取所有已安装程序的包信息
List<PackageInfo>pinfo = packageManager.getInstalledPackages(0)
for ( int i = 0i <pinfo.size()i++ )
{
if(pinfo.get(i).packageName.equalsIgnoreCase(packageName))
return true
}
return false
}
然后检测是否安装:
if(isAvilible(MainActivity.this, "com.tencent.mm")){
Intent i = new Intent()
ComponentName cn = new ComponentName("com.tencent.mm",
"com.tencent.mm.WeiXinActivity"正散)
i.setComponent(cn)
startActivityForResult(i, RESULT_OK)
}
//未安装哪悔,跳转至market下李清正载该程序
else {
Uri uri = Uri.parse("market://details?id=com.tencent.mm")
// 直接从指定网址下载
// Uri uri = Uri.parse("http://dldir1.qq.com/foxmail/weixin521android400.apk")
Intent it = new Intent(Intent.ACTION_VIEW, uri)
startActivity(it)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)