2011-09-23 70 views
0

我一直在嘗試3天左右來解決這個問題,到目前爲止我還沒有找到解決方案。我正試圖通過GPS獲取我的位置。 它似乎沒有收到我嘗試通過cmd.exe或DDMS中的仿真器控件發送的座標。而且,它會重新啓動仿真器。我一直在嘗試的代碼如下。如果有人可以幫我,那麼請做,因爲我找不到解決這個問題的任何方法。謝謝!Android上的GPS問題

import android.app.Activity; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.widget.TextView; 

public class Check_Location extends Activity { 

public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

LocationManager locationManager = (LocationManager)  this.getSystemService(Context.LOCATION_SERVICE); 

LocationListener locationListener = new LocationListener() { 
public void onLocationChanged(Location location) { 

TextView tv=(TextView) findViewById(R.id.textView1); 
tv.setText(location.getLongitude()+" "+location.getLatitude()); 
} 

public void onStatusChanged(String provider, int status, Bundle extras) {} 

public void onProviderEnabled(String provider) {} 

public void onProviderDisabled(String provider) {} 
}; 

//Register the listener with the Location Manager to receive location updates 
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 
} 
} 

,我已經試過,也沒有工作,另一種方法是如下:

import android.app.Activity; 
    import android.content.Context; 
    import android.location.Criteria; 
    import android.location.Location; 
    import android.location.LocationListener; 
    import android.location.LocationManager; 
    import android.os.Bundle; 
    import android.util.Log; 
    import android.widget.TextView; 
    import android.widget.Toast; 

public class MainActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
/* Use the LocationManager class to obtain GPS locations */ 
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
LocationListener mlocListener = new MyLocationListener(); 
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener); 

} 

/* Class My Location Listener */ 
public class MyLocationListener implements LocationListener 
{ 
    @Override 
    public void onLocationChanged(Location loc) 
    { 
     loc.getLatitude(); 
     loc.getLongitude(); 

     String Text = "My current location is: " + 
         "Latitude = " + loc.getLatitude() + 
         "Longitude = " + loc.getLongitude(); 

     Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onProviderDisabled(String provider) 
    { 
     Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onProviderEnabled(String provider) 
    { 
     Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) 
    { 
    } 

}/* End of Class MyLocationListener */ 
} 

兩個設置的permisssions是:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission> 
    <uses-permission android:name="android.permission.INTERNET"></uses-permission> 
    <uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES"></uses-permission> 
+0

我設法解決了我的問題。我已閱讀過其他論壇。這兩個代碼都可以工作,但可以在小於10級的Android平臺上試用。例如,爲8級製作模擬器。它可以工作! – Lara

+0

在此發佈您的答案並接受它,這樣其他人可以學習。 –

回答

0

我設法解決我的問題。我已閱讀過其他論壇。這兩個代碼都可以工作,但可以在小於10級的Android平臺上嘗試。例如,爲8級製作模擬器。它可以工作!

+0

你知道我可以在哪裏下載兼容android api level 8的Google API嗎?我面臨與2.3.x相同的GPS問題,謝謝! – coredump