如何开始操作Android GPS?

时间:2016-12-30 17:00:08

标签: android android-gps

因此,我一直在尝试了解有关Android GPS应用程序(特别是离线)的更多信息,并且我注意到LocationManager可能能够解决我的问题。问题是它需要API 23才能运行,我希望出于特定原因而针对API 19设备。有没有其他方法来操纵GPS功能,以便在较低级API上获取没有互联网访问的坐标?

请帮忙!

@Edit:我已经添加了一个示例应用程序(并且工作得很好 - 这里唯一的问题是它需要API 23才能运行)这段代码描述了一个显示十进制的应用程序每5秒坐标一次。

  

button =(Button)findViewById(R.id.button);           textView =(TextView)findViewById(R.id.textView);

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            textView.append("\n "+ location.getLatitude() + " "+ location.getLongitude());
        }

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

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {
            Intent intent = new Intent (Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }
    };
    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_FINE_LOCATION
            }, 10 );
        }
        return;
    }
    else {
        configureButton();
    }



} //onCreate

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case 10:
            if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                configureButton();
            return;
    }
}

private void configureButton() {
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            locationManager.requestLocationUpdates("gps", 5000, 0, locationListener);
        }
    });

}

0 个答案:

没有答案
相关问题