位置监听器不适用于网络提供商

时间:2018-09-17 21:12:11

标签: android gps

我有以下代码,可以测试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时,该应用程序将按预期运行(位置显示在textview和Toast中)。

但是,当我使用NETWORK_PROVIDER时,什么也没发生(textview停留在wifi gps上,并且没有吐司)。我的应用有权粗略定位和访问互联网,并且同时启用了GPS和Wifi。

我以为没有记录路由器的位置,但是当我打开Goog​​le Maps时,打开wifi估计会更好。

编辑:该代码在装有Android 5的Samsung Galaxy A5上工作正常,但不适用于装有Android 6的Huawei P8 lite

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

尝试检查您的许可

<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" />