GPS位置仅在应用程序首次打开时有效

时间:2017-08-04 20:17:42

标签: java android

我在点击按钮时尝试获取用户的longitudelatitude

以下代码在首次安装应用时获取GPS,但是,当应用关闭并重新打开时,它无法获取任何位置

我不确定我是否错误地编写了代码以获取GPS位置或者我遗漏了一些东西。任何建议将不胜感激。

public class MainActivity extends AppCompatActivity {
    private Button button;
    private EditText editText;
    private LocationManager locationManager
    private LocationListener locationListener;

    private double longitude;
    private double latitude;
    private TextView textView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById((R.id.button2));
        editText = (EditText) findViewById((R.id.editText2));

        //My code
        textView = (TextView) findViewById((R.id.textView2));

        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                longitude = location.getLongitude();
                latitude = location.getLatitude();
                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 (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
               requestPermissions(new String[]{
                       android.Manifest.permission.ACCESS_FINE_LOCATION,android.Manifest.permission.ACCESS_COARSE_LOCATION,
                       android.Manifest.permission.INTERNET
               }, 10);
                //return;
            }
        }
        else {
            configureButton();
        }
        button.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                configureButton();
            }
        });
    }

    @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 view){
                locationManager.requestLocationUpdates("gps", 5000, 0, locationListener);

            }
        });*/
        locationManager.requestLocationUpdates("gps", 5000, 0, locationListener);


     }
}

1 个答案:

答案 0 :(得分:1)

如果用户已经授予了位置权限,只需要请求位置更新:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

           requestPermissions(new String[]{
                   android.Manifest.permission.ACCESS_FINE_LOCATION,android.Manifest.permission.ACCESS_COARSE_LOCATION,
                   android.Manifest.permission.INTERNET
           }, 10);

        } else {
            //Added:
            locationManager.requestLocationUpdates("gps", 5000, 0, locationListener);
        }
    }
    else {
        locationManager.requestLocationUpdates("gps", 5000, 0, locationListener);
    }