持续后台更新位置

Continuous background update location

我希望我的应用在后台连续更新位置。我已经实现了下面的代码。

 locationManager.pausesLocationUpdatesAutomatically =false
 self.locationManager = CLLocationManager()
 locationManager.requestWhenInUseAuthorization()
 locationManager.delegate = self
 locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
 locationManager.allowsBackgroundLocationUpdates=true
 locationManager.startMonitoringSignificantLocationChanges()
 self.locationManager.startUpdatingLocation()

您需要调用 requestAlwaysAuthorization() 才能接收位置更新,即使您的应用 运行 在后台也是如此。 requestWhenInUseAuthorization() 仅授权您在您的应用 运行 在前台时接收位置更新。

public class LocationUpdatesService extends Service {

    private static final String PACKAGE_NAME =
            "com.jbs.gpteam.locationtracking.tracking2";

    private static final String TAG = LocationUpdatesService.class.getSimpleName();

    /**
     * The name of the channel for notifications.
     */
    private static final String CHANNEL_ID = "channel_01";

    public static final String ACTION_BROADCAST = PACKAGE_NAME + ".broadcast";

    public static final String EXTRA_LOCATION = PACKAGE_NAME + ".location";
    private static final String EXTRA_STARTED_FROM_NOTIFICATION = PACKAGE_NAME +
            ".started_from_notification";

    private final IBinder mBinder = new LocalBinder();
    List<TrackDetail> tempListtrack = new ArrayList<TrackDetail>();
    /**
     * The desired interval for location updates. Inexact. Updates may be more or less frequent.
     */
    private static final long UPDATE_INTERVAL_IN_MILLISECONDS = 60000;

    /**
     * The fastest rate for active location updates. Updates will never be more frequent
     * than this value.
     */
    private static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS =
            UPDATE_INTERVAL_IN_MILLISECONDS / 2;

    /**
     * The identifier for the notification displayed for the foreground service.
     */
    private static final int NOTIFICATION_ID = 12345678;
    DatabaseHelper sql;
    /**
     * Used to check whether the bound activity has really gone away and not unbound as part of an
     * orientation change. We create a foreground service notification only if the former takes
     * place.
     */
    private boolean mChangingConfiguration = false;

    private NotificationManager mNotificationManager;

    /**
     * Contains parameters used by {@link com.google.android.gms.location.FusedLocationProviderApi}.
     */
    private LocationRequest mLocationRequest;

    /**
     * Provides access to the Fused Location Provider API.
     */
    private FusedLocationProviderClient mFusedLocationClient;

    /**
     * Callback for changes in location.
     */
    private LocationCallback mLocationCallback;

    private Handler mServiceHandler;

    /**
     * The current location.
     */
    public static Location mLocation;
    LocationManager locationManager;
    CharSequence text;
    List<TrackDetail2> tempListtrack2 = new ArrayList<TrackDetail2>();
    public static final String ACTION_LOCATION_BROADCAST = LocationUpdatesService.class.getName() + "LocationBroadcast";
    public static final String EXTRA_LATITUDE = "extra_latitude";
    public static final String EXTRA_LONGITUDE = "extra_longitude";

    public LocationUpdatesService() {
    }

    @SuppressLint("WrongConstant")
    @Override
    public void onCreate() {
        locationManager = (LocationManager) getApplicationContext().getSystemService(FirebaseAnalytics.Param.LOCATION);
        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

        mLocationCallback = new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {
                super.onLocationResult(locationResult);
                if (locationManager.isProviderEnabled("gps")) {
                    text = "Location Enable...";
                }else {
                    text = "Location Disable...";
                }
                onNewLocation(locationResult.getLastLocation());
            }
        };

        createLocationRequest();
        getLastLocation();

