package com.tencent.qcloud.tim.demo;
import android.app.ActivityManager;
import android.content.Context;
import android.util.Log;
import java.util.List;
public class SessionWrapper {
private static final String TAG = SessionWrapper.class.getSimpleName();
public static boolean isMainProcess(Context context) {
ActivityManager am = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE));
String mainProcessName = context.getPackageName();
int myPid = android.os.Process.myPid();
List<ActivityManager.RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
if (processInfos == null) {
Log.i(TAG, "isMainProcess get getRunningAppProcesses null");
List<ActivityManager.RunningServiceInfo> processList = am.getRunningServices(Integer.MAX_VALUE);
if (processList == null) {
Log.i(TAG, "isMainProcess get getRunningServices null");
return false;
}
for (ActivityManager.RunningServiceInfo rsi : processList) {
if (rsi.pid == myPid && mainProcessName.equals(rsi.service.getPackageName())) {
return true;
}
}
return false;
}
for (ActivityManager.RunningAppProcessInfo info : processInfos) {
if (info.pid == myPid && mainProcessName.equals(info.processName)) {
return true;
}
}
return false;
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。