从我的android应用程序调用后,我的设置页面会继续重新加载

时间:2019-01-11 10:01:32

标签: android android-location android-settings location-provider

从现在开始,我有点被封锁在这里。 从我的设置页面调用后,我的设置页面会继续重新加载 android应用程序。 我的Android应用程序会定期检查位置更新。 在我的设置中,我必须在打开“位置”按钮后将其校准为“高精度”。
    //     //     //

    locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
            listener = new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    setLattitude(location.getLatitude());
                    setLongitude(location.getLongitude());
                    getAllLocations();
                }

                @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);
                }
            };

            // this code won't execute IF permissions are not allowed, because in the line above there is return statement.
            if (Build.VERSION.SDK_INT < 23) {
                if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)
                        != PackageManager.PERMISSION_GRANTED
                        && ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION)
                        != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    Toast.makeText(getApplicationContext(),
                            "111111",
                            Toast.LENGTH_LONG).show();
                    locationManager.requestLocationUpdates(NETWORK_PROVIDER, 5000, 0, listener);
                    return;
                }
                locationManager.requestLocationUpdates(NETWORK_PROVIDER, 5000, 0, listener);
                Toast.makeText(getApplicationContext(),
                        "222222",
                        Toast.LENGTH_LONG).show();
            }else{

                //noinspection MissingPermission
                if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION)
                        != PackageManager.PERMISSION_GRANTED
                        && ActivityCompat.checkSelfPermission
                        (context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    Toast.makeText(getApplicationContext(),
                            "333333",
                            Toast.LENGTH_LONG).show();
                    ActivityCompat.requestPermissions((Activity) context, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
                    //ActivityCompat.requestPermissions((Activity) context, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
                    return;
                }
                else{
                    locationManager.requestLocationUpdates(NETWORK_PROVIDER, 0, 0, listener);
                    Toast.makeText(getApplicationContext(),
                            "444444",
                            Toast.LENGTH_LONG).show();
                }
            }

然后按我的要求

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);

            if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                //locationManager.requestLocationUpdates(NETWORK_PROVIDER, 0, 0, listener);
                if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                        == PackageManager.PERMISSION_GRANTED){
                    locationManager.requestLocationUpdates(NETWORK_PROVIDER, 0, 0, listener);
                }
    /*
                if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
                        == PackageManager.PERMISSION_GRANTED){
                    locationManager.requestLocationUpdates(NETWORK_PROVIDER, 0, 0, listener);
                }
    */
            }
        }

0 个答案:

没有答案