在我的应用程序中,我希望让用户知道他的本地化......但我想实现GPS和网络提供商...因为GPS提供商并不总是可用...我使用模拟器和我不知道我的代码是否有效......
这是我的代码来源:(提前谢谢)
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
location = locationManager.getLastKnownLocation(provider);
if(location!=null)
{
mylat=location.getLatitude();
mylong=location.getLongitude();
}
这是myLocationlistener类:
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
Log.i("location", "lat :"+loc.getLatitude());
mylat=loc.getLatitude();
mylong=loc.getLongitude();
for(int i=0; i<results.size(); i++)
{
gare g=results.get(i);
}
list.invalidateViews();
}
@Override
public void onProviderDisabled(String provider)
{
AlertDialog alertDialog = new AlertDialog.Builder(liste_activity.this).create();
alertDialog.setTitle("Le GPS est désactivé");
alertDialog.setMessage(" activez le GPS");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
} });
alertDialog.show();
}
@Override
public void onProviderEnabled(String provider)
{
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
答案 0 :(得分:0)
对我来说很好看。我通常将#requestLocationUpdates()包装在首先检查可用性的条件中。
if (this.mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
this.mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
if (this.mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
this.mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}