Java 如何检查应用程序是否在 Android 上运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4212992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I check if an app running on Android?
提问by sjor
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?
我是一名 Android 开发人员,我想if
在我的应用程序中写一个声明。在此语句中,我想检查默认浏览器(Android 操作系统中的浏览器)是否正在运行。如何以编程方式执行此操作?
采纳答案by dhaval
Add the below Helper class:
添加以下 Helper 类:
public class Helper {
public static boolean isAppRunning(final Context context, final String packageName) {
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
final List<ActivityManager.RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
if (procInfos != null)
{
for (final ActivityManager.RunningAppProcessInfo processInfo : procInfos) {
if (processInfo.processName.equals(packageName)) {
return true;
}
}
}
return false;
}
}
Now you can check from the below code if your desired App is running or not:
现在您可以从下面的代码中检查您想要的应用程序是否正在运行:
if (Helper.isAppRunning(YourActivity.this, "com.your.desired.app")) {
// App is running
} else {
// App is not running
}
回答by Android Dev
You can check it by the following method
您可以通过以下方法进行检查
public static boolean isRunning(Context ctx) {
ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (ActivityManager.RunningTaskInfo task : tasks) {
if (ctx.getPackageName().equalsIgnoreCase(task.baseActivity.getPackageName()))
return true;
}
return false;
}
回答by Rajneesh Shukla
isInBackground
is the status of app
isInBackground
是应用程序的状态
ActivityManager.RunningAppProcessInfo myProcess = new ActivityManager.RunningAppProcessInfo();
ActivityManager.getMyMemoryState(myProcess);
Boolean isInBackground = myProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
回答by Rohit Lalwani
Best solution is :
最佳解决方案是:
Create an interface like this.
创建一个这样的界面。
interface LifeCycleDelegate {
void onAppBackgrounded();
void onAppForegrounded();
}
Now add a class that handle all activity lifecycle callbacks.
现在添加一个处理所有活动生命周期回调的类。
public class AppLifecycleHandler implements
Application.ActivityLifecycleCallbacks, ComponentCallbacks2 {
LifeCycleDelegate lifeCycleDelegate;
boolean appInForeground = false;
public AppLifecycleHandler(LifeCycleDelegate lifeCycleDelegate) {
this.lifeCycleDelegate = lifeCycleDelegate;
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
if (!appInForeground) {
appInForeground = true;
lifeCycleDelegate.onAppForegrounded();
}
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
@Override
public void onTrimMemory(int level) {
if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
// lifecycleDelegate instance was passed in on the constructor
appInForeground = false;
lifeCycleDelegate.onAppBackgrounded();
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
}
@Override
public void onLowMemory() {
}
}
Now in a class extending Application
现在在一个扩展应用程序的类中
public class MyApplication extends Application implements
LifeCycleDelegate{
@Override
public void onCreate() {
super.onCreate();
AppLifecycleHandler lifeCycleHandler = new
AppLifecycleHandler(MyApplication.this);
registerLifecycleHandler(lifeCycleHandler);
}
@Override
public void onAppBackgrounded() {
Log.d("Awww", "App in background");
}
@Override
public void onAppForegrounded() {
Log.d("Yeeey", "App in foreground");
}
private void registerLifecycleHandler(AppLifecycleHandler lifeCycleHandler) {
registerActivityLifecycleCallbacks(lifeCycleHandler);
registerComponentCallbacks(lifeCycleHandler);
}
}
For More refer : https://android.jlelse.eu/how-to-detect-android-application-open-and-close-background-and-foreground-events-1b4713784b57
更多请参考:https: //android.jlelse.eu/how-to-detect-android-application-open-and-close-background-and-foreground-events-1b4713784b57
回答by SohailAziz
fun Context.isAppInForeground(): Boolean {
val application = this.applicationContext
val activityManager = this.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val runningProcessList = activityManager.runningAppProcesses
if (runningProcessList != null) {
val myApp = runningProcessList.find { it.processName == application.packageName }
ActivityManager.getMyMemoryState(myApp)
return myApp?.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
}
return false
}