我想知道是否有办法检查Twitter应用是否在后台运行。
答案 0 :(得分:4)
您可以使用正在运行的进程:
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();
}
}
我不完全确定为什么你会想知道这个。
编辑:
由于您只需知道它是否已安装,因此此代码更适合您:
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;
}