0
我是新的android開發。我想在android中使用GPS定位。我採用了兩類:一類用於GPSTracker,另一類用於活動。該位置是越來越設置爲空每次,當我在logcat的檢查,我得到以下錯誤:native_start在開始失敗android GPS GPS
native_start failed in startNavigating
我已在清單中的GPS數據的許可也是如此。我甚至試圖通過使用模擬器控制傳遞虛假的經度和緯度,不起作用。可能是什麼原因?我編碼的Android 2.3
我的代碼是://GPSTracker.java
public class GPSTracker extends Service implements LocationListener {
private final Context mcontext;
boolean isGPSEnabled = false;
boolean canGetLocation = false;
double latitude,longitude;
Location location;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;
protected LocationManager locationManager;
protected LocationListener listener;
public GPSTracker(Context context){
this.mcontext = context;
getLocation();
}
public Location getLocation() {
try{
locationManager = (LocationManager) mcontext.getSystemService(LOCATION_SERVICE);
listener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location1) {
// TODO Auto-generated method stub
location = location1;
}
};
isGPSEnabled = locationManager.isProviderEnabled(locationManager.GPS_PROVIDER);
if(isGPSEnabled){
this.canGetLocation = true;
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, listener);
Log.d("GPS", "Enabled");
if(locationManager!=null){
location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
if(location!=null){
latitude = location.getLatitude();
longitude = location.getLongitude();
//Log.d("Latitude", latitude.toString());
//Log.d("Longitude", longitude.toString());
}
else{
Log.d("Location","Null");
}
}
}
}catch(Exception e){
e.printStackTrace();
}
return location;
// TODO Auto-generated method stub
}
public boolean canGetLocation(){
return this.canGetLocation;
}
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mcontext);
alertDialog.setTitle("GPS Settings");
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mcontext.startActivity(intent);
}
});
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 arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public double getLatitude() {
// TODO Auto-generated method stub
if(location!=null){
Log.d("Location",location.toString());
latitude = location.getLatitude();
}
return latitude;
}
public double getLongitude() {
// TODO Auto-generated method stub
if(location!=null){
Log.d("Location",location.toString());
longitude = location.getLongitude();
}
return longitude;
}
}
//LocationActivity.java
public class LocationActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
GPSTracker gps = new GPSTracker(LocationActivity.this);
if(gps.canGetLocation() == false){
Log.d("GPSLogFromActivity","False");
gps.showSettingsAlert();
}
else{
double latitude = gps.getLatitude();
System.out.println("Latitude: "+latitude);
double longitude = gps.getLongitude();
System.out.println("Longitud: e"+longitude);
Log.d("GPSLogFromActivity","True");
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
}
}
});
沒有工作。我仍然在LogCat ..:/ – 2013-03-21 06:41:53
我也想過試圖從網絡提供商那裏得到的位置。將權限和GPSProvider更改爲網絡提供者之後,我管理刪除了該錯誤,但即使從模擬器控件傳入數據,我仍然將該位置設置爲Null。 完全卡在這一個..:/ – 2013-03-21 06:52:26
嘿朋友 私人靜態最後長MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; private static final long MIN_TIME_BW_UPDATES = 0; 你必須保持這個0那麼只有你會得到待機 當前代碼的位置將其設置爲10就意味着10米運動它將覆蓋onlocationchamge方法 **** locationManager.removeUpdate(locationListner後); ****** locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,listener); 嘗試從NETWORK_PROVIDER獲取位置,現在它的工作也是 – 2013-03-21 07:48:46