【发布时间】:2016-06-10 15:58:19
【问题描述】:
我正在制作一个 android 应用程序来通过服务获取当前位置并使用 Google Play 服务融合 API 上传到数据库,但我面临的问题是,每当我试图通过我的家庭 wifi 获取位置时,它工作正常,我是定期更新位置,但是当我切换到移动数据时,我没有得到正确的更新,定期更新在那里,但无论我在移动,它一次又一次地显示相同的坐标值,即纬度和经度。请帮我解决这个问题,因为我是 android 开发的新手。
这是我的 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;
}
}
这是我的 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>
【问题讨论】:
标签: android service location google-play-services