我尝试使用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提到的代码已更正,但按下按钮时仍未收到任何位置
答案 0 :(得分:1)
我认为你打算写
if (!lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
this.t1.setText("Provider disabled"); // always get this
不是吗?
当它启用时,你会显示'已禁用'。