我尝试使用LocationManager
来获取用户当前位置但似乎永远不会调用onLocationChanged
。
这是我的代码
public void checkLocationService(){
LocationManager lm = (LocationManager)MapsActivity.this.getSystemService(Context.LOCATION_SERVICE);
boolean gps_enabled = false;
boolean network_enabled = false;
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch(Exception ex) {
ex.printStackTrace();
}
try {
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch(Exception ex) {
ex.printStackTrace();
}
if(!gps_enabled && !network_enabled) {
// notify user
AlertDialog.Builder dialog = new AlertDialog.Builder(MapsActivity.this);
dialog.setTitle("Perhatian");
dialog.setMessage("Anda harus mengaktifkan GPS pada perangkat anda untuk dapat menggunakan semua fitur yang ada. Aktifkan GPS?");
dialog.setPositiveButton("Pengaturan", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub
Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
MapsActivity.this.startActivity(myIntent);
}
});
dialog.setNegativeButton("Batal", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub
}
});
dialog.show();
}
if (gps_enabled) {
try {
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
} catch (SecurityException e) {
}
if (lm != null) {
try {
lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
} catch (SecurityException e) {
}
}
}
if (network_enabled) {
try {
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
} catch (SecurityException e) {
}
if (lm != null) {
try {
lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
} catch (SecurityException e) {
}
}
}
}
这里是onLocationChanged
@Override
public void onLocationChanged(Location l) {
pointLatitude = l.getLatitude();
pointLongitude = l.getLongitude();
//Fetching username from shared preferences
SharedPreferences sharedPreferences = getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
final String username = sharedPreferences.getString(Config.USERNAME_SHARED_PREF, "Tidak tersedia");
final ProgressDialog loading = ProgressDialog.show(this, "Login...", "Mohon tunggu...", true, false);
//Creating a string request
StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.GET_LOCATION_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//You can handle error here if you want
//Toast.makeText(LoginActivity.this,error.toString(),Toast.LENGTH_LONG ).show();
loading.dismiss();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
//Adding parameters to request
params.put(Config.KEY_USERNAME, username);
params.put(Config.KEY_LATITUDE, String.valueOf(pointLatitude));
params.put(Config.KEY_LONGITUDE, String.valueOf(pointLongitude));
//returning parameter
return params;
}
};
//Adding the string request to the queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
这里有什么问题?提前谢谢。
答案 0 :(得分:0)
first check below permission is required.
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
Then please check that your class need to extend LocationListener
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location arg0) {
latitude = arg0.getLatitude();
longitude = arg0.getLongitude();
}
}