2

if i run this, for first time it will ask location permissions and gives the gps location but after closing and opening it again it wont give any data have uninstall and re-install it agian, then it works fine, can anyone help me?? i have been working on this for couple of weeks and i am stuck here

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


        }
    });
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    Why not use Google Location Services API that provides a more powerful, high-level framework that automates tasks such as location provider choice and power management? Anyway you requestçpcationUpdates in onResume and in onPause call removeUpdates – RaffaD Sep 24 '17 at 22:04
  • Is all of this within onCreate? – AnthonyK Sep 24 '17 at 22:16
  • yes all within on create – Aman Hasan Shaik Sep 24 '17 at 23:43
  • try this code https://stackoverflow.com/questions/45699971/fusedlocationapi-getlastlocation-is-always-null-even-oncreated?answertab=votes#tab-top – Vinayak B Sep 25 '17 at 07:05

0 Answers0