在 Android 上使用 LocationListener 禁用提供程序
我尝试使用 NETWORK_PROVIDER
来定位自己。当我运行我的应用程序(在带有 Eclipse 的模拟器和我的手机上)时,我总是检测到该提供程序已禁用。
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
// refresh button
this.b1 = (Button)findViewById(R.id.b1);
this.b1.setOnClickListener(this);
this.lm = (LocationManager)getSystemService(LOCATION_SERVICE);
String provider = LocationManager.NETWORK_PROVIDER;
Location location = this.lm.getLastKnownLocation(provider);
this.t1 = (TextView)findViewById(R.id.t1);
if (location != null)
this.t1.setText(location.toString());
else
if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)==false)
this.t1.setText("Provider disabled");
else
this.t1.setText("No location, please wait");
int t = 5000;
int distance = 5;
lm.requestLocationUpdates(provider, t, distance, myLocationListener);
}
public void onClick(View view){
Location location = this.lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null)
this.t1.setText(location.toString());
else
this.t1.setText("Refreshed but no location");
}
我查了一下,设置>中的选项地点>已启用使用无线网络。
编辑:代码已按照 NickT 的说明进行了更正,但按下按钮时仍然没有收到任何位置信息
I try to use the NETWORK_PROVIDER
to locate myself. When I run my application (on the emulator with eclipse and with my phone), I always detect the provider as disabled.
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
// refresh button
this.b1 = (Button)findViewById(R.id.b1);
this.b1.setOnClickListener(this);
this.lm = (LocationManager)getSystemService(LOCATION_SERVICE);
String provider = LocationManager.NETWORK_PROVIDER;
Location location = this.lm.getLastKnownLocation(provider);
this.t1 = (TextView)findViewById(R.id.t1);
if (location != null)
this.t1.setText(location.toString());
else
if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)==false)
this.t1.setText("Provider disabled");
else
this.t1.setText("No location, please wait");
int t = 5000;
int distance = 5;
lm.requestLocationUpdates(provider, t, distance, myLocationListener);
}
public void onClick(View view){
Location location = this.lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null)
this.t1.setText(location.toString());
else
this.t1.setText("Refreshed but no location");
}
I checked, the option in Settings > Location > Use wireless networks is enabled.
edit: code corrected as mentioned by NickT but still receive no location when pushing the button
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想你是想写的
不是吗?
按照目前的情况,启用时会显示“已禁用”。
I think you meant to write
didn't you?
As it stands you display 'disabled' when it's enabled.