首页 > 解决方案 > OnLocationChanged 永远不会在后台定位服务中被调用。融合定位API

问题描述

我正在尝试构建一个跟踪器应用程序并将当前位置以及背景中的时间和日期发布到我的网站。问题是 onlocationchanged 函数永远不会被调用,即使相同的代码在前台工作而不是在服务中工作。

MainActivity.java

package time.real.identify.location.tracker.gps.saksham.com.emptracker;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private static final int PERMISSIONS_REQUEST = 100;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   int permission = ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION);
    if (permission == PackageManager.PERMISSION_GRANTED) {

        startService(new Intent(this, TrackingService.class));
    } else {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                PERMISSIONS_REQUEST);
    }
    Toast.makeText(this, "GPS tracking enabled", Toast.LENGTH_SHORT).show();
    finish();
}
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[]
        grantResults) {

    if (requestCode == PERMISSIONS_REQUEST && grantResults.length == 1
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    } else {
        finish();
    }
}
}

跟踪服务.java

import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.os.StrictMode;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;


public class TrackingService extends Service implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, LocationListener {
    GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest;
    private long UPDATE_INTERVAL = 5000;
    int i =0;
    double llo=0;
    double lla=0;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }
    @Override
    public void onCreate(){
        super.onCreate();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
        mGoogleApiClient.connect();
    }
    @Override
    public void onConnected(@Nullable Bundle bundle) {
        Log.e("Loc","Connected");
  startLocationUpdates();
  Log.e("Loc","Started Location Updates!");
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.e("Loc","Connection suspended");

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Log.e("Loc","Unable to connect");
    }
    @Override
    public void onLocationChanged(Location location) {
       Log.e("Loc","onLocationChanged");
       if(location!=null) {
           Log.e("Loc", "Latitude : " + location.getLatitude() + " , Longitude : " + location.getLongitude());
           double la = location.getLatitude();
           double lo = location.getLongitude();
           if (i == 1) {
               double da = la - lla;
               double d = lo - llo;
               if ((da > 0.0001 || da < -0.0001) || (d > 0.0001 || d < -0.0001)) {
                   lla = la;
                   llo = lo;
                   String l = la + "N," + lo + "E" + "/";
                   SimpleDateFormat postFormater = new SimpleDateFormat("dd,MM,yyyy");

                   SimpleDateFormat postFormaterTime = new SimpleDateFormat("hh,mm,ss");

                   String newDateStr = postFormater.format(new Date().getTime());
                   String newTimeStr = postFormaterTime.format(new Date().getTime());
                   String link = "/*My website*/location=" + l + "&area=" + getarea(la, lo) + "&date=" + newDateStr + "&time=" + newTimeStr;
                   link = link.replaceAll(" ", "%20");

                   try {
                       URL url = new URL(link);
                   } catch (MalformedURLException e) {
                       e.printStackTrace();
                   }
                   HttpClient client = new DefaultHttpClient();
                   HttpGet request = new HttpGet();
                   try {
                       request.setURI(new URI(link));
                   } catch (URISyntaxException e) {
                       e.printStackTrace();
                   }
                   try {
                       client.execute(request);
                   } catch (IOException e) {
                       e.printStackTrace();
                   }
               }
           } else if (i == 0) {
               String l = la + "N," + lo + "E" + "/";
               lla = la;
               llo = lo;
               SimpleDateFormat postFormater = new SimpleDateFormat("dd,MM,yyyy");

               SimpleDateFormat postFormaterTime = new SimpleDateFormat("HH,mm,ss");

               String newDateStr = postFormater.format(new Date().getTime());
               String newTimeStr = postFormaterTime.format(new Date().getTime());
               String link = "/*MyWebsite*/location=" + l + "&area=" + getarea(la, lo) + "&date=" + newDateStr + "&time=" + newTimeStr;
               link = link.replaceAll(" ", "%20");

               try {
                   URL url = new URL(link);
               } catch (MalformedURLException e) {
                   e.printStackTrace();
               }
               HttpClient client = new DefaultHttpClient();
               HttpGet request = new HttpGet();
               try {
                   request.setURI(new URI(link));
               } catch (URISyntaxException e) {
                   e.printStackTrace();
               }
               try {
                   client.execute(request);
               } catch (IOException e) {
                   e.printStackTrace();
               }
               i = 1;
           }
       }
    }
    protected void startLocationUpdates() {
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(getApplicationContext(), "Enable Permissions", Toast.LENGTH_LONG).show();
        }
        mLocationRequest = new LocationRequest();
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(UPDATE_INTERVAL);

        mLocationRequest.setExpirationDuration(15);
        LocationServices.FusedLocationApi.requestLocationUpdates(
                mGoogleApiClient, mLocationRequest, this);
        Location l = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (l != null) {
            String lon = String.valueOf(l.getLongitude());
            String lat = String.valueOf(l.getLatitude());
            Log.e("Loc", "Initial :- " + lon + "," + lat);
        }
    }
    public String getarea(double latitude,double longitude){
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

        StrictMode.setThreadPolicy(policy);
//        String url = "https://geocode.xyz/"+latitude+","+longitude+"?geoit=xml"+"&auth=967851380791825982176x1596";
        String url = "https://geocode.xyz/"+latitude+","+longitude+"?geoit=xml";
        String area="",city="",country="";
        URL url1 = null;
        try {
            url1 = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        URLConnection conn = null;
        try {
            conn = url1.openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        try {
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        Document doc = null;
        try {
            doc = builder.parse(conn.getInputStream());
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if((doc.getDocumentElement().getElementsByTagName("staddress") == null) || (doc.getDocumentElement().getElementsByTagName("state") == null) || (doc.getDocumentElement().getElementsByTagName("country") == null)){
            return "Failed to get Location";
        }
        NodeList list = doc.getDocumentElement().getElementsByTagName("staddress");
        if (list != null && list.getLength() > 0) {
            NodeList subList = list.item(0).getChildNodes();

            if (subList != null && subList.getLength() > 0) {
                area = subList.item(0).getNodeValue();
            }
        }
        list = doc.getDocumentElement().getElementsByTagName("state");
        if (list != null && list.getLength() > 0) {
            NodeList subList = list.item(0).getChildNodes();

            if (subList != null && subList.getLength() > 0) {
                city = subList.item(0).getNodeValue();
            }
        }
        list = doc.getDocumentElement().getElementsByTagName("country");
        if (list != null && list.getLength() > 0) {
            NodeList subList = list.item(0).getChildNodes();

            if (subList != null && subList.getLength() > 0) {
                country = subList.item(0).getNodeValue();
            }
        }
        String a = area + "," + city + "," + country;
        return a;
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="time.real.identify.location.tracker.gps.saksham.com.emptracker">

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
<receiver android:name=".Start_on_Boot">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
        <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
    </intent-filter>
</receiver>

        <service
            android:name=".TrackingService"
            android:enabled="true"
            android:exported="true" />

    </application>

</manifest>

Start_on_Boot.java

package time.real.identify.location.tracker.gps.saksham.com.emptracker;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
 * Created by saksh on 25-03-2020.
 */

public class Start_on_Boot extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent){
        if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())){
            Intent i = new Intent(context, MainActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
    }
}

标签: javaandroid

解决方案


推荐阅读