位置服务第二次不工作

时间:2017-09-24 21:39:36

标签: java android

如果我运行它,第一次它会询问位置权限并给出gps位置但是在关闭并再次打开它之后它不会给出任何数据 有卸载并重新安装它agian,然后它工作正常,任何人都可以帮助我? 我已经工作了几个星期而且我被困在这里

     locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {

            new task().execute("http://192.168.0.104:1337/updatelocation?user_id="+tveid.getText()+"&lat="+location.getLatitude()+"&lng="+location.getLongitude());



            Toast.makeText(TrackingGpsLocation.this,location.getLatitude() +" "+location.getLongitude(), Toast.LENGTH_SHORT).show();


        }

        @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[]{
                    Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.INTERNET

            }, 10);
            return;
        }
    }else {
        configureButton();
    }


}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode){
        case 10:
            if (grantResults.length>0 && grantResults[0] == getPackageManager().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 个答案:

没有答案