我有一個使用用戶位置的活動。這裏是我到目前爲止已經編寫的代碼:Android烤麪包沒有顯示
public class VolSaveLocation extends Activity implements LocationListener {
@SuppressLint("HandlerLeak")
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 0) {
useLocation();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.volsavelocaction);
LocationManager locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
TelephonyManager phoneManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
String phoneNumber = phoneManager.getLine1Number();
Toast.makeText(getApplicationContext(), phoneNumber, Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
return super.onCreateOptionsMenu(menu);
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if (location != null) {
Toast.makeText(getApplicationContext(), "Location not null!", Toast.LENGTH_SHORT).show();
Message msg = new Message();
msg.what = 0;
msg.obj = location;
handler.sendMessage(msg);
}
else
Toast.makeText(getApplicationContext(), "Location is null!", Toast.LENGTH_SHORT).show();
}
}
問題是,無論是在onLocationChanged方法顯示在祝酒詞中。我正在使用地理位置修改來更改位置。我已經實現了LocationListener的其他方法,但它們是空的。顯示電話號碼的吐司工作正常。有人可以幫我嗎?
放一個記錄在您的「onLocationChanged」,看看它甚至得到調用。 –
onLocationChanged每次都沒有100%的保證。 –