2013-05-08 22 views
1

我想知道是否有任何方法来检查一个twitter应用是否在后台运行。有没有什么办法来检查一个twitter应用是否在后台运行

+0

你可以有点更具描述性的,你要完成什么? – Karakuri 2013-05-08 05:49:29

+0

http://stackoverflow.com/questions/3667022/android-is-application-running-in-background/5862048#5862048。检查链接 – Raghunandan 2013-05-08 05:51:01

+0

@Karakuri。在我的应用程序中,我只想显示在用户已经登录的手机中是否有Twitter应用程序。 – Abx 2013-05-08 05:51:42

回答

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; 
} 
+0

在我的应用程序中有一个规定,用户登录到Twitter,除非有一个预先twitter应用程序instaled.if已经有一个twitter应用程序安装然后用户不需要使用我的应用程序的提供,本地应用程序将被使用。 – Abx 2013-05-08 05:55:23

+1

@Abhilash在这种情况下查看我的编辑。第二个片段会告诉你它是否已安装,但不能正常运行。但是,您无法确定用户是否已登录。 – 2013-05-08 05:59:12

+0

不幸的是,这依赖于了解Twitter应用程序的包名,并且只有在拥有官方Twitter应用程序时才有效。 – Karakuri 2013-05-08 06:01:39

相关问题