        HandlerThread handlerThread = new HandlerThread(TAG);
        handlerThread.start();
        mServiceHandler = new Handler(handlerThread.getLooper());
        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        // Android O requires a Notification Channel.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = getString(R.string.app_name);
            // Create the channel for the notification
            NotificationChannel mChannel =
                    new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_DEFAULT);

            // Set the Notification Channel for the Notification Manager.
            mNotificationManager.createNotificationChannel(mChannel);
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "Service started");
        boolean startedFromNotification = intent.getBooleanExtra(EXTRA_STARTED_FROM_NOTIFICATION,
                false);

        // We got here because the user decided to remove location updates from the notification.
        if (startedFromNotification) {
            removeLocationUpdates();
            stopSelf();
        }
        // Tells the system to not try to recreate the service after it has been killed.
        return START_NOT_STICKY;
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mChangingConfiguration = true;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // Called when a client (MainActivity in case of this sample) comes to the foreground
        // and binds with this service. The service should cease to be a foreground service
        // when that happens.
        Log.i(TAG, "in onBind()");
        boolean startedFromNotification = intent.getBooleanExtra(EXTRA_STARTED_FROM_NOTIFICATION,
                false);

        // We got here because the user decided to remove location updates from the notification.
        stopForeground(true);
        if (startedFromNotification) {
            removeLocationUpdates();
            stopSelf();
        }
        mChangingConfiguration = false;
        return mBinder;
    }

    @Override
    public void onRebind(Intent intent) {
        // Called when a client (MainActivity in case of this sample) returns to the foreground
        // and binds once again with this service. The service should cease to be a foreground
        // service when that happens.
        Log.i(TAG, "in onRebind()");
        stopForeground(true);
        mChangingConfiguration = false;
        super.onRebind(intent);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.i(TAG, "Last client unbound from service");

        // Called when the last client (MainActivity in case of this sample) unbinds from this
        // service. If this method is called due to a configuration change in MainActivity, we
        // do nothing. Otherwise, we make this service a foreground service.
        if (!mChangingConfiguration && Utils.requestingLocationUpdates(this)) {
            Log.i(TAG, "Starting foreground service");

            Location location = intent.getParcelableExtra(LocationUpdatesService.EXTRA_LOCATION);
            if (location != null) {
                Toast.makeText(this, Utils.getLocationText(location),
                        Toast.LENGTH_SHORT).show();

                Log.d("tag location ::", "location :: " + Utils.getLocationText(location));
                onLocationChanged(location);
            }
            /*
            // TODO(developer). If targeting O, use the following code.
            if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) {
                mNotificationManager.startServiceInForeground(new Intent(this,
                        LocationUpdatesService.class), NOTIFICATION_ID, getNotification());
            } else {
                startForeground(NOTIFICATION_ID, getNotification());
            }
             */

            startForeground(NOTIFICATION_ID, getNotification());
        }
        return true; // Ensures onRebind() is called when a client re-binds.
    }

    @Override
    public void onDestroy() {
        mServiceHandler.removeCallbacksAndMessages(null);
    }

    /**
     * Makes a request for location updates. Note that in this sample we merely log the
     * {@link SecurityException}.
     */
    public void requestLocationUpdates() {
        Log.i(TAG, "Requesting location updates");
        Utils.setRequestingLocationUpdates(this, true);
        startService(new Intent(getApplicationContext(), LocationUpdatesService.class));
        try {
            mFusedLocationClient.requestLocationUpdates(mLocationRequest,
                    mLocationCallback, Looper.myLooper());
        } catch (SecurityException unlikely) {
            Utils.setRequestingLocationUpdates(this, false);
            Log.e(TAG, "Lost location permission. Could not request updates. " + unlikely);
        }
    }

    /**
     * Removes location updates. Note that in this sample we merely log the
     * {@link SecurityException}.
     */
    public void removeLocationUpdates() {
        Log.i(TAG, "Removing location updates");
        try {
            mFusedLocationClient.removeLocationUpdates(mLocationCallback);
            Utils.setRequestingLocationUpdates(this, false);
            stopSelf();
        } catch (SecurityException unlikely) {
            Utils.setRequestingLocationUpdates(this, true);
            Log.e(TAG, "Lost location permission. Could not remove updates. " + unlikely);
        }
    }

    /**
     * Returns the {@link NotificationCompat} used as part of the foreground service.
     */
    private Notification getNotification() {
        CharSequence text;
        Intent intent = new Intent(this, LocationUpdatesService.class);
        if (locationManager.isProviderEnabled("gps")) {
            text = "Location Enable...";
        }else {
            text = "Location Disable...";
        }
        CharSequence text1 = Utils.getLocationText(mLocation);


        // Extra to help us figure out if we arrived in onStartCommand via the notification or not.
        intent.putExtra(EXTRA_STARTED_FROM_NOTIFICATION, true);

        // The PendingIntent that leads to a call to onStartCommand() in this service.
        PendingIntent servicePendingIntent = PendingIntent.getService(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        // The PendingIntent to launch activity.
        PendingIntent activityPendingIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, MainActivity.class), 0);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                /*.addAction(R.drawable.ic_launch, getString(R.string.launch_activity),
                        activityPendingIntent)
                .addAction(R.drawable.ic_cancel, getString(R.string.remove_location_updates),
                        servicePendingIntent)*/
                /*.setContentText(text1+"  "+text)*/
                .setContentTitle(getString(R.string.app_name)+", Your Day Started...")
                .setOngoing(true)
                .setPriority(Notification.PRIORITY_HIGH)
                .setSmallIcon(R.drawable.ic_notificationdownload)
                .setTicker(text)
                .setWhen(System.currentTimeMillis());

        // Set the Channel ID for Android O.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId(CHANNEL_ID); // Channel ID
        }

        return builder.build();
    }

    private void getLastLocation() {
        try {
            mFusedLocationClient.getLastLocation()
                    .addOnCompleteListener(new OnCompleteListener<Location>() {
                        @Override
                        public void onComplete(@NonNull Task<Location> task) {
                            if (task.isSuccessful() && task.getResult() != null) {
                                mLocation = task.getResult();
                                Log.d("tag1 last loction :::", "location :: ");
                                onLocationChanged(mLocation);
                            } else {
                                Log.w(TAG, "Failed to get location.");
                            }
                        }
                    });
        } catch (SecurityException unlikely) {
            Log.e(TAG, "Lost location permission." + unlikely);
        }
    }

    private void onNewLocation(Location location) {
        Log.i(TAG, "New location: " + location);

        mLocation = location;

        // Notify anyone listening for broadcasts about the new location.
        Intent intent = new Intent(ACTION_BROADCAST);
        intent.putExtra(EXTRA_LOCATION, location);
        LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
        onLocationChanged(mLocation);
        // Update notification content if running as a foreground service.
        if (serviceIsRunningInForeground(this)) {
            mNotificationManager.notify(NOTIFICATION_ID, getNotification());
        }
    }


    public void onLocationChanged(final Location location) {
        mLocation=location;
        final Auth auth = new Auth(getApplicationContext());
        Location old_loc = auth.getLotLong();
        Log.d("tagg", "LOCATION CHANGED : 1  " + location.getLatitude() + "," + location.getLongitude() + "-" + new Date().getTime());

        if ((old_loc == null || location.distanceTo(old_loc) >= 250.0f) && Helper.getBooleanValue(getApplicationContext(), "isdaystart")) {
            Log.d("tagg", "LOCATION CHANGED : " + location.getLatitude() + "," + location.getLongitude() + "-" + new Date().getTime());
            String formattedDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(Calendar.getInstance().getTime());

            Log.d("taggg", "bettery leveeeeeeeeel : " + getBatteryLevel());
            if (locationManager.isProviderEnabled("gps")) {
                tempListtrack2.add(new TrackDetail2(location.getLatitude() + "", location.getLongitude() + "", formattedDate, 1, String.valueOf(getBatteryLevel())));
            } else {
                tempListtrack2.add(new TrackDetail2(location.getLatitude() + "", location.getLongitude() + "", formattedDate, 0, String.valueOf(getBatteryLevel())));
            }
            GetManageTracking2List(locationManager.isProviderEnabled("gps"), location.getLatitude(), location.getLongitude());

            auth.setLatLong(location.getLatitude(), location.getLongitude());

        }
        boolean is = false;
        is = locationManager.isProviderEnabled("gps");
        if (Helper.getBooleanValue(getApplicationContext(), "isdaystart")) {
            sendOnOff(is, String.valueOf(location.getLatitude()), String.valueOf(location.getLongitude()));
        }


        if (NetworkUtil.getConnectivityStatus(getApplicationContext()) == 0) {
            String formattedDate1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(Calendar.getInstance().getTime());
            if (!(location.getLatitude() == 0.0d || location.getLongitude() == 0.0d)) {
                if ((old_loc == null || location.distanceTo(old_loc) >= 250.0f) && Helper.getBooleanValue(getApplicationContext(), "isdaystart")) {
                    sql = new DatabaseHelper(getApplicationContext());
                    sql.open();
                    sql.insert(formattedDate1, location.getLatitude() + "", location.getLongitude() + "");
                    sql.close();
                    auth.setLatLong(location.getLatitude(), location.getLongitude());
                }
            }
            new Handler(getMainLooper()).post(new C06792());
        } else {
            Log.d("___DATA___", "DT : " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(Calendar.getInstance().getTime()) + "LOC : " + location.getLatitude() + "-" + location.getLongitude());
            if (!(location.getLatitude() == 0.0d || location.getLongitude() == 0.0d)) {
                if ((old_loc == null || location.distanceTo(old_loc) >= 250.0f) && Helper.getBooleanValue(getApplicationContext(), "isdaystart")) {
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                    String formattedDate2 = df.format(Calendar.getInstance().getTime());
                    tempListtrack.add(new TrackDetail(String.valueOf(location.getLatitude()), String.valueOf(location.getLongitude()), formattedDate2));

                    GetManageTrackingList(tempListtrack);
                    auth.setLatLong(location.getLatitude(), location.getLongitude());
                }
            }

        }
    }

    class C06792 implements Runnable {
        C06792() {
        }

        @SuppressLint("WrongConstant")
        public void run() {
            if (Helper.getBooleanValue(getApplicationContext(), "isdaystart"))
                Toast.makeText(LocationUpdatesService.this.getApplicationContext(), "Please Enable GPS.", 0).show();
        }
    }

    private float getBatteryLevel() {
        Intent batteryStatus = registerReceiver(null, new IntentFilter("android.intent.action.BATTERY_CHANGED"));
        int batteryLevel = -1;
        int batteryScale = 1;
        if (batteryStatus != null) {
            batteryLevel = batteryStatus.getIntExtra(FirebaseAnalytics.Param.LEVEL, -1);
            batteryScale = batteryStatus.getIntExtra("scale", 1);
        }
        return (((float) batteryLevel) / ((float) batteryScale)) * 100.0f;
    }

    private void GetManageTrackingList(List<TrackDetail> tempListtrack) {
        //final AlertDialog dialog = Utils.dialogProgress(getActivity());        dialog.show(); dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        GetManageEmployeeTrackingReqEnvelope requestEnvelope = new GetManageEmployeeTrackingReqEnvelope();
        final String deviceId = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
        Log.d("tag ::", "device id :" + deviceId);
        Date c = Calendar.getInstance().getTime();
        System.out.println("Current time => " + c);

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        String formattedDate = df.format(c);
        System.out.println(formattedDate);
        Date date = null;
        try {
            date = df.parse(formattedDate);
            System.out.println(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        GetManageEmployeeTrackingReqBody requestBody = null;

        requestBody = new GetManageEmployeeTrackingReqBody(tempListtrack);


        RequestHeader requestHeader = new RequestHeader(Helper.getStringValue(this, "mobile"), Helper.getStringValue(this, "password"), Helper.getStringValue(this, "fcmToken"), Helper.getStringValue(this, "deviceId"));

        requestEnvelope.setHeader(requestHeader);
        requestEnvelope.setZbody(requestBody);
        Call<GetManageEmployeeTrackingResEnvelope> responseEnvelopeCall = BhanuSamajApiImpl.getApi().getManageEmployeeTracking(requestEnvelope);
        responseEnvelopeCall.enqueue(new Callback<GetManageEmployeeTrackingResEnvelope>() {
            @Override
            public void onResponse(Call<GetManageEmployeeTrackingResEnvelope> call, Response<GetManageEmployeeTrackingResEnvelope> response) {
              /*  if (dialog.isShowing()) {
                    dialog.dismiss();
                }*/
                try {
                    if (response != null) {
                        Log.d("tag", "response-->>>" + response);
                        GetManageEmployeeTrackingData data = ((GetManageEmployeeTrackingResEnvelope) response.body()).getBody().getEmployeeTrackingV2Response();


                        ObjectMapper mapper = new ObjectMapper();
                        EmployeeTrackingResponse employeeTrackingResponse = new EmployeeTrackingResponse();
                        employeeTrackingResponse = mapper.readValue(data.getEmployeeTrackingV2Result().toString(), EmployeeTrackingResponse.class);
/*
                        if (employeeTrackingResponse.getTrackingResponse().getCode().equals("1")) {
                            sql.deleteLatlong();
//                            EmployeeTracking fragment = new EmployeeTracking();
//                            replaceFragmentWithoutBackstackWithClearAll(fragment, "EmployeeTracking");
                            //Toast.makeText(this,""+getEmployeeTrackingResponse.getNewEmployeeTrackingForDealer().getMessage(),Toast.LENGTH_LONG).show();
                        }*/
                        LocationUpdatesService.this.tempListtrack.clear();
                        //  Log.d("tag", "CODE ::; " + myProfileResponse.getEmployeeAuth().getCode());
                        Log.d("tag", "response :: " + data.getEmployeeTrackingV2Result().toString());

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<GetManageEmployeeTrackingResEnvelope> call, Throwable t) {
                //     dialog.dismiss();
                t.printStackTrace();
            }

        });

    }

    private void GetManageTracking2List(boolean isEnable) {
        //final AlertDialog dialog = Utils.dialogProgress(getActivity());        dialog.show(); dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        GetManageEmployeeTracking2ReqEnvelope requestEnvelope = new GetManageEmployeeTracking2ReqEnvelope();
        final String deviceId = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
        Log.d("tag ::", "device id :1--" + deviceId);
        Date c = Calendar.getInstance().getTime();
        System.out.println("Current time => " + c);

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        String formattedDate = df.format(c);
        System.out.println(formattedDate);
        Date date = null;
        try {
            date = df.parse(formattedDate);
            System.out.println(date);

            GetManageEmployeeTracking2ReqBody requestBody;
            EmployeeTracking2RequestHeader requestHeader;
            if (isEnable) {
                requestHeader = new EmployeeTracking2RequestHeader(Helper.getStringValue(this, "mobile"), Helper.getStringValue(this, "password"), Helper.getStringValue(this, "fcmToken"), Helper.getStringValue(this, "deviceId"), 1);
            } else {
                requestHeader = new EmployeeTracking2RequestHeader(Helper.getStringValue(this, "mobile"), Helper.getStringValue(this, "password"), Helper.getStringValue(this, "fcmToken"), Helper.getStringValue(this, "deviceId"), 2);
            }

            requestBody = new GetManageEmployeeTracking2ReqBody(tempListtrack2, Integer.parseInt(0 + ""));

            requestEnvelope.setHeader(requestHeader);
            requestEnvelope.setZbody(requestBody);
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Call<GetManageEmployeeTracking2ResEnvelope> responseEnvelopeCall = BhanuSamajApiImpl.getApi().getManageEmployeeTracking2(requestEnvelope);
        responseEnvelopeCall.enqueue(new Callback<GetManageEmployeeTracking2ResEnvelope>() {
            @Override
            public void onResponse(Call<GetManageEmployeeTracking2ResEnvelope> call, Response<GetManageEmployeeTracking2ResEnvelope> response) {
                //dialog.dismiss();
                if (response != null) {
                    Log.d("tag", "response-->>>" + response);
                    try {
                        GetManageEmployeeTracking2Data data = ((GetManageEmployeeTracking2ResEnvelope) response.body()).getBody().getEmployeeTracking2V2Response();

                        ObjectMapper mapper = new ObjectMapper();
                        EmployeeTracking2Response employeeTracking2Response = new EmployeeTracking2Response();
                        employeeTracking2Response = mapper.readValue(data.getEmployeeTracking2V2Result().toString(), EmployeeTracking2Response.class);

                        tempListtrack2.clear();
                        //  Log.d("tag", "CODE ::; " + myProfileResponse.getEmployeeAuth().getCode());
                        Log.d("tag", "response :: " + data.getEmployeeTracking2V2Result().toString());

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Call<GetManageEmployeeTracking2ResEnvelope> call, Throwable t) {
                //     dialog.dismiss();
                t.printStackTrace();
            }
        });
    }

    public void sendOnOff(final boolean isEnable, String lat, String lng) {

        Intent intent = new Intent(ACTION_LOCATION_BROADCAST);
        intent.putExtra(EXTRA_LATITUDE, lat);
        intent.putExtra(EXTRA_LONGITUDE, lng);
        sendBroadcast(intent);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        String formattedDate = df.format(Calendar.getInstance().getTime());
        if (isEnable) {
            tempListtrack2.add(new TrackDetail2("0", "0", formattedDate, 1, String.valueOf(getBatteryLevel())));
        } else {
            tempListtrack2.add(new TrackDetail2("0", "0", formattedDate, 0, String.valueOf(getBatteryLevel())));
        }

        GetManageTracking2List(isEnable);
    }

    /**
     * Sets the location request parameters.
     */
    private void createLocationRequest() {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
        mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }

    /**
     * Class used for the client Binder.  Since this service runs in the same process as its
     * clients, we don't need to deal with IPC.
     */
    public class LocalBinder extends Binder {
        public LocationUpdatesService getService() {
            return LocationUpdatesService.this;
        }
    }

    /**
     * Returns true if this is a foreground service.
     *
     * @param context The {@link Context}.
     */
    public boolean serviceIsRunningInForeground(Context context) {
        ActivityManager manager = (ActivityManager) context.getSystemService(
                Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(
                Integer.MAX_VALUE)) {
            if (getClass().getName().equals(service.service.getClassName())) {
                if (service.foreground) {
                    return true;
                }
            }
        }
        return false;
    }
}