我正在尝试检查是否有另一个我的应用程序正在运行。启动第二个实例时,我总是发现我的应用程序没有运行。检查的代码是:
public String NameActivity() {
ActivityManager am = (ActivityManager)
context.getSystemService(context.ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = this.context.getPackageManager();
while (i.hasNext()) {
ActivityManager.RunningAppProcessInfo info =
(ActivityManager.RunningAppProcessInfo) (i.next());
try {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(
info.processName, PackageManager.GET_META_DATA));
Log.w("LABEL", c.toString());
} catch (Exception e) {
//Name Not FOund Exception
}
}
return null;
}
public static boolean isThisProcessRunning(Context context, String processName) {
if (processName == null) {
return false;
}
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> processes = manager.getRunningAppProcesses();
for (RunningAppProcessInfo process : processes) {
if (processName.equals(process.processName)) {
return true;
}
}
return false;
}
}
我希望你能帮助我。