title: Splash Screen Delay timer wait till permission dialog close in android
body: <p>I am collecting GPS location data in Splash Screen and store it in session, and use it later in <code>MainActivity</code>. If the GPS is turn off or used for the first time it will ask for permission in my splash screen. The problem is that I am using the timer to move splash screen Activity to another activity. Due to the timer, SplashScreen is moving to next the activity without the permission. I want my Splash screen to wait till permission dialog is allowed or denied. Below is my code</p>
  
  <pre><code>import android.animation.ObjectAnimator;
  import android.annotation.SuppressLint;
  import android.annotation.TargetApi;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.content.pm.PackageManager;
  import android.graphics.Typeface;
  import android.location.Address;
  import android.location.Geocoder;
  import android.os.Build;
  import android.os.Bundle;
  import android.os.Handler;
  import android.support.v7.app.AlertDialog;
  import android.support.v7.app.AppCompatActivity;
  import android.view.Window;
  import android.view.WindowManager;
  import android.view.animation.Animation;
  import android.view.animation.AnimationUtils;
  import android.widget.ImageView;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Locale;
  
  import zesteve.com.myapplication.location.LocationTrack;
  
  import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
  import static android.Manifest.permission.ACCESS_FINE_LOCATION;
  
  
  public class SplashActivity extends AppCompatActivity {
  
      TextView welcomeText;
      ImageView mLogo;
      ImageView zbgimg;
  
      Typeface tf1;
  
      private Session session;
  
      private ArrayList&lt;String&gt; permissionsToRequest;
      private ArrayList&lt;String&gt; permissionsRejected = new ArrayList&lt;&gt;();
      private ArrayList&lt;String&gt; permissions = new ArrayList&lt;&gt;();
  
      private final static int ALL_PERMISSIONS_RESULT = 101;
      LocationTrack locationTrack;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                  WindowManager.LayoutParams.FLAG_FULLSCREEN);
  
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_splash);
  
          session = new Session(SplashActivity.this);
  
          //GPS Tracker
          permissions.add(ACCESS_FINE_LOCATION);
          permissions.add(ACCESS_COARSE_LOCATION);
  
          permissionsToRequest = findUnAskedPermissions(permissions);
          //get the permissions we have asked for before but are not granted..
          //we will store this in a global list to access later.
  
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
              if (permissionsToRequest.size() &gt; 0)
                  requestPermissions(permissionsToRequest.toArray(new String[permissionsToRequest.size()]), ALL_PERMISSIONS_RESULT);
          }
  
          locationTrack = new LocationTrack(SplashActivity.this);
  
          if (locationTrack.canGetLocation()) {
              double longitude = locationTrack.getLongitude();
              double latitude = locationTrack.getLatitude();
  
              Geocoder gcd = new Geocoder(SplashActivity.this, Locale.getDefault());
              List&lt;Address&gt; addresses = null;
              try {
                  addresses = gcd.getFromLocation(latitude, longitude, 1);
              } catch (IOException e) {
                  e.printStackTrace();
              }
              if (addresses != null &amp;&amp; addresses.size() &gt; 0) {
                  String locality = addresses.get(0).getLocality();
                  String subLocality = addresses.get(0).getSubLocality();
                  String address = addresses.get(0).getAddressLine(0);
                  String state = addresses.get(0).getAdminArea();
                  String countryn = addresses.get(0).getCountryName();
                  String postalCode = addresses.get(0).getPostalCode();
                  String knownName = addresses.get(0).getFeatureName();
  
  
                  session.setLocation(latitude,longitude,locality,subLocality);
  
                  //Toast.makeText(SplashActivity.this,locality +" "+ postalCode + " " + latitude+ " " + longitude,Toast.LENGTH_SHORT).show();
              }
          } else {
              locationTrack.showSettingsAlert();
          }
  
          welcomeText = (TextView) findViewById(R.id.welcome);
          tf1 = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Light.ttf");
          welcomeText.setTypeface(tf1);
          mLogo = (ImageView) findViewById(R.id.applogo);
          zbgimg = (ImageView) findViewById(R.id.zbgimg);
          zbgimg.setImageResource(R.drawable.splash_screen_option_three);
  
  
          animation2();
          animation3();
  
          new Handler().postDelayed(new Runnable() {
  
              @SuppressLint("PrivateResource")
              @Override
              public void run() {
                  if (session.FbLoggedIn()) {
                      Intent i = new Intent(SplashActivity.this,
                              MainActivity.class);
                      startActivity(i);
                      finish();
                      overridePendingTransition(R.anim.entry, R.anim.exit);
                  } else {
                      Intent i = new Intent(SplashActivity.this,
                              LoginActivity.class);
                      startActivity(i);
                      finish();
                      overridePendingTransition(R.anim.entry, R.anim.exit);
                  }
  
              }
  
          }, 3000);
      }
  
  
      private void animation2() {
          mLogo.setAlpha(1.0F);
          Animation anim = AnimationUtils.loadAnimation(SplashActivity.this, R.anim.translate_top_to_center);
          mLogo.startAnimation(anim);
      }
  
      private void animation3() {
          ObjectAnimator alphaAnimation = ObjectAnimator.ofFloat(welcomeText, "alpha", 0.0F, 1.0F);
          alphaAnimation.setStartDelay(1700);
          alphaAnimation.setDuration(500);
          alphaAnimation.start();
      }
  
  
      ///GPS Tracker
      private ArrayList&lt;String&gt; findUnAskedPermissions(ArrayList&lt;String&gt; wanted) {
          ArrayList&lt;String&gt; result = new ArrayList&lt;String&gt;();
  
          for (String perm : wanted) {
              if (!hasPermission(perm)) {
                  result.add(perm);
              }
          }
  
          return result;
      }
  
      private boolean hasPermission(String permission) {
          if (canMakeSmores()) {
              if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
                  return (checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED);
              }
          }
          return true;
      }
  
      private boolean canMakeSmores() {
          return (Build.VERSION.SDK_INT &gt; Build.VERSION_CODES.LOLLIPOP_MR1);
      }
  
  
      @TargetApi(Build.VERSION_CODES.M)
      @Override
      public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  
          switch (requestCode) {
  
              case ALL_PERMISSIONS_RESULT:
                  for (String perms : permissionsToRequest) {
                      if (!hasPermission(perms)) {
                          permissionsRejected.add(perms);
                      }
                  }
  
                  if (permissionsRejected.size() &gt; 0) {
  
  
                      if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
                          if (shouldShowRequestPermissionRationale(permissionsRejected.get(0))) {
                              showMessageOKCancel("These permissions are mandatory for the application. Please allow access.",
                                      new DialogInterface.OnClickListener() {
                                          @Override
                                          public void onClick(DialogInterface dialog, int which) {
                                              if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
                                                  requestPermissions(permissionsRejected.toArray(new String[permissionsRejected.size()]), ALL_PERMISSIONS_RESULT);
                                              }
                                          }
                                      });
                              return;
                          }
                      }
  
                  }
  
                  break;
          }
  
      }
  
      private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
          new AlertDialog.Builder(SplashActivity.this)
                  .setMessage(message)
                  .setPositiveButton("OK", okListener)
                  .setNegativeButton("Cancel", null)
                  .create()
                  .show();
      }
  
      @Override
      protected void onDestroy() {
          super.onDestroy();
          locationTrack.stopListener();
      }
  </code></pre>
  
  <p>If you do not understand please comment.</p>