Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 连续背景更新位置_Ios_Swift_Location_Cllocationmanager - Fatal编程技术网

Ios 连续背景更新位置

Ios 连续背景更新位置,ios,swift,location,cllocationmanager,Ios,Swift,Location,Cllocationmanager,我希望我的应用程序在后台连续更新位置。我已经实现了以下代码 locationManager.pausesLocationUpdatesAutomatically =false self.locationManager = CLLocationManager() locationManager.requestWhenInUseAuthorization() locationManager.delegate = self locationManager.desiredAccuracy =

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

 locationManager.pausesLocationUpdatesAutomatically =false
 self.locationManager = CLLocationManager()
 locationManager.requestWhenInUseAuthorization()
 locationManager.delegate = self
 locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
 locationManager.allowsBackgroundLocationUpdates=true
 locationManager.startMonitoringSignificantLocationChanges()
 self.locationManager.startUpdatingLocation()
  • 此代码在
    appdelegate
    didFinishLaunchingWithOptions()中管理

您需要调用
requestAlwaysAuthorization()
才能接收位置更新,即使您的应用程序正在后台运行
requestwhenUseAuthorization()
仅授予您在应用程序在前台运行时接收位置更新的授权。

您需要调用
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;
    }
}
私有静态最终字符串包\u名称= “com.jbs.gpteam.locationtracking.tracking2”; 私有静态最终字符串标记=LocationUpdateService.class.getSimpleName(); /** *通知通道的名称。 */ 专用静态最终字符串通道\u ID=“通道\u 01”; public static final String ACTION_BROADCAST=包名称+“.BROADCAST”; 公共静态最终字符串EXTRA_LOCATION=包名称+“.LOCATION”; 私有静态最终字符串额外\u从\u通知开始\u=包\u名称+ “.从通知开始启动”; private final IBinder mBinder=new LocalBinder(); List tempListtrack=新建ArrayList(); /** *位置更新所需的间隔。不精确。更新的频率可能会增加或减少。 */ 私有静态最终长更新间隔(以毫秒为单位)=60000; /** *活动位置更新的最快速度。更新的频率将前所未有 *大于此值。 */ 私有静态最终长最快更新间隔(毫秒)= 更新间隔(单位:毫秒/2); /** *为前台服务显示的通知的标识符。 */ 私有静态最终整数通知_ID=12345678; 数据库辅助sql; /** *用于检查绑定的活动是否确实已离开,而不是作为活动的一部分解除绑定 *方向更改。只有在前一个发生更改时,我们才会创建前台服务通知 *地点。 */ 私有布尔值mChangingConfiguration=false; 私人通知经理通知经理; /** *包含{@link com.google.android.gms.location.FusedLocationProviderApi}使用的参数。 */ 私人位置请求mLocationRequest; /** *提供对融合位置提供程序API的访问。 */ 私有FusedLocationProviderClient mFusedLocationClient; /** *位置更改的回调。 */ 私有位置回调mLocationCallback; 私有处理器mServiceHandler; /** *当前位置。 */ 公共静态定位; 地点经理地点经理; 字符序列文本; List tempListtrack2=新建ArrayList(); public static final String ACTION\u LOCATION\u BROADCAST=LocationUpdateService.class.getName()+“LocationBroadcast”; 公共静态最终字符串额外纬度=“额外纬度”; 公共静态最终字符串EXTRA_longide=“EXTRA_longide”; 公共位置更新服务(){ } @SuppressLint(“错误常量”) @凌驾 public void onCreate(){ locationManager=(locationManager)getApplicationContext().getSystemService(FirebaseAnalytics.Param.LOCATION); mFusedLocationClient=LocationServices.getFusedLocationProviderClient(此); mlLocationCallback=新位置Callback(){ @凌驾 public void onLocationResult(LocationResult LocationResult){ super.onLocationResult(定位结果); if(locationManager.isProviderEnabled(“gps”)){ text=“位置启用…”; }否则{ text=“位置禁用…”; } onNewLocation(locationResult.getLastLocation()); } }; createLocationRequest(); getLastLocation(); HandlerThread HandlerThread=新HandlerThread(标记); handlerThread.start(); mServiceHandler=新处理程序(handlerThread.getLooper()); mNotificationManager=(NotificationManager)getSystemService(通知服务); //Android O需要一个通知通道。 if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.O){ CharSequence name=getString(R.string.app_name); //为通知创建通道 通知通道= 新建NotificationChannel(频道ID、名称、NotificationManager.IMPORTANCE\u默认值); //设置通知管理器的通知通道。 mNotificationManager.createNotificationChannel(mChannel); } } @凌驾 公共int onStartCommand(Intent Intent、int标志、int startId){ Log.i(标记“服务已启动”); boolean startedFromNotification=intent.getBooleanExtra(EXTRA_从_通知开始, 假); //我们之所以来到这里,是因为用户决定从通知中删除位置更新。 如果(从通知开始){ RemovelocationUpdate(); stopSelf(); } //告诉系统在服务被终止后不要尝试重新创建服务。 返回开始时间不粘; } @凌驾 公共无效OnConfiguration已更改(配置newConfig){ super.onConfigurationChanged(newConfig); mChangingConfiguration=true; } @凌驾 公共IBinder onBind(意向){ //当客户端(本示例中为MainActivity)到达foregr时调用