Georgi Koemdzhiev
Georgi Koemdzhiev

Reputation: 11931

How to return to my app after sending the user to settings with an intent

I am trying to figure a way to how to return the user to my app after they enabled location services automatically without the need for the user to press the back button. I check if the location services is enabled like that:

if( !locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            alertForNoLocationEnabled();
        }else {

            locationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, 0, 0, new MyLocationListener());
        }
private void alertForNoLocationEnabled() {
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle(R.string.network_not_found_title);  // network not found
    builder.setMessage(R.string.network_not_found_message); // Want to enable?
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialogInterface, int i) {
            toggleSwipeRefreshLayoutsOff();
            Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }
    });
    builder.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            toggleSwipeRefreshLayoutsOff();
        }
    });
    AlertDialog dialog = builder.create();
    dialog.setCanceledOnTouchOutside(false);
    dialog.show();
}

and after the user turns on the location services I want automatically the user to be taken back to my app and not pressing the back button. Please, anyone who has an idea to share it.

enter image description here

Upvotes: 2

Views: 1225

Answers (1)

A.Sanchez.SD
A.Sanchez.SD

Reputation: 2070

This might work.

 builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialogInterface, int i) {
        toggleSwipeRefreshLayoutsOff();

    AsyncTask.execute(new Runnable() {
        @Override
       public void run() {
         long fortySecondsFromNow = System.currentTimeMillis() + 40*1000
         while((System.curremtTimeMillis < fortySecondsFromNow)
              && !locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))) {
             Thread.sleep(300);
          } 
        if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER){
          Intent intent = new Intent(context, YourActivity.class);
          intent.setAction(Intent.ACTION_MAIN);
          intent.addCategory(Intent.CATEGORY_LAUNCHER);
          startActivity(intent);
          //Do what u want
           } 

        });

        Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
    }

Upvotes: 2

Related Questions