【问题标题】:Location Listener is not working on Network provider位置侦听器不适用于网络提供商
【发布时间】:2018-09-17 21:12:10
【问题描述】:

我有下面的代码,来试验来自 wifi 的位置:

public class MainActivity extends AppCompatActivity {    
   Context mContext;
   TextView text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mContext=getApplicationContext();
    text=findViewById(R.id.text1);
    askpermissions();
    doLocationStuff();
}
void doLocationStuff(){
    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            // Called when a new location is found by the network location provider.
            text.append(location.getLatitude()+" "+location.getLatitude());
            Toast.makeText(mContext, location.getLatitude()+" "+location.getLatitude(), Toast.LENGTH_SHORT).show();
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {}

        public void onProviderEnabled(String provider) {}

        public void onProviderDisabled(String provider) {
            Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(i);
        }
    };


    if(ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION)== PackageManager.PERMISSION_GRANTED) {
        text.setText("wifi gps");
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    }
}

void askpermissions(){
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ||
            ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ||
            ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
            ActivityCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE) !=PackageManager.PERMISSION_GRANTED) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.INTERNET,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE}
                    ,10);
        }
    }

}

当我将提供程序设置为 GPS_PROVIDER 时,应用程序按预期工作(位置显示在文本视图和吐司中)。

但是,当我使用 NETWORK_PROVIDER 时,什么都没有发生(textview 停留在 wifi gps 并且没有 toast 出现)。我的应用程序具有粗略和精细定位和互联网访问权限,并且启用了 GPS 和 Wifi。

我以为我的路由器的位置没有被记录下来,但是当我打开谷歌地图时,它的估计值在 wifi 开启的情况下要好得多。

编辑:该代码在搭载 Android 5 的三星 Galaxy A5 上运行良好,但不适用于搭载 Android 6 的华为 P8 lite

我做错了什么?

【问题讨论】:

    标签: android gps


    【解决方案1】:

    尝试检查您的权限

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    

    【讨论】:

    • 我添加了 ACCESS_NETWORK_STATE、ACCESS_WIFI_STATE,并使用了 android.hardware.location.network 和 android.hardware.location 功能,没有任何改变
    • 您说在三星中可以正常工作,这可能与您的代码无关,而是某些手机需要用户操作才能获得额外权限,请尝试在您的华为Settings --&gt; Apps ---&gt; Advanced (left corner at bottom of the screen) ---&gt; Ignore optimisation (last option of the menu) ---&gt; Allowed ---&gt; All Apps (it will now show all the apps on the list) ---&gt; YOUR APPS ---&gt; Allow 中设置权限,如果这项工作需要额外权限也适用于某些设备。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-01
    • 1970-01-01
    相关资源
    最近更新 更多