检查谷歌地图是否在android

时间:2016-12-09 10:00:39

标签: android google-maps

I have created an android application and there is an icon which take me to google map. I have created a service which is checking google map is visible or not. I am using below code to find the google map but this code is not returning google map package. I am returning my application package name on map screen. 

我也使用了getRunninTask方法,但此方法也没有返回googlemap包

MyApplication.java

private boolean isAppOnForeground(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();

    if (appProcesses == null) {
        return false;
    }
    final String packageName = "com.google.android.apps.maps";
    for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
        Log.i(TAG, appProcess.processName);
        if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
            return true;
        }
    }
    return false;
}

How would i check google map is in forground or not? 

1 个答案:

答案 0 :(得分:0)

您可以尝试使用以下代码:

public class MyApplication extends Application {

  @Override
  public void onCreate() {
    super.onCreate();
    mInstance = this;
  } 

  public static MyApplication getInstance(){
    return mInstance;
  }

  public static boolean isActivityVisible() {
    return activityVisible;
  }  

  public static void activityResumed() {
    activityVisible = true;
  }

  public static void activityPaused() {
    activityVisible = false;
  }

  private static boolean activityVisible;
  private static MyApplication mInstance;
}

在你的Manifest中也包括这个:

<application
    android:name="your.app.package.MyApplication"
    android:icon="@drawable/icon"
    android:label="@string/app_name" >

然后在你的活动中使用它:

 @Override
 public void onPause(){
     MyApplication.getInstance().activityPaused();
 }

 @Override
 public void onResume(){
    MyApplication.getInstance().activityResumed();
 }