在 Android 上使用 LocationListener 禁用提供程序

发布于 2024-12-11 10:34:25 字数 1331 浏览 0 评论 0原文

我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想念有你 2024-12-18 10:34:25

我想你是想写的

if (!lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
   this.t1.setText("Provider disabled"); // always get this

不是吗?

按照目前的情况,启用时会显示“已禁用”。

I think you meant to write

if (!lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
   this.t1.setText("Provider disabled"); // always get this

didn't you?

As it stands you display 'disabled' when it's enabled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文