我有一个使用用户位置的活动。这是我到目前为止编写的代码:
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 方法中的 Toast 都没有显示。我正在使用地理修复来更改位置。我实现了 LocationListener 的其他方法,但它们是空的。显示电话号码的 toast 工作正常。有人可以帮我吗?