在android系统的GPS定位更新不工作 [英] Gps Location updates in android is not working
问题描述
从Android的官方网站我已经下载了位置服务的例子和放大器;并在真实的设备上运行。每次onLocationChanged方法返回网络的纬度和放大器;经度但不准确的GPS定位。如何实现从全新的Android位置API精确的GPS位置。
下面是我的服务,code
包com.example.locationservice;进口android.app.Service;
进口android.content.Context;
进口android.content.Intent;
进口android.location.Location;
进口android.location.LocationListener;
进口android.os.Bundle;
进口android.os.IBinder;
进口android.util.Log;进口com.google.android.gms.common.ConnectionResult;
进口com.google.android.gms.common.GooglePlayServicesClient;
进口com.google.android.gms.common.GooglePlayServicesUtil;
进口com.google.android.gms.location.LocationClient;
进口com.google.android.gms.location.LocationRequest;公共类MDFYLocationUpdate扩展服务实现
LocationListener的,
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
com.google.android.gms.location.LocationListener { LocationClient mLocationClient;
私人LocationRequest mLocationRequest;
公共静态语境℃;
最后弦乐BROADCAST_ACTION =CURRENT_LOCATION;
公共静态最后弦乐TAG =位置;
//每秒毫秒
私有静态最终诠释MILLISECONDS_PER_SECOND = 1000;
以秒为单位更新//频率
公共静态最终诠释UPDATE_INTERVAL_IN_SECONDS = 5;
以毫秒为单位//更新频率
私有静态最后的更新周期长= 10000;
// MILLISECONDS_PER_SECOND * UPDATE_INTERVAL_IN_SECONDS;
//最快的更新频率,以秒为单位
私有静态最终诠释FASTEST_INTERVAL_IN_SECONDS = 1;
//毫秒的快速频率上限
私有静态最后长FASTEST_INTERVAL =
MILLISECONDS_PER_SECOND * FASTEST_INTERVAL_IN_SECONDS; @覆盖
公众的IBinder onBind(意向意图){
// TODO自动生成方法存根
返回null;
}
@覆盖
公共无效的onCreate(){
// TODO自动生成方法存根
super.onCreate();
mLocationRequest = LocationRequest.create(); mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
//设置更新间隔为5秒
mLocationRequest.setInterval(更新周期);
//最快的更新间隔设置为1秒
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
servicesConnected();
mLocationClient =新LocationClient(这,这,这);
}
@覆盖
公共无效的onDestroy(){
// TODO自动生成方法存根
super.onDestroy();
}
@覆盖
公共无效onConnected(捆绑connectionHint){
// TODO自动生成方法存根 Log.d(TAGonconnected);
mLocationClient.requestLocationUpdates(mLocationRequest,这一点);
}
@覆盖
公共无效onDisconnected(){
// TODO自动生成方法存根
Log.d(TAGonsisconnect);
//销毁当前位置客户端
mLocationClient = NULL;
}
@覆盖
公共无效onConnectionFailed(ConnectionResult结果){
// TODO自动生成方法存根 } 私人布尔servicesConnected(){
//检查谷歌播放服务提供
INT结果code = GooglePlayServicesUtil.isGooglePlayServicesAvailable(本);
//如果谷歌Play服务的可
如果(ConnectionResult.SUCCESS ==结果code){ 返回true;
}其他{ 返回false;
}
} @覆盖
公共无效onLocationChanged(地点){
// TODO自动生成方法存根
Log.d(TAGonLocationChanged);
Log.d(TAG,纬度=+ location.getLatitude()+LNG =+ location.getLongitude());
Logger.writeLog(纬度=+ location.getLatitude()+LNG =+ location.getLongitude()+==+ BasicDeviceInformation.getCurrentDate()); }
@覆盖
公众诠释onStartCommand(意向意图,诠释标志诠释startId){
// TODO自动生成方法存根
Log.d(TAGonStartCommand);
mLocationClient.connect();
返回super.onStartCommand(意向,旗帜,startId);
}
@覆盖
公共无效onProviderDisabled(字符串提供商){
// TODO自动生成方法存根 }
@覆盖
公共无效onProviderEnabled(字符串提供商){
// TODO自动生成方法存根 }
@覆盖
公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){
// TODO自动生成方法存根 }
}
不知道用户将回到SO与否,代表他如此张贴一个答案为别人谁遇到这个问题。
PS: - 此答案由用户自己建议,但在错误的地方,所以张贴在代表他的
更新
我找到了这个问题的解决方案....我错过了元数据标签
在清单中。
低于code将在清单中的应用程序标签内。
<元数据机器人:名字=com.google.android.gms.version
机器人:值=@整数/ GOOGLE_PLAY_SERVICES_VERSION/>
将这个我得到正确的位置后。
问题已经结束的
块引用>From Android official site I have downloaded Location services example & did run in the real device. Every time onLocationChanged method is returning Network's Latitude & Longitude but not accurate GPS location. How to achieve accurate GPS locations from the new android Location API.
Here is my service code
package com.example.locationservice; import android.app.Service; import android.content.Context; import android.content.Intent; import android.location.Location; import android.location.LocationListener; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesClient; import com.google.android.gms.common.GooglePlayServicesUtil; import com.google.android.gms.location.LocationClient; import com.google.android.gms.location.LocationRequest; public class MDFYLocationUpdate extends Service implements LocationListener, GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener { LocationClient mLocationClient; private LocationRequest mLocationRequest; public static Context c; final String BROADCAST_ACTION ="current_location"; public static final String TAG="Location"; // Milliseconds per second private static final int MILLISECONDS_PER_SECOND = 1000; // Update frequency in seconds public static final int UPDATE_INTERVAL_IN_SECONDS = 5; // Update frequency in milliseconds private static final long UPDATE_INTERVAL = 10000; //MILLISECONDS_PER_SECOND * UPDATE_INTERVAL_IN_SECONDS; // The fastest update frequency, in seconds private static final int FASTEST_INTERVAL_IN_SECONDS = 1; // A fast frequency ceiling in milliseconds private static final long FASTEST_INTERVAL = MILLISECONDS_PER_SECOND * FASTEST_INTERVAL_IN_SECONDS; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); mLocationRequest =LocationRequest.create(); mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); // Set the update interval to 5 seconds mLocationRequest.setInterval(UPDATE_INTERVAL); // Set the fastest update interval to 1 second mLocationRequest.setFastestInterval(FASTEST_INTERVAL); servicesConnected(); mLocationClient =new LocationClient(this, this, this); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } @Override public void onConnected(Bundle connectionHint) { // TODO Auto-generated method stub Log.d(TAG, "onconnected"); mLocationClient.requestLocationUpdates(mLocationRequest, this); } @Override public void onDisconnected() { // TODO Auto-generated method stub Log.d(TAG, "onsisconnect"); // Destroy the current location client mLocationClient = null; } @Override public void onConnectionFailed(ConnectionResult result) { // TODO Auto-generated method stub } private boolean servicesConnected() { // Check that Google Play services is available int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); // If Google Play services is available if (ConnectionResult.SUCCESS == resultCode) { return true; } else { return false; } } @Override public void onLocationChanged(Location location) { // TODO Auto-generated method stub Log.d(TAG, "onLocationChanged"); Log.d(TAG, "Lat = " + location.getLatitude() + " Lng = " + location.getLongitude()); Logger.writeLog("Lat = " + location.getLatitude() + " Lng = " + location.getLongitude() + " == "+BasicDeviceInformation.getCurrentDate()); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub Log.d(TAG, "onStartCommand"); mLocationClient.connect(); return super.onStartCommand(intent, flags, startId); } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } }
解决方案Don't know if the user will be back on SO or not, so posting an answer on his behalf for others who faced this problem.
PS:- This answer was suggested by the user himself but was at the wrong place, so posting on his behalf.
UPDATE
I found the solution for this problem.... I missed the meta-data tag in the manifest.
Place below code in manifest inside the Application tag.
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
After placing this one i am getting correct locations.
ISSUE IS CLOSED
这篇关于在android系统的GPS定位更新不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!