mrmans0n / smart-location-lib

Android library project that lets you manage the location updates to be as painless as possible
1.65k stars 353 forks source link

smart-location-lib not working on api level 21 and above android #202

Open kkhurramshahzad opened 7 years ago

kkhurramshahzad commented 7 years ago

I am using smart location library. I am trying to get location using longitude and latitude. But my code is working perfectly in api 16. but I checked it on api 21. the same code isnt showing anything.. nor crashing. I have added permissions but still isnt working. Kindly check this out. Help will be appreciated. Thanks.

Can you guys run this and checkout what's the issue.. please

                            public class MainActivity extends AppCompatActivity {

                           private Double lat;
                           private Double lon;
                          private Context context;
                         private Button address_button;
                            private static final int PERMISSION_REQUEST_CODE =11 ;

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

                      context=this;
                       address_button= (Button) findViewById(R.id.button_address);

                      address_button.setOnClickListener(new View.OnClickListener() {
                       @Override
                     public void onClick(View v) {

        if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.M){
            if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)!= 
                        PackageManager.PERMISSION_GRANTED){
                if 
                (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_COARSE_LOCATION))
         {
                    new AlertDialog.Builder(MainActivity.this)
                            .setTitle("Call Permission")
                            .setMessage("Hi there! We can't call anyone without the call permission, could you 
       please grant it?")
                            .setPositiveButton("Yep", new DialogInterface.OnClickListener() {
                                @RequiresApi(api = Build.VERSION_CODES.M)
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    requestPermissions(new String[]
      {Manifest.permission.ACCESS_COARSE_LOCATION},PERMISSION_REQUEST_CODE);

                                }
                            })
                            .setNegativeButton("No thanks", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Toast.makeText(MainActivity.this, ":(", Toast.LENGTH_SHORT).show();                                        
                    }
                            }).show();
                }else {
                    requestPermissions(new String[]
                 {Manifest.permission.ACCESS_COARSE_LOCATION},PERMISSION_REQUEST_CODE);
                }
            } else{
                SmartLocationCityName();
            }
           }else{
            SmartLocationCityName();
           }

         }

         });

             }

                        @Override
                        protected void onStop() {
                          super.onStop();
                         SmartLocation.with(context).location().stop();
                              } 

                          @Override
                           public void onRequestPermissionsResult(int requestCode, @NonNull String[] 
                   permissions, @NonNull 
                   int[] grantResults) {
                   if (requestCode== PERMISSION_REQUEST_CODE){
                if (grantResults.length>0 && grantResults[0]== PackageManager.PERMISSION_GRANTED){
                    SmartLocationCityName();
                }
             }
            }

                  public void SmartLocationCityName(){
                   SmartLocation.with(context).location()
        .start(new OnLocationUpdatedListener() {

            @Override
            public void onLocationUpdated(Location location) {

                lat = location.getLatitude();
                lon = location.getLongitude();
                Toast.makeText(context,lat+" "+lon+" ",Toast.LENGTH_LONG).show();

                try {
                    Geocoder geocoder = new Geocoder(context, Locale.getDefault());
                    List<Address> addresses = geocoder.getFromLocation(lat, lon, 1);
                    String cityName = addresses.get(0).getAddressLine(0);
                    String stateName = addresses.get(0).getAddressLine(1);
                    String countryName = addresses.get(0).getAddressLine(2);

                    Toast.makeText(context,lat+" "+lon+" "+cityName+"-"+"-"+stateName+"-
                  "+countryName,Toast.LENGTH_LONG).show();
                }catch (IOException ex){

                }

            }
        });

          }
                  }
ReemHazzaa commented 5 years ago

I have the same problem, can anyone help?