如何检查,如果一个应用程序在运行Android?应用程序、Android

2023-09-11 20:08:05 作者:帅萌的酷

我是一名Android开发者,我想一个if语句写在我的应用程序。在这次发言中,我要检查是否为默认浏览器(浏览器Android操作系统)运行。我怎样才能做到这一点编程?等待您的帮助。

I am an Android developer and I want to write an if statement in my application. In this statement I want to check if the default browser(browser in Android OS) is running. How can I do this programmatically? Waiting for your help.

推荐答案

试试这个code:

        ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
        List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
        for(int i = 0; i < procInfos.size(); i++)
        {
            if(procInfos.get(i).processName.equals("com.android.browser")) 
            {
                Toast.makeText(getApplicationContext(), "Browser is running", Toast.LENGTH_LONG).show();
            }
        }