我想知道是否有办法检查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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.