简体   繁体   English

在Android中使用Google Play服务API和移动数据无法获取位置更新

[英]Not getting location updates using Google play services API with mobile data in android

I am making an android application to get current location via service and upload to database using Google play services fused API but the problem I am facing is that whenever I am trying to get location via my home wifi its working fine and i am getting regular location updates but when i switch to mobile data I am not getting correct updates, regular updates are there but its showing the same values of coordinates ie latitude and longitude again and again no matter hwere i am moving. 我正在制作一个Android应用程序,以通过服务获取当前位置并使用Google Play服务融合的API上传到数据库,但是我面临的问题是,每当我尝试通过家庭wifi获取位置时,它的工作状况都很好,并且我正在获取常规位置更新,但是当我切换到移动数据时,我没有得到正确的更新,那里有常规更新,但是无论我在移动什么,它一次又一次显示相同的坐标值,即纬度和经度。 Please helpme solving this issue as I am new to android development. 请帮助我解决此问题,因为我是android开发的新手。

This is my Myservices.java where i have all the necessary code: 这是我的Myservices.java,其中包含所有必需的代码:

package com.maps.saury.location;


import android.Manifest;
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.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.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

public class Myservice extends Service implements ConnectionCallbacks,
        OnConnectionFailedListener, LocationListener {


    BGTask bgtask = new BGTask(this);

    public static String data;

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

    private static Location mLastLocation;

    private GoogleApiClient mGoogleApiClient;

    private  LocationRequest mLocationRequest;

    // Location updates intervals in sec
    private static int UPDATE_INTERVAL = 100; 
    private static int FATEST_INTERVAL = 100; 
    private static int DISPLACEMENT = 0; 

    final class Mythreadclass implements Runnable {



        @Override
        public void run() {

            mLocationRequest.setInterval(UPDATE_INTERVAL);
            mLocationRequest.setFastestInterval(FATEST_INTERVAL);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            mLocationRequest.setSmallestDisplacement(DISPLACEMENT);

        }
    }



    @Override
    public void onCreate() {
        super.onCreate();

        buildGoogleApiClient();
        createLocationRequest();
        mGoogleApiClient.connect();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        if (mGoogleApiClient != null) {
            mGoogleApiClient.connect();
        }
        Thread thread = new Thread(new Mythreadclass());
        thread.start();

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
        Toast.makeText(this,"destroyed",Toast.LENGTH_SHORT).show();
        super.onDestroy();
    }


    private void displayLocation() {


       // mLastLocation = LocationServices.FusedLocationApi
         //       .getLastLocation(mGoogleApiClient);

        if (mLastLocation != null) {
            double latitude = mLastLocation.getLatitude();
            double longitude = mLastLocation.getLongitude();
            data=Double.toString(latitude)+"\n"+Double.toString(longitude);
            Toast.makeText(this,data,Toast.LENGTH_SHORT).show();

            BGTask bgtask = new BGTask(this);
            bgtask.execute(Double.toString(latitude), Double.toString(longitude));

        } else {


                    Toast.makeText(this,"Couldn't get the location. Make sure location is enabled on the device",Toast.LENGTH_SHORT).show();
        }
    }



    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API).build();
    }

    /**
     * Creating location request object
     * */
    protected void createLocationRequest() {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(UPDATE_INTERVAL);
        mLocationRequest.setFastestInterval(FATEST_INTERVAL);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
    }


    protected void startLocationUpdates() {
       mGoogleApiClient.connect();

        LocationServices.FusedLocationApi.requestLocationUpdates(
                mGoogleApiClient, mLocationRequest, this);

    }


    protected void stopLocationUpdates() {
        LocationServices.FusedLocationApi.removeLocationUpdates(
                mGoogleApiClient, this);
    }


    @Override
    public void onConnectionFailed(ConnectionResult result) {
        Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = "
                + result.getErrorCode());
    }

    @Override
    public void onConnected(Bundle arg0) {

        // Once connected with google api, get the location
        Toast.makeText(this,"connected",Toast.LENGTH_SHORT).show();
        startLocationUpdates();
    }

    @Override
    public void onConnectionSuspended(int arg0) {
        mGoogleApiClient.connect();
    }

    @Override
    public void onLocationChanged(Location location) {
        // Assign the new location
        mLastLocation = location;


        Toast.makeText(getApplicationContext(), "Locatn changed!",
                Toast.LENGTH_SHORT).show();

        // Displaying the new location on UI
        displayLocation();
    }


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

and this is my AndroidMainfest.xml : 这是我的AndroidMainfest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.maps.saury.location">
    <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"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".Myservice"/>
    </application>

</manifest>

我想当您在家庭/办公室以调试模式运行它时,位置更新就会进入,并且如果您构建应用程序的发行版并出门,那么位置更新就不会进入。如果是这种情况,那么确保为“发布”构建版本定义了正确的Google Maps API密钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过使用最新的Google Play服务API位置来停止位置更新 - Stop location updates by using the last google play services api location 使用Google Play服务的Android位置API - Android location API using Google Play Services 使用Google Play服务的Android Location API - Android Location API using Google Play Services 位置更新使用Google Play服务和FusedLocationProviderClient - Location Updates using google play services and FusedLocationProviderClient 使用Google Play服务Location API收听位置服务更新 - Using Google Play Services Location API to get listen for location services updates 使用Google Play服务进行活动识别和位置更新 - Activity Recognition and Location Updates using Google play services 使用Google Play Location API获取速度 - Getting Speed using Google Play Location API Google Play服务或Android定位服务 - Google Play Services or Android Location Services Google Play服务API(定位服务)是否需要数据计划? - Does Google Play Service API (location services) require a data plan? 使用Google Play服务位置API获取位置与使用Awareness API获取位置 - Getting location with the Google Play Services Location API vs getting it with the Awareness API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM