Splashscreen with runtime permissions










1















I have a splash screen on launching my app. So when showing the splash screen run time permission should show. I got this code below from github. But runtime permissions are not showing. Splash screen works fine, but runtime permission is not working when add this code below. I have added permissions read sms, read external storage, access location. I have given all these permissions in the manifest file also.



 public class SplashActivity extends AppCompatActivity 



String loginstatus;
final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124;


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


final Handler handler = new Handler();
handler.postDelayed(new Runnable()


@Override

public void run()

Intent i = new Intent(SplashActivity.this, LoginActivity.class);

startActivity(i);

finish();



, 3000);

/* Thread background = new Thread()
public void run()

try
// Thread will sleep for 10 seconds
sleep(4*1000);

startActivity(new Intent(SplashScreen.this,HomeActivity.class));


finish();

catch (Exception e)



;

// start thread
background.start();*/


private void permissioncheck()
List<String> permissionsNeeded = new ArrayList<String>();

final List<String> permissionsList = new ArrayList<String>();
if (!addPermission(permissionsList, Manifest.permission.READ_EXTERNAL_STORAGE))
permissionsNeeded.add("READ");
if (!addPermission(permissionsList, Manifest.permission.ACCESS_COARSE_LOCATION))
permissionsNeeded.add("COURLOC");
if (!addPermission(permissionsList, Manifest.permission.ACCESS_FINE_LOCATION))
permissionsNeeded.add("FINELOC");
if (!addPermission(permissionsList, Manifest.permission.READ_SMS))
permissionsNeeded.add("SMS");

if (permissionsList.size() > 0)
if (permissionsNeeded.size() > 0)
// Need Rationale
String message = "You need to grant access to " + permissionsNeeded.get(0);
for (int i = 1; i < permissionsNeeded.size(); i++)
message = message + ", " + permissionsNeeded.get(i);
showMessageOKCancel(message,
new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)

if (Build.VERSION.SDK_INT >= 23)
// Marshmallow+
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);


else
// Pre-Marshmallow



);
return;


if (Build.VERSION.SDK_INT >= 23)
// Marshmallow+
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);


else
// Pre-Marshmallow



return;
else

// Toast.makeText(this,"Permission",Toast.LENGTH_LONG).show();
LaunchApp();


//insertDummyContact();



private boolean addPermission(List<String> permissionsList, String permission)

Boolean cond;
if (Build.VERSION.SDK_INT >= 23)
// Marshmallow+
if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED)
permissionsList.add(permission);
// Check for Rationale Option
if (!shouldShowRequestPermissionRationale(permission))
// return false;

cond = false;

// return true;

cond = true;


else
// Pre-Marshmallow
cond = true;


return cond;



private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener)
new AlertDialog.Builder(SplashActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();



@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions, @NonNull int grantResults)

//Checking the request code of our request
if (requestCode == 23)

//If permission is granted
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)

//Displaying a toast
Toast.makeText(this, "Permission granted", Toast.LENGTH_LONG).show();
else
//Displaying another toast if permission is not granted
Toast.makeText(this, "Permission Needed To Run The App", Toast.LENGTH_LONG).show();


if (requestCode == REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS)
Map<String, Integer> perms = new HashMap<String, Integer>();
// Initial
perms.put(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_COARSE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_SMS, PackageManager.PERMISSION_GRANTED);
//Toast.makeText(SplashScreen.this, " Permissions are jddddd", Toast.LENGTH_SHORT).show();
// Fill with results
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for ACCESS_FINE_LOCATION
if (perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
perms.get(Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED)
// All Permissions Granted
// insertDummyContact();

//Toast.makeText(SplashScreen.this, " Permissions are l", Toast.LENGTH_SHORT).show();
LaunchApp();

else
// Permission Denied
Toast.makeText(SplashActivity.this, "Some Permission is Denied", Toast.LENGTH_SHORT)
.show();

final Handler handler = new Handler();
handler.postDelayed(new Runnable()
@Override
public void run()
//Do something after 100
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
finish();

, 3000);





public void LaunchApp()

Thread background = new Thread()
public void run()

try
// Thread will sleep for 10 seconds
sleep(4*1000);

startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();

catch (Exception e)



;

// start thread
background.start();













share|improve this question
























  • Have you tried it isolate the failure? Providing minimized code for the problem will speed up getting the correct answer.

    – benc
    Nov 13 '18 at 8:42











  • have you added permissions you are requesting in your manifest file?

    – Karan Mer
    Nov 13 '18 at 8:50











  • @KaranMer Yeah I have already mmentioned it in my question that i have added it in manifest

    – Aseel Ali
    Nov 13 '18 at 8:51











  • you have not called your permissioncheck() function, call it in onCreate and see if its showing you permission dialog.

    – Karan Mer
    Nov 13 '18 at 8:54






  • 2





    also your handler will start new activity in 3 seconds only, what if user doesnt allow permission in those 3 seconds, instead of using handler, you can start your next activity in onPermissionResult()

    – Karan Mer
    Nov 13 '18 at 8:57
















1















I have a splash screen on launching my app. So when showing the splash screen run time permission should show. I got this code below from github. But runtime permissions are not showing. Splash screen works fine, but runtime permission is not working when add this code below. I have added permissions read sms, read external storage, access location. I have given all these permissions in the manifest file also.



 public class SplashActivity extends AppCompatActivity 



String loginstatus;
final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124;


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


final Handler handler = new Handler();
handler.postDelayed(new Runnable()


@Override

public void run()

Intent i = new Intent(SplashActivity.this, LoginActivity.class);

startActivity(i);

finish();



, 3000);

/* Thread background = new Thread()
public void run()

try
// Thread will sleep for 10 seconds
sleep(4*1000);

startActivity(new Intent(SplashScreen.this,HomeActivity.class));


finish();

catch (Exception e)



;

// start thread
background.start();*/


private void permissioncheck()
List<String> permissionsNeeded = new ArrayList<String>();

final List<String> permissionsList = new ArrayList<String>();
if (!addPermission(permissionsList, Manifest.permission.READ_EXTERNAL_STORAGE))
permissionsNeeded.add("READ");
if (!addPermission(permissionsList, Manifest.permission.ACCESS_COARSE_LOCATION))
permissionsNeeded.add("COURLOC");
if (!addPermission(permissionsList, Manifest.permission.ACCESS_FINE_LOCATION))
permissionsNeeded.add("FINELOC");
if (!addPermission(permissionsList, Manifest.permission.READ_SMS))
permissionsNeeded.add("SMS");

if (permissionsList.size() > 0)
if (permissionsNeeded.size() > 0)
// Need Rationale
String message = "You need to grant access to " + permissionsNeeded.get(0);
for (int i = 1; i < permissionsNeeded.size(); i++)
message = message + ", " + permissionsNeeded.get(i);
showMessageOKCancel(message,
new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)

if (Build.VERSION.SDK_INT >= 23)
// Marshmallow+
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);


else
// Pre-Marshmallow



);
return;


if (Build.VERSION.SDK_INT >= 23)
// Marshmallow+
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);


else
// Pre-Marshmallow



return;
else

// Toast.makeText(this,"Permission",Toast.LENGTH_LONG).show();
LaunchApp();


//insertDummyContact();



private boolean addPermission(List<String> permissionsList, String permission)

Boolean cond;
if (Build.VERSION.SDK_INT >= 23)
// Marshmallow+
if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED)
permissionsList.add(permission);
// Check for Rationale Option
if (!shouldShowRequestPermissionRationale(permission))
// return false;

cond = false;

// return true;

cond = true;


else
// Pre-Marshmallow
cond = true;


return cond;



private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener)
new AlertDialog.Builder(SplashActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();



@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions, @NonNull int grantResults)

//Checking the request code of our request
if (requestCode == 23)

//If permission is granted
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)

//Displaying a toast
Toast.makeText(this, "Permission granted", Toast.LENGTH_LONG).show();
else
//Displaying another toast if permission is not granted
Toast.makeText(this, "Permission Needed To Run The App", Toast.LENGTH_LONG).show();


if (requestCode == REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS)
Map<String, Integer> perms = new HashMap<String, Integer>();
// Initial
perms.put(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_COARSE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_SMS, PackageManager.PERMISSION_GRANTED);
//Toast.makeText(SplashScreen.this, " Permissions are jddddd", Toast.LENGTH_SHORT).show();
// Fill with results
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for ACCESS_FINE_LOCATION
if (perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
perms.get(Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED)
// All Permissions Granted
// insertDummyContact();

//Toast.makeText(SplashScreen.this, " Permissions are l", Toast.LENGTH_SHORT).show();
LaunchApp();

else
// Permission Denied
Toast.makeText(SplashActivity.this, "Some Permission is Denied", Toast.LENGTH_SHORT)
.show();

final Handler handler = new Handler();
handler.postDelayed(new Runnable()
@Override
public void run()
//Do something after 100
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
finish();

, 3000);





public void LaunchApp()

Thread background = new Thread()
public void run()

try
// Thread will sleep for 10 seconds
sleep(4*1000);

startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();

catch (Exception e)



;

// start thread
background.start();













share|improve this question
























  • Have you tried it isolate the failure? Providing minimized code for the problem will speed up getting the correct answer.

    – benc
    Nov 13 '18 at 8:42











  • have you added permissions you are requesting in your manifest file?

    – Karan Mer
    Nov 13 '18 at 8:50











  • @KaranMer Yeah I have already mmentioned it in my question that i have added it in manifest

    – Aseel Ali
    Nov 13 '18 at 8:51











  • you have not called your permissioncheck() function, call it in onCreate and see if its showing you permission dialog.

    – Karan Mer
    Nov 13 '18 at 8:54






  • 2





    also your handler will start new activity in 3 seconds only, what if user doesnt allow permission in those 3 seconds, instead of using handler, you can start your next activity in onPermissionResult()

    – Karan Mer
    Nov 13 '18 at 8:57














1












1








1








I have a splash screen on launching my app. So when showing the splash screen run time permission should show. I got this code below from github. But runtime permissions are not showing. Splash screen works fine, but runtime permission is not working when add this code below. I have added permissions read sms, read external storage, access location. I have given all these permissions in the manifest file also.



 public class SplashActivity extends AppCompatActivity 



String loginstatus;
final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124;


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


final Handler handler = new Handler();
handler.postDelayed(new Runnable()


@Override

public void run()

Intent i = new Intent(SplashActivity.this, LoginActivity.class);

startActivity(i);

finish();



, 3000);

/* Thread background = new Thread()
public void run()

try
// Thread will sleep for 10 seconds
sleep(4*1000);

startActivity(new Intent(SplashScreen.this,HomeActivity.class));


finish();

catch (Exception e)



;

// start thread
background.start();*/


private void permissioncheck()
List<String> permissionsNeeded = new ArrayList<String>();

final List<String> permissionsList = new ArrayList<String>();
if (!addPermission(permissionsList, Manifest.permission.READ_EXTERNAL_STORAGE))
permissionsNeeded.add("READ");
if (!addPermission(permissionsList, Manifest.permission.ACCESS_COARSE_LOCATION))
permissionsNeeded.add("COURLOC");
if (!addPermission(permissionsList, Manifest.permission.ACCESS_FINE_LOCATION))
permissionsNeeded.add("FINELOC");
if (!addPermission(permissionsList, Manifest.permission.READ_SMS))
permissionsNeeded.add("SMS");

if (permissionsList.size() > 0)
if (permissionsNeeded.size() > 0)
// Need Rationale
String message = "You need to grant access to " + permissionsNeeded.get(0);
for (int i = 1; i < permissionsNeeded.size(); i++)
message = message + ", " + permissionsNeeded.get(i);
showMessageOKCancel(message,
new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)

if (Build.VERSION.SDK_INT >= 23)
// Marshmallow+
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);


else
// Pre-Marshmallow



);
return;


if (Build.VERSION.SDK_INT >= 23)
// Marshmallow+
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);


else
// Pre-Marshmallow



return;
else

// Toast.makeText(this,"Permission",Toast.LENGTH_LONG).show();
LaunchApp();


//insertDummyContact();



private boolean addPermission(List<String> permissionsList, String permission)

Boolean cond;
if (Build.VERSION.SDK_INT >= 23)
// Marshmallow+
if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED)
permissionsList.add(permission);
// Check for Rationale Option
if (!shouldShowRequestPermissionRationale(permission))
// return false;

cond = false;

// return true;

cond = true;


else
// Pre-Marshmallow
cond = true;


return cond;



private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener)
new AlertDialog.Builder(SplashActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();



@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions, @NonNull int grantResults)

//Checking the request code of our request
if (requestCode == 23)

//If permission is granted
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)

//Displaying a toast
Toast.makeText(this, "Permission granted", Toast.LENGTH_LONG).show();
else
//Displaying another toast if permission is not granted
Toast.makeText(this, "Permission Needed To Run The App", Toast.LENGTH_LONG).show();


if (requestCode == REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS)
Map<String, Integer> perms = new HashMap<String, Integer>();
// Initial
perms.put(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_COARSE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_SMS, PackageManager.PERMISSION_GRANTED);
//Toast.makeText(SplashScreen.this, " Permissions are jddddd", Toast.LENGTH_SHORT).show();
// Fill with results
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for ACCESS_FINE_LOCATION
if (perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
perms.get(Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED)
// All Permissions Granted
// insertDummyContact();

//Toast.makeText(SplashScreen.this, " Permissions are l", Toast.LENGTH_SHORT).show();
LaunchApp();

else
// Permission Denied
Toast.makeText(SplashActivity.this, "Some Permission is Denied", Toast.LENGTH_SHORT)
.show();

final Handler handler = new Handler();
handler.postDelayed(new Runnable()
@Override
public void run()
//Do something after 100
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
finish();

, 3000);





public void LaunchApp()

Thread background = new Thread()
public void run()

try
// Thread will sleep for 10 seconds
sleep(4*1000);

startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();

catch (Exception e)



;

// start thread
background.start();













share|improve this question
















I have a splash screen on launching my app. So when showing the splash screen run time permission should show. I got this code below from github. But runtime permissions are not showing. Splash screen works fine, but runtime permission is not working when add this code below. I have added permissions read sms, read external storage, access location. I have given all these permissions in the manifest file also.



 public class SplashActivity extends AppCompatActivity 



String loginstatus;
final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124;


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


final Handler handler = new Handler();
handler.postDelayed(new Runnable()


@Override

public void run()

Intent i = new Intent(SplashActivity.this, LoginActivity.class);

startActivity(i);

finish();



, 3000);

/* Thread background = new Thread()
public void run()

try
// Thread will sleep for 10 seconds
sleep(4*1000);

startActivity(new Intent(SplashScreen.this,HomeActivity.class));


finish();

catch (Exception e)



;

// start thread
background.start();*/


private void permissioncheck()
List<String> permissionsNeeded = new ArrayList<String>();

final List<String> permissionsList = new ArrayList<String>();
if (!addPermission(permissionsList, Manifest.permission.READ_EXTERNAL_STORAGE))
permissionsNeeded.add("READ");
if (!addPermission(permissionsList, Manifest.permission.ACCESS_COARSE_LOCATION))
permissionsNeeded.add("COURLOC");
if (!addPermission(permissionsList, Manifest.permission.ACCESS_FINE_LOCATION))
permissionsNeeded.add("FINELOC");
if (!addPermission(permissionsList, Manifest.permission.READ_SMS))
permissionsNeeded.add("SMS");

if (permissionsList.size() > 0)
if (permissionsNeeded.size() > 0)
// Need Rationale
String message = "You need to grant access to " + permissionsNeeded.get(0);
for (int i = 1; i < permissionsNeeded.size(); i++)
message = message + ", " + permissionsNeeded.get(i);
showMessageOKCancel(message,
new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)

if (Build.VERSION.SDK_INT >= 23)
// Marshmallow+
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);


else
// Pre-Marshmallow



);
return;


if (Build.VERSION.SDK_INT >= 23)
// Marshmallow+
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);


else
// Pre-Marshmallow



return;
else

// Toast.makeText(this,"Permission",Toast.LENGTH_LONG).show();
LaunchApp();


//insertDummyContact();



private boolean addPermission(List<String> permissionsList, String permission)

Boolean cond;
if (Build.VERSION.SDK_INT >= 23)
// Marshmallow+
if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED)
permissionsList.add(permission);
// Check for Rationale Option
if (!shouldShowRequestPermissionRationale(permission))
// return false;

cond = false;

// return true;

cond = true;


else
// Pre-Marshmallow
cond = true;


return cond;



private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener)
new AlertDialog.Builder(SplashActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();



@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions, @NonNull int grantResults)

//Checking the request code of our request
if (requestCode == 23)

//If permission is granted
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)

//Displaying a toast
Toast.makeText(this, "Permission granted", Toast.LENGTH_LONG).show();
else
//Displaying another toast if permission is not granted
Toast.makeText(this, "Permission Needed To Run The App", Toast.LENGTH_LONG).show();


if (requestCode == REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS)
Map<String, Integer> perms = new HashMap<String, Integer>();
// Initial
perms.put(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_COARSE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_SMS, PackageManager.PERMISSION_GRANTED);
//Toast.makeText(SplashScreen.this, " Permissions are jddddd", Toast.LENGTH_SHORT).show();
// Fill with results
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for ACCESS_FINE_LOCATION
if (perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
perms.get(Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED)
// All Permissions Granted
// insertDummyContact();

//Toast.makeText(SplashScreen.this, " Permissions are l", Toast.LENGTH_SHORT).show();
LaunchApp();

else
// Permission Denied
Toast.makeText(SplashActivity.this, "Some Permission is Denied", Toast.LENGTH_SHORT)
.show();

final Handler handler = new Handler();
handler.postDelayed(new Runnable()
@Override
public void run()
//Do something after 100
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
finish();

, 3000);





public void LaunchApp()

Thread background = new Thread()
public void run()

try
// Thread will sleep for 10 seconds
sleep(4*1000);

startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();

catch (Exception e)



;

// start thread
background.start();










android runtime-permissions






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 9:00









Rahul Chandrabhan

1,71241222




1,71241222










asked Nov 13 '18 at 8:32









Aseel AliAseel Ali

255




255












  • Have you tried it isolate the failure? Providing minimized code for the problem will speed up getting the correct answer.

    – benc
    Nov 13 '18 at 8:42











  • have you added permissions you are requesting in your manifest file?

    – Karan Mer
    Nov 13 '18 at 8:50











  • @KaranMer Yeah I have already mmentioned it in my question that i have added it in manifest

    – Aseel Ali
    Nov 13 '18 at 8:51











  • you have not called your permissioncheck() function, call it in onCreate and see if its showing you permission dialog.

    – Karan Mer
    Nov 13 '18 at 8:54






  • 2





    also your handler will start new activity in 3 seconds only, what if user doesnt allow permission in those 3 seconds, instead of using handler, you can start your next activity in onPermissionResult()

    – Karan Mer
    Nov 13 '18 at 8:57


















  • Have you tried it isolate the failure? Providing minimized code for the problem will speed up getting the correct answer.

    – benc
    Nov 13 '18 at 8:42











  • have you added permissions you are requesting in your manifest file?

    – Karan Mer
    Nov 13 '18 at 8:50











  • @KaranMer Yeah I have already mmentioned it in my question that i have added it in manifest

    – Aseel Ali
    Nov 13 '18 at 8:51











  • you have not called your permissioncheck() function, call it in onCreate and see if its showing you permission dialog.

    – Karan Mer
    Nov 13 '18 at 8:54






  • 2





    also your handler will start new activity in 3 seconds only, what if user doesnt allow permission in those 3 seconds, instead of using handler, you can start your next activity in onPermissionResult()

    – Karan Mer
    Nov 13 '18 at 8:57

















Have you tried it isolate the failure? Providing minimized code for the problem will speed up getting the correct answer.

– benc
Nov 13 '18 at 8:42





Have you tried it isolate the failure? Providing minimized code for the problem will speed up getting the correct answer.

– benc
Nov 13 '18 at 8:42













have you added permissions you are requesting in your manifest file?

– Karan Mer
Nov 13 '18 at 8:50





have you added permissions you are requesting in your manifest file?

– Karan Mer
Nov 13 '18 at 8:50













@KaranMer Yeah I have already mmentioned it in my question that i have added it in manifest

– Aseel Ali
Nov 13 '18 at 8:51





@KaranMer Yeah I have already mmentioned it in my question that i have added it in manifest

– Aseel Ali
Nov 13 '18 at 8:51













you have not called your permissioncheck() function, call it in onCreate and see if its showing you permission dialog.

– Karan Mer
Nov 13 '18 at 8:54





you have not called your permissioncheck() function, call it in onCreate and see if its showing you permission dialog.

– Karan Mer
Nov 13 '18 at 8:54




2




2





also your handler will start new activity in 3 seconds only, what if user doesnt allow permission in those 3 seconds, instead of using handler, you can start your next activity in onPermissionResult()

– Karan Mer
Nov 13 '18 at 8:57






also your handler will start new activity in 3 seconds only, what if user doesnt allow permission in those 3 seconds, instead of using handler, you can start your next activity in onPermissionResult()

– Karan Mer
Nov 13 '18 at 8:57













2 Answers
2






active

oldest

votes


















1














You need to call permissioncheck() on onCreate() method after setContentView(). Replace the code provided below with your onCreate() method.



@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.splashfile);
permissioncheck();



I removed Handler codes on onCreate() method. Instead, use onRequestPermissionResult() and start LoginActivity from it. Replace the following code to your onRequestPermissionResult().



 @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions, @NonNull int grantResults)

//Checking the request code of our request
if (requestCode == 23)

//If permission is granted
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)

//Displaying a toast
Toast.makeText(this, "Permission granted", Toast.LENGTH_LONG).show();
else
//Displaying another toast if permission is not granted
Toast.makeText(this, "Permission Needed To Run The App", Toast.LENGTH_LONG).show();


if (requestCode == REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS)
Map<String, Integer> perms = new HashMap<String, Integer>();
// Initial
perms.put(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_COARSE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_SMS, PackageManager.PERMISSION_GRANTED);
//Toast.makeText(SplashScreen.this, " Permissions are jddddd", Toast.LENGTH_SHORT).show();
// Fill with results
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for ACCESS_FINE_LOCATION
if (perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
perms.get(Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED)
// All Permissions Granted
// Here start the activity
final Handler handler = new Handler();
handler.postDelayed(new Runnable()

@Override
public void run()
Intent i = new Intent(SplashActivity.this, LoginActivity.class);
startActivity(i);
finish();



, 3000);

else
// Permission Denied
Toast.makeText(SplashActivity.this, "Some Permission is Denied", Toast.LENGTH_SHORT)
.show();

finish();







Please carefully read comments in the code provided above.







share|improve this answer

























  • At where shoul i call this?

    – Aseel Ali
    Nov 13 '18 at 8:41











  • I have already provided you with comment line in the code.

    – Zwal Pyae Kyaw
    Nov 13 '18 at 8:42












  • still not working

    – Aseel Ali
    Nov 13 '18 at 8:50











  • hey. now i noticed this. after the splash screen appears the login page comes so presssed the back button to exit the app. then its asking these permission

    – Aseel Ali
    Nov 13 '18 at 8:55











  • I've added more answers to my main answer.

    – Zwal Pyae Kyaw
    Nov 13 '18 at 9:08


















1














Best Code



 @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public class Firstclass extends AppCompatActivity




private int timeoutMillis = 5000;

/** The time when this @link AppCompatActivity was created. */

private long startTimeMillis = 0;

/** The code used when requesting permissions */

private static final int PERMISSIONS_REQUEST = 1234;


public int getTimeoutMillis()
return timeoutMillis;



@SuppressWarnings("rawtypes")
public Class getNextActivityClass()
return SecondActivity.class;
;


public String getRequiredPermissions()
String permissions = null;
try
permissions = getPackageManager().getPackageInfo(getPackageName(),
PackageManager.GET_PERMISSIONS).requestedPermissions;
catch (PackageManager.NameNotFoundException e)
e.printStackTrace();

if (permissions == null)
return new String[0];
else
return permissions.clone();



@TargetApi(23)
@Override
protected void onCreate(Bundle savedInstanceState)

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
startTimeMillis = System.currentTimeMillis();


if (Build.VERSION.SDK_INT >= 23)
checkPermissions();
else
startNextActivity();




@TargetApi(23)
@Override
public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults)
if (requestCode == PERMISSIONS_REQUEST)
checkPermissions();



private void startNextActivity()
runOnUiThread(new Runnable()

@Override
public void run()


);
long delayMillis = getTimeoutMillis() - (System.currentTimeMillis() - startTimeMillis);
if (delayMillis < 0)
delayMillis = 0;

new Handler().postDelayed(new Runnable()
@Override
public void run()
startActivity(new Intent(Firstclass.this, getNextActivityClass()));
finish();

, delayMillis);



private void checkPermissions()
String ungrantedPermissions = requiredPermissionsStillNeeded();
if (ungrantedPermissions.length == 0)
startNextActivity();
else
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
requestPermissions(ungrantedPermissions, PERMISSIONS_REQUEST);




@TargetApi(23)
private String requiredPermissionsStillNeeded()

Set<String> permissions = new HashSet<String>();
for (String permission : getRequiredPermissions())
permissions.add(permission);

for (Iterator<String> i = permissions.iterator(); i.hasNext();)
String permission = i.next();
if (checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED)
Log.d(Firstclass.class.getSimpleName(),
"Permission: " + permission + " already granted.");
i.remove();
else
Log.d(Firstclass.class.getSimpleName(),
"Permission: " + permission + " not yet granted.");


return permissions.toArray(new String[permissions.size()]);




Manifest xml permission



<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276818%2fsplashscreen-with-runtime-permissions%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You need to call permissioncheck() on onCreate() method after setContentView(). Replace the code provided below with your onCreate() method.



    @Override
    protected void onCreate(Bundle savedInstanceState)
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splashfile);
    permissioncheck();



    I removed Handler codes on onCreate() method. Instead, use onRequestPermissionResult() and start LoginActivity from it. Replace the following code to your onRequestPermissionResult().



     @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions, @NonNull int grantResults)

    //Checking the request code of our request
    if (requestCode == 23)

    //If permission is granted
    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)

    //Displaying a toast
    Toast.makeText(this, "Permission granted", Toast.LENGTH_LONG).show();
    else
    //Displaying another toast if permission is not granted
    Toast.makeText(this, "Permission Needed To Run The App", Toast.LENGTH_LONG).show();


    if (requestCode == REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS)
    Map<String, Integer> perms = new HashMap<String, Integer>();
    // Initial
    perms.put(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
    perms.put(Manifest.permission.ACCESS_COARSE_LOCATION, PackageManager.PERMISSION_GRANTED);
    perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
    perms.put(Manifest.permission.READ_SMS, PackageManager.PERMISSION_GRANTED);
    //Toast.makeText(SplashScreen.this, " Permissions are jddddd", Toast.LENGTH_SHORT).show();
    // Fill with results
    for (int i = 0; i < permissions.length; i++)
    perms.put(permissions[i], grantResults[i]);
    // Check for ACCESS_FINE_LOCATION
    if (perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
    && perms.get(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
    perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
    perms.get(Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED)
    // All Permissions Granted
    // Here start the activity
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable()

    @Override
    public void run()
    Intent i = new Intent(SplashActivity.this, LoginActivity.class);
    startActivity(i);
    finish();



    , 3000);

    else
    // Permission Denied
    Toast.makeText(SplashActivity.this, "Some Permission is Denied", Toast.LENGTH_SHORT)
    .show();

    finish();







    Please carefully read comments in the code provided above.







    share|improve this answer

























    • At where shoul i call this?

      – Aseel Ali
      Nov 13 '18 at 8:41











    • I have already provided you with comment line in the code.

      – Zwal Pyae Kyaw
      Nov 13 '18 at 8:42












    • still not working

      – Aseel Ali
      Nov 13 '18 at 8:50











    • hey. now i noticed this. after the splash screen appears the login page comes so presssed the back button to exit the app. then its asking these permission

      – Aseel Ali
      Nov 13 '18 at 8:55











    • I've added more answers to my main answer.

      – Zwal Pyae Kyaw
      Nov 13 '18 at 9:08















    1














    You need to call permissioncheck() on onCreate() method after setContentView(). Replace the code provided below with your onCreate() method.



    @Override
    protected void onCreate(Bundle savedInstanceState)
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splashfile);
    permissioncheck();



    I removed Handler codes on onCreate() method. Instead, use onRequestPermissionResult() and start LoginActivity from it. Replace the following code to your onRequestPermissionResult().



     @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions, @NonNull int grantResults)

    //Checking the request code of our request
    if (requestCode == 23)

    //If permission is granted
    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)

    //Displaying a toast
    Toast.makeText(this, "Permission granted", Toast.LENGTH_LONG).show();
    else
    //Displaying another toast if permission is not granted
    Toast.makeText(this, "Permission Needed To Run The App", Toast.LENGTH_LONG).show();


    if (requestCode == REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS)
    Map<String, Integer> perms = new HashMap<String, Integer>();
    // Initial
    perms.put(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
    perms.put(Manifest.permission.ACCESS_COARSE_LOCATION, PackageManager.PERMISSION_GRANTED);
    perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
    perms.put(Manifest.permission.READ_SMS, PackageManager.PERMISSION_GRANTED);
    //Toast.makeText(SplashScreen.this, " Permissions are jddddd", Toast.LENGTH_SHORT).show();
    // Fill with results
    for (int i = 0; i < permissions.length; i++)
    perms.put(permissions[i], grantResults[i]);
    // Check for ACCESS_FINE_LOCATION
    if (perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
    && perms.get(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
    perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
    perms.get(Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED)
    // All Permissions Granted
    // Here start the activity
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable()

    @Override
    public void run()
    Intent i = new Intent(SplashActivity.this, LoginActivity.class);
    startActivity(i);
    finish();



    , 3000);

    else
    // Permission Denied
    Toast.makeText(SplashActivity.this, "Some Permission is Denied", Toast.LENGTH_SHORT)
    .show();

    finish();







    Please carefully read comments in the code provided above.







    share|improve this answer

























    • At where shoul i call this?

      – Aseel Ali
      Nov 13 '18 at 8:41











    • I have already provided you with comment line in the code.

      – Zwal Pyae Kyaw
      Nov 13 '18 at 8:42












    • still not working

      – Aseel Ali
      Nov 13 '18 at 8:50











    • hey. now i noticed this. after the splash screen appears the login page comes so presssed the back button to exit the app. then its asking these permission

      – Aseel Ali
      Nov 13 '18 at 8:55











    • I've added more answers to my main answer.

      – Zwal Pyae Kyaw
      Nov 13 '18 at 9:08













    1












    1








    1







    You need to call permissioncheck() on onCreate() method after setContentView(). Replace the code provided below with your onCreate() method.



    @Override
    protected void onCreate(Bundle savedInstanceState)
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splashfile);
    permissioncheck();



    I removed Handler codes on onCreate() method. Instead, use onRequestPermissionResult() and start LoginActivity from it. Replace the following code to your onRequestPermissionResult().



     @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions, @NonNull int grantResults)

    //Checking the request code of our request
    if (requestCode == 23)

    //If permission is granted
    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)

    //Displaying a toast
    Toast.makeText(this, "Permission granted", Toast.LENGTH_LONG).show();
    else
    //Displaying another toast if permission is not granted
    Toast.makeText(this, "Permission Needed To Run The App", Toast.LENGTH_LONG).show();


    if (requestCode == REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS)
    Map<String, Integer> perms = new HashMap<String, Integer>();
    // Initial
    perms.put(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
    perms.put(Manifest.permission.ACCESS_COARSE_LOCATION, PackageManager.PERMISSION_GRANTED);
    perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
    perms.put(Manifest.permission.READ_SMS, PackageManager.PERMISSION_GRANTED);
    //Toast.makeText(SplashScreen.this, " Permissions are jddddd", Toast.LENGTH_SHORT).show();
    // Fill with results
    for (int i = 0; i < permissions.length; i++)
    perms.put(permissions[i], grantResults[i]);
    // Check for ACCESS_FINE_LOCATION
    if (perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
    && perms.get(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
    perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
    perms.get(Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED)
    // All Permissions Granted
    // Here start the activity
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable()

    @Override
    public void run()
    Intent i = new Intent(SplashActivity.this, LoginActivity.class);
    startActivity(i);
    finish();



    , 3000);

    else
    // Permission Denied
    Toast.makeText(SplashActivity.this, "Some Permission is Denied", Toast.LENGTH_SHORT)
    .show();

    finish();







    Please carefully read comments in the code provided above.







    share|improve this answer















    You need to call permissioncheck() on onCreate() method after setContentView(). Replace the code provided below with your onCreate() method.



    @Override
    protected void onCreate(Bundle savedInstanceState)
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splashfile);
    permissioncheck();



    I removed Handler codes on onCreate() method. Instead, use onRequestPermissionResult() and start LoginActivity from it. Replace the following code to your onRequestPermissionResult().



     @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions, @NonNull int grantResults)

    //Checking the request code of our request
    if (requestCode == 23)

    //If permission is granted
    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)

    //Displaying a toast
    Toast.makeText(this, "Permission granted", Toast.LENGTH_LONG).show();
    else
    //Displaying another toast if permission is not granted
    Toast.makeText(this, "Permission Needed To Run The App", Toast.LENGTH_LONG).show();


    if (requestCode == REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS)
    Map<String, Integer> perms = new HashMap<String, Integer>();
    // Initial
    perms.put(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
    perms.put(Manifest.permission.ACCESS_COARSE_LOCATION, PackageManager.PERMISSION_GRANTED);
    perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
    perms.put(Manifest.permission.READ_SMS, PackageManager.PERMISSION_GRANTED);
    //Toast.makeText(SplashScreen.this, " Permissions are jddddd", Toast.LENGTH_SHORT).show();
    // Fill with results
    for (int i = 0; i < permissions.length; i++)
    perms.put(permissions[i], grantResults[i]);
    // Check for ACCESS_FINE_LOCATION
    if (perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
    && perms.get(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
    perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
    perms.get(Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED)
    // All Permissions Granted
    // Here start the activity
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable()

    @Override
    public void run()
    Intent i = new Intent(SplashActivity.this, LoginActivity.class);
    startActivity(i);
    finish();



    , 3000);

    else
    // Permission Denied
    Toast.makeText(SplashActivity.this, "Some Permission is Denied", Toast.LENGTH_SHORT)
    .show();

    finish();







    Please carefully read comments in the code provided above.








    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 13 '18 at 9:26

























    answered Nov 13 '18 at 8:39









    Zwal Pyae KyawZwal Pyae Kyaw

    878




    878












    • At where shoul i call this?

      – Aseel Ali
      Nov 13 '18 at 8:41











    • I have already provided you with comment line in the code.

      – Zwal Pyae Kyaw
      Nov 13 '18 at 8:42












    • still not working

      – Aseel Ali
      Nov 13 '18 at 8:50











    • hey. now i noticed this. after the splash screen appears the login page comes so presssed the back button to exit the app. then its asking these permission

      – Aseel Ali
      Nov 13 '18 at 8:55











    • I've added more answers to my main answer.

      – Zwal Pyae Kyaw
      Nov 13 '18 at 9:08

















    • At where shoul i call this?

      – Aseel Ali
      Nov 13 '18 at 8:41











    • I have already provided you with comment line in the code.

      – Zwal Pyae Kyaw
      Nov 13 '18 at 8:42












    • still not working

      – Aseel Ali
      Nov 13 '18 at 8:50











    • hey. now i noticed this. after the splash screen appears the login page comes so presssed the back button to exit the app. then its asking these permission

      – Aseel Ali
      Nov 13 '18 at 8:55











    • I've added more answers to my main answer.

      – Zwal Pyae Kyaw
      Nov 13 '18 at 9:08
















    At where shoul i call this?

    – Aseel Ali
    Nov 13 '18 at 8:41





    At where shoul i call this?

    – Aseel Ali
    Nov 13 '18 at 8:41













    I have already provided you with comment line in the code.

    – Zwal Pyae Kyaw
    Nov 13 '18 at 8:42






    I have already provided you with comment line in the code.

    – Zwal Pyae Kyaw
    Nov 13 '18 at 8:42














    still not working

    – Aseel Ali
    Nov 13 '18 at 8:50





    still not working

    – Aseel Ali
    Nov 13 '18 at 8:50













    hey. now i noticed this. after the splash screen appears the login page comes so presssed the back button to exit the app. then its asking these permission

    – Aseel Ali
    Nov 13 '18 at 8:55





    hey. now i noticed this. after the splash screen appears the login page comes so presssed the back button to exit the app. then its asking these permission

    – Aseel Ali
    Nov 13 '18 at 8:55













    I've added more answers to my main answer.

    – Zwal Pyae Kyaw
    Nov 13 '18 at 9:08





    I've added more answers to my main answer.

    – Zwal Pyae Kyaw
    Nov 13 '18 at 9:08













    1














    Best Code



     @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    public class Firstclass extends AppCompatActivity




    private int timeoutMillis = 5000;

    /** The time when this @link AppCompatActivity was created. */

    private long startTimeMillis = 0;

    /** The code used when requesting permissions */

    private static final int PERMISSIONS_REQUEST = 1234;


    public int getTimeoutMillis()
    return timeoutMillis;



    @SuppressWarnings("rawtypes")
    public Class getNextActivityClass()
    return SecondActivity.class;
    ;


    public String getRequiredPermissions()
    String permissions = null;
    try
    permissions = getPackageManager().getPackageInfo(getPackageName(),
    PackageManager.GET_PERMISSIONS).requestedPermissions;
    catch (PackageManager.NameNotFoundException e)
    e.printStackTrace();

    if (permissions == null)
    return new String[0];
    else
    return permissions.clone();



    @TargetApi(23)
    @Override
    protected void onCreate(Bundle savedInstanceState)

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    startTimeMillis = System.currentTimeMillis();


    if (Build.VERSION.SDK_INT >= 23)
    checkPermissions();
    else
    startNextActivity();




    @TargetApi(23)
    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults)
    if (requestCode == PERMISSIONS_REQUEST)
    checkPermissions();



    private void startNextActivity()
    runOnUiThread(new Runnable()

    @Override
    public void run()


    );
    long delayMillis = getTimeoutMillis() - (System.currentTimeMillis() - startTimeMillis);
    if (delayMillis < 0)
    delayMillis = 0;

    new Handler().postDelayed(new Runnable()
    @Override
    public void run()
    startActivity(new Intent(Firstclass.this, getNextActivityClass()));
    finish();

    , delayMillis);



    private void checkPermissions()
    String ungrantedPermissions = requiredPermissionsStillNeeded();
    if (ungrantedPermissions.length == 0)
    startNextActivity();
    else
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    requestPermissions(ungrantedPermissions, PERMISSIONS_REQUEST);




    @TargetApi(23)
    private String requiredPermissionsStillNeeded()

    Set<String> permissions = new HashSet<String>();
    for (String permission : getRequiredPermissions())
    permissions.add(permission);

    for (Iterator<String> i = permissions.iterator(); i.hasNext();)
    String permission = i.next();
    if (checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED)
    Log.d(Firstclass.class.getSimpleName(),
    "Permission: " + permission + " already granted.");
    i.remove();
    else
    Log.d(Firstclass.class.getSimpleName(),
    "Permission: " + permission + " not yet granted.");


    return permissions.toArray(new String[permissions.size()]);




    Manifest xml permission



    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS" />





    share|improve this answer





























      1














      Best Code



       @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
      public class Firstclass extends AppCompatActivity




      private int timeoutMillis = 5000;

      /** The time when this @link AppCompatActivity was created. */

      private long startTimeMillis = 0;

      /** The code used when requesting permissions */

      private static final int PERMISSIONS_REQUEST = 1234;


      public int getTimeoutMillis()
      return timeoutMillis;



      @SuppressWarnings("rawtypes")
      public Class getNextActivityClass()
      return SecondActivity.class;
      ;


      public String getRequiredPermissions()
      String permissions = null;
      try
      permissions = getPackageManager().getPackageInfo(getPackageName(),
      PackageManager.GET_PERMISSIONS).requestedPermissions;
      catch (PackageManager.NameNotFoundException e)
      e.printStackTrace();

      if (permissions == null)
      return new String[0];
      else
      return permissions.clone();



      @TargetApi(23)
      @Override
      protected void onCreate(Bundle savedInstanceState)

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);
      startTimeMillis = System.currentTimeMillis();


      if (Build.VERSION.SDK_INT >= 23)
      checkPermissions();
      else
      startNextActivity();




      @TargetApi(23)
      @Override
      public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults)
      if (requestCode == PERMISSIONS_REQUEST)
      checkPermissions();



      private void startNextActivity()
      runOnUiThread(new Runnable()

      @Override
      public void run()


      );
      long delayMillis = getTimeoutMillis() - (System.currentTimeMillis() - startTimeMillis);
      if (delayMillis < 0)
      delayMillis = 0;

      new Handler().postDelayed(new Runnable()
      @Override
      public void run()
      startActivity(new Intent(Firstclass.this, getNextActivityClass()));
      finish();

      , delayMillis);



      private void checkPermissions()
      String ungrantedPermissions = requiredPermissionsStillNeeded();
      if (ungrantedPermissions.length == 0)
      startNextActivity();
      else
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
      requestPermissions(ungrantedPermissions, PERMISSIONS_REQUEST);




      @TargetApi(23)
      private String requiredPermissionsStillNeeded()

      Set<String> permissions = new HashSet<String>();
      for (String permission : getRequiredPermissions())
      permissions.add(permission);

      for (Iterator<String> i = permissions.iterator(); i.hasNext();)
      String permission = i.next();
      if (checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED)
      Log.d(Firstclass.class.getSimpleName(),
      "Permission: " + permission + " already granted.");
      i.remove();
      else
      Log.d(Firstclass.class.getSimpleName(),
      "Permission: " + permission + " not yet granted.");


      return permissions.toArray(new String[permissions.size()]);




      Manifest xml permission



      <uses-permission android:name="android.permission.INTERNET" />
      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
      <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
      <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
      <uses-permission android:name="android.permission.READ_PHONE_STATE" />
      <uses-permission android:name="android.permission.READ_SMS" />
      <uses-permission android:name="android.permission.SEND_SMS" />





      share|improve this answer



























        1












        1








        1







        Best Code



         @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
        public class Firstclass extends AppCompatActivity




        private int timeoutMillis = 5000;

        /** The time when this @link AppCompatActivity was created. */

        private long startTimeMillis = 0;

        /** The code used when requesting permissions */

        private static final int PERMISSIONS_REQUEST = 1234;


        public int getTimeoutMillis()
        return timeoutMillis;



        @SuppressWarnings("rawtypes")
        public Class getNextActivityClass()
        return SecondActivity.class;
        ;


        public String getRequiredPermissions()
        String permissions = null;
        try
        permissions = getPackageManager().getPackageInfo(getPackageName(),
        PackageManager.GET_PERMISSIONS).requestedPermissions;
        catch (PackageManager.NameNotFoundException e)
        e.printStackTrace();

        if (permissions == null)
        return new String[0];
        else
        return permissions.clone();



        @TargetApi(23)
        @Override
        protected void onCreate(Bundle savedInstanceState)

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        startTimeMillis = System.currentTimeMillis();


        if (Build.VERSION.SDK_INT >= 23)
        checkPermissions();
        else
        startNextActivity();




        @TargetApi(23)
        @Override
        public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults)
        if (requestCode == PERMISSIONS_REQUEST)
        checkPermissions();



        private void startNextActivity()
        runOnUiThread(new Runnable()

        @Override
        public void run()


        );
        long delayMillis = getTimeoutMillis() - (System.currentTimeMillis() - startTimeMillis);
        if (delayMillis < 0)
        delayMillis = 0;

        new Handler().postDelayed(new Runnable()
        @Override
        public void run()
        startActivity(new Intent(Firstclass.this, getNextActivityClass()));
        finish();

        , delayMillis);



        private void checkPermissions()
        String ungrantedPermissions = requiredPermissionsStillNeeded();
        if (ungrantedPermissions.length == 0)
        startNextActivity();
        else
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        requestPermissions(ungrantedPermissions, PERMISSIONS_REQUEST);




        @TargetApi(23)
        private String requiredPermissionsStillNeeded()

        Set<String> permissions = new HashSet<String>();
        for (String permission : getRequiredPermissions())
        permissions.add(permission);

        for (Iterator<String> i = permissions.iterator(); i.hasNext();)
        String permission = i.next();
        if (checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED)
        Log.d(Firstclass.class.getSimpleName(),
        "Permission: " + permission + " already granted.");
        i.remove();
        else
        Log.d(Firstclass.class.getSimpleName(),
        "Permission: " + permission + " not yet granted.");


        return permissions.toArray(new String[permissions.size()]);




        Manifest xml permission



        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.READ_PHONE_STATE" />
        <uses-permission android:name="android.permission.READ_SMS" />
        <uses-permission android:name="android.permission.SEND_SMS" />





        share|improve this answer















        Best Code



         @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
        public class Firstclass extends AppCompatActivity




        private int timeoutMillis = 5000;

        /** The time when this @link AppCompatActivity was created. */

        private long startTimeMillis = 0;

        /** The code used when requesting permissions */

        private static final int PERMISSIONS_REQUEST = 1234;


        public int getTimeoutMillis()
        return timeoutMillis;



        @SuppressWarnings("rawtypes")
        public Class getNextActivityClass()
        return SecondActivity.class;
        ;


        public String getRequiredPermissions()
        String permissions = null;
        try
        permissions = getPackageManager().getPackageInfo(getPackageName(),
        PackageManager.GET_PERMISSIONS).requestedPermissions;
        catch (PackageManager.NameNotFoundException e)
        e.printStackTrace();

        if (permissions == null)
        return new String[0];
        else
        return permissions.clone();



        @TargetApi(23)
        @Override
        protected void onCreate(Bundle savedInstanceState)

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        startTimeMillis = System.currentTimeMillis();


        if (Build.VERSION.SDK_INT >= 23)
        checkPermissions();
        else
        startNextActivity();




        @TargetApi(23)
        @Override
        public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults)
        if (requestCode == PERMISSIONS_REQUEST)
        checkPermissions();



        private void startNextActivity()
        runOnUiThread(new Runnable()

        @Override
        public void run()


        );
        long delayMillis = getTimeoutMillis() - (System.currentTimeMillis() - startTimeMillis);
        if (delayMillis < 0)
        delayMillis = 0;

        new Handler().postDelayed(new Runnable()
        @Override
        public void run()
        startActivity(new Intent(Firstclass.this, getNextActivityClass()));
        finish();

        , delayMillis);



        private void checkPermissions()
        String ungrantedPermissions = requiredPermissionsStillNeeded();
        if (ungrantedPermissions.length == 0)
        startNextActivity();
        else
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        requestPermissions(ungrantedPermissions, PERMISSIONS_REQUEST);




        @TargetApi(23)
        private String requiredPermissionsStillNeeded()

        Set<String> permissions = new HashSet<String>();
        for (String permission : getRequiredPermissions())
        permissions.add(permission);

        for (Iterator<String> i = permissions.iterator(); i.hasNext();)
        String permission = i.next();
        if (checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED)
        Log.d(Firstclass.class.getSimpleName(),
        "Permission: " + permission + " already granted.");
        i.remove();
        else
        Log.d(Firstclass.class.getSimpleName(),
        "Permission: " + permission + " not yet granted.");


        return permissions.toArray(new String[permissions.size()]);




        Manifest xml permission



        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.READ_PHONE_STATE" />
        <uses-permission android:name="android.permission.READ_SMS" />
        <uses-permission android:name="android.permission.SEND_SMS" />






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 9:59

























        answered Nov 13 '18 at 9:01









        suresh madaparthisuresh madaparthi

        25419




        25419



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276818%2fsplashscreen-with-runtime-permissions%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3