我編碼的GPS跟蹤應用程序,我發現一個propplem:有一段時間它得到我的舊位置,當我拿出電池電池,並重新啓動手機它工作正常。 我不知道爲什麼,你能幫我嗎?Android GPS得到舊位置
回答
有兩種方法,你可以在Android的檢索位置。
1.使用LocationManager
2.使用LocationClient
(谷歌播放服務)
使用
LocationManager
,你必須提供一個LocationManager
Provider
(GPS,網絡,WIFI,被動)。在GPS提供商的情況下,LocationManager
將使用硬件GPS來解析當前位置。如果目前沒有GPS,並且您撥打GetLastKnownLocation()
。它會返回一個Location
對象,來自GPS捕獲位置的最後一個位置(如果有),如果沒有,則返回null
。如果您嘗試使用getTime()
方法Location
您從getLastKnownLocation()
獲得的對象,它將在最後一個位置被捕獲時返回給您。 網絡,無線或被動提供商也是如此。使用
LocationClient
從谷歌Play服務的LocationClient
會使用自動給你最準確的電流Location
*通過調用getLastLocation()
ALL THREE(如果可用),GPS,WIFI,3G(網絡)*。再次,這將返回一個Location
對象,其餘部分將如上所述。
任何使用這種方法是在你的應用程序更合適的。
注:的LocationManager將要求設備具有一個硬件GPS和LocationClient會要求谷歌播放服務,GPS是不是必須的,但如果設備有一個那麼這將有利於位置更加準確
這是代碼來獲取位置:
public class GPSTraker extends Service implements LocationListener {
private final Context mContext;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 5; // 1 minute
// Declaring a Location Manager
protected LocationManager locationManager;
public GPSTraker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
/**
* Stop using GPS listener Calling this function will stop using GPS in your
* app
* */
public void stopUsingGPS() {
if (locationManager != null) {
locationManager.removeUpdates(GPSTraker.this);
}
}
/**
* Function to get latitude
* */
public double getLatitude() {
if (location != null) {
latitude = location.getLatitude();
}
return latitude;
}
public float getspeed() {
float seeed = 0;
if (location != null)
seeed = location.getSpeed();
return seeed;
}
/**
* Function to get longitude
* */
public double getLongitude() {
if (location != null) {
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
/**
* Function to check GPS/wifi enabled
*
* @return boolean
* */
public boolean canGetLocation() {
return this.canGetLocation;
}
/**
* Function to show settings alert dialog On pressing Settings button will
* lauch Settings Options
* */
public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog
.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
任何人都可以幫助我嗎? –
用您的問題發佈您的代碼 – Boopathi
public final LocationListener locationListener = new LocationListener()
{
public void onLocationChanged(Location location) {
Home.this.location=location;
}
public void onProviderDisabled(String provider) {
Home.this.location=null;
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
public String gpsadd() {
try {
if(isGPS) {
Geocoder gc = new Geocoder(Home.this, Locale.getDefault());
try
{
addflg=0;
lat=location.getLatitude();
lng=location.getLongitude();
boolean lop=true;
while(lop) {
List<Address> addresses = gc.getFromLocation(lat, lng, 1);
if (addresses.size() > 0)
{
lop=false;
addflg=1;
address = addresses.get(0);
s_addr=address.getAddressLine(0)+" "+address.getAddressLine(1)+" "+address.getAddressLine(2);
disp="Location\n "+address.getAddressLine(0)+"\n"+address.getAddressLine(1)+"\n"+address.getAddressLine(2);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
} else {
display("GPS not Enabled");
}
} catch(Exception e) {
e.printStackTrace();
}
return disp;
}
這裏我給你的代碼,通過GPS顯示當前位置...只需複製並粘貼以下代碼...並在您的Android清單文件中定義2個權限.....可能是這個代碼可以幫助你,請通知我,如果你想任何clearification ....
**MainActivity.java**
package com.example.android;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button btnShowLocation;
GPSTracker gps;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
// show location button click event
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// create class object
gps = new GPSTracker(MainActivity.this);
// check if GPS enabled
if(gps.canGetLocation()){
double longitude = gps.getLongitude();
double latitude = gps.getLatitude();
getAddress(latitude, longitude);
}
else{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
}
});
}
private void getAddress(double latitude, double longitude) {
StringBuilder result = new StringBuilder();
try {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) {
Address address = addresses.get(0);
result.append(address.getLocality()).append("\n");
//result.append(address.getPostalCode());
result.append(address.getAdminArea()).append("\n");
result.append(address.getCountryName());
String address1 = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);
Toast.makeText(getApplicationContext(), "Your address is " + address1 + " ," + city + "," + country , Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
} }
GPS Tracker.java
package com.example.android;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
// Declaring a Location Manager
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
* */
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(GPSTracker.this);
}
}
/**
* Function to get latitude
* */
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
* */
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
/**
* Function to check GPS/wifi enabled
* @return boolean
* */
public boolean canGetLocation() {
return this.canGetLocation;
}
/**
* Function to show settings alert dialog
* On pressing Settings button will lauch Settings Options
* */
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
Activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button android:id="@+id/btnShowLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Location"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
AndroidManifest。XML
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
- 1. 我得到的GPS /網絡位置android刪除位置的值
- 2. Android GPS位置
- 3. 如何獲得Android的GPS位置
- 4. Android Force GPS位置
- 5. Q. Android GPS位置
- 6. Android的GPS位置
- 7. 得到GPS位置不工作
- 8. 獲得GPS當前位置
- 9. 無法獲得GPS位置
- 10. android檢測GPS提供商,但沒有得到的位置
- 11. 得到錯誤的GPS在android中的位置
- 12. 需要android活動才能等到獲得GPS位置
- 13. 監視android GPS位置
- 14. Android仿真器 - GPS位置
- 15. 更改GPS位置爲Android
- 16. Android-獲取GPS位置
- 17. Android GPS位置更新
- 18. Android GPS位置監聽器
- 19. 用戶的Android GPS位置
- 20. Android GPS返回null位置
- 21. android gps當前位置
- 22. 在android中的GPS位置
- 23. Android GPS定位,不會傳遞位置
- 24. 拍攝照片後獲得gps位置
- 25. GPS位置到時區
- 26. 找不到GPS位置
- 27. 如何獲得持續的GPS位置
- 28. Gps位置+ googlemap
- 29. 流星Android設備上無法獲得GPS位置
- 30. 如何獲得精確的GPS座標Android位置服務
示例代碼?使用Google Play服務或位置提供商? – alex
我正在使用位置提供商 –
您已實施的代碼? – johntheripp3r