[英]Is there any way to check if a twitter app is running in the background
我想知道是否有办法检查Twitter应用程序是否在后台运行。
You can use the Running Processes: 您可以使用正在运行的进程:
ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
for(int i = 0; i < procInfos.size(); i++){
if(procInfos.get(i).processName.equals("com.twitter.android")) {
Toast.makeText(getApplicationContext(), "Twitter is running", Toast.LENGTH_LONG).show();
}
}
I'm not entirely sure why you would want to know this though. 我不完全确定为什么你会想知道这个。
EDIT: 编辑:
Since you only need to know if it is installed, this code will suit you better: 由于您只需要知道它是否已安装,因此此代码更适合您:
private boolean isTwitterInstalled() {
PackageManager pm = getPackageManager();
boolean twitter_installed = false;
try {
pm.getPackageInfo("com.twitter.android", PackageManager.GET_ACTIVITIES);
twitter_installed = true;
} catch (PackageManager.NameNotFoundException e) {
twitter_installed = false;
}
return app_installed;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.