【问题标题】:Network location provider's status never changes?网络位置提供商的状态永远不会改变?
【发布时间】:2013-05-26 18:55:53
【问题描述】:

LocationManager 注册LocationManager.NETWORK_PROVIDER 后,即使在注册期间打开/关闭数据或网络,也不会调用回调onStatusChanged()。这种方法对基于网络的提供商无效吗?

【问题讨论】:

  • 哪个android API?它适用于 GPS 吗?
  • @Mr_and_Mrs_D 最后一次检查果冻豆。是的,它适用于 GPS,只有 NETWORK_PROVIDER 没有改变其状态。
  • 奇怪 - 因为 GPS 是 reported,因为它不适用于旧设备/API (?) - 另请参阅 here。也许发布一个问题(并在此处添加)?
  • @Mr_and_Mrs_D 我没有在旧版本上测试过,但是在我的果冻豆设备上,当我关闭或打开它时,GPS_PROVIDER 会立即调用onStatusChanged()
  • 好的 - 或许可以发布代码的相关部分?

标签: android geolocation


【解决方案1】:

试试这个:

package com.mytest;

import android.R;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.widget.TextView;

public class NetworkTest extends Activity {

    private LocationManager locationManager;
    private TextView textView;
    private final LocationListener networkLocationListener=new LocationListener(){

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            switch (status) {
            case LocationProvider.AVAILABLE:
                textView.setText(textView.getText().toString()
                        + "Network location available again\n");
                break;
            case LocationProvider.OUT_OF_SERVICE:
                textView.setText(textView.getText().toString()
                        + "Network location out of service\n");
                break;
            case LocationProvider.TEMPORARILY_UNAVAILABLE:
                textView.setText(textView.getText().toString()
                        + "Network location temporarily unavailable\n");
                break;
            }
        }

        @Override
        public void onProviderEnabled(String provider) {
            textView.setText(textView.getText().toString()
                    + "Network Provider Enabled\n");
        }

        @Override
        public void onProviderDisabled(String provider) {
            textView.setText(textView.getText().toString()
                    + "Network Provider Disabled\n");
        }

        @Override
        public void onLocationChanged(Location location) {
            textView.setText(textView.getText().toString()
                    + "New network location: "
                    + String.format("%9.6f", location.getLatitude()) + ", "
                    + String.format("%9.6f", location.getLongitude()) + "\n");
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activitynetwork);
        textView = (TextView) findViewById(R.id.textview);
        locationManager = (LocationManager)
                getSystemService(Context.LOCATION_SERVICE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 5000, 0,
                networkLocationListener);
    }

    @Override
    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(networkLocationListener);
    }
}

它对我有用。

【讨论】:

  • 感谢您的努力,但不,当我打开/关闭网络或切换到飞行模式并返回时,不会调用 onStatusChanged()
  • @Singularity 用Broadcast Receiver 监控网络变化可以吗?
  • 我可以这样做,或者可能是另一种解决方法。我的想法是,如果 Android 将网络列为提供商之一,它还必须向我更新其状态,就像 GPS 等一样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多