无法使用GPS获取位置

时间:2017-06-07 16:47:22

标签: android gps android-gps

我正在尝试创建一个简单的应用来显示我的位置坐标。当我模拟代码时(nexus 6.0,它可以工作)。但是,当我通过USB调试(我的htc 10)使用真实设备时,当我按下按钮显示我的坐标时,什么都没有出现。知道为什么吗?这是我的代码:

    //Location
    textView = (TextView) findViewById(R.id.textView);
    button2 = (Button) findViewById(R.id.button2);
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    listener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            textView.append("\n " + location.getLongitude() + " " + location.getLatitude());
            System.out.println(location.getLongitude());
            System.out.println(location.getLatitude());
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

            Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(i);
        }
    };

    configure_button();

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    switch (requestCode) {
        case 10:
            configure_button();
            break;
        default:
            break;
    }
}

void configure_button() {
    // first check for permissions
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
            PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_COARSE_LOCATION) != 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}
                    , 10);
        }
        return;
    }
    // this code won't execute IF permissions are not allowed, because in the line above there is return statement.
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //noinspection MissingPermission
            locationManager.requestLocationUpdates("gps", 5000, 0, listener);
        }
    });
}

我在清单中添加了这个:

  <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />

1 个答案:

答案 0 :(得分:1)

你应该在oncreate中声明button2点击监听器,因为configure_button()方法中有return语句。

在声明ID后立即声明。

  textView = (TextView) findViewById(R.id.textView);
    button2 = (Button) findViewById(R.id.button2);
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //noinspection MissingPermission
            locationManager.requestLocationUpdates("gps", 5000, 0, listener);
        }
    });
   //rest of the code