After Android Marshmallow, other Productive use of Splash screen I come to think of is requesting necessary Android Permissions
in your app's splash screen.
it seems like most apps handle permission request this way.
Dialogs make bad UIX and they break the main flow and make you decide on runtime and truth is most users might not even care if your app want to write something on SD card. Some of them might not even understand what we are trying to convey until we translate it in plain english.
Requesting permissions at one time make less number of "if else" before every operation and make your code looks clutter free.
This is a example of how you can ask for permissions in your splash activity for device running Android OS 23+ .
If all permissions are granted OR already granted OR app is running on Pre Marshmallow THEN just go and display main contents with little delay of half second so that user can appreciate effort we had put in reading this question and trying to give our best.
import android.Manifest;
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.c2h5oh.beer.R;
import com.c2h5oh.beer.utils.Animatrix;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SplashActivity extends AppCompatActivity {
final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
//show animations
Animatrix.scale(findViewById(R.id.title_play), 100);
Animatrix.scale(findViewById(R.id.title_edit), 100);
Animatrix.scale(findViewById(R.id.title_record), 100);
Animatrix.scale(findViewById(R.id.title_share), 100);
if (Build.VERSION.SDK_INT >= 23) {
// Marshmallow+ Permission APIs
fuckMarshMallow();
} else {
// Pre-Marshmallow
///Display main contents
displaySplashScreen();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS: {
Map perms = new HashMap();
// Initial
perms.put(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.RECORD_AUDIO, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.MODIFY_AUDIO_SETTINGS, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.VIBRATE, PackageManager.PERMISSION_GRANTED);
// 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.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.MODIFY_AUDIO_SETTINGS) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.VIBRATE) == PackageManager.PERMISSION_GRANTED) {
// All Permissions Granted
// Permission Denied
Toast.makeText(SplashActivity.this, "All Permission GRANTED !! Thank You :)", Toast.LENGTH_SHORT)
.show();
displaySplashScreen();
} else {
// Permission Denied
Toast.makeText(SplashActivity.this, "One or More Permissions are DENIED Exiting App :(", Toast.LENGTH_SHORT)
.show();
finish();
}
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
@TargetApi(Build.VERSION_CODES.M)
private void fuckMarshMallow() {
List permissionsNeeded = new ArrayList();
final List permissionsList = new ArrayList();
if (!addPermission(permissionsList, Manifest.permission.READ_EXTERNAL_STORAGE))
permissionsNeeded.add("Read SD Card");
if (!addPermission(permissionsList, Manifest.permission.RECORD_AUDIO))
permissionsNeeded.add("Record Audio");
if (!addPermission(permissionsList, Manifest.permission.MODIFY_AUDIO_SETTINGS))
permissionsNeeded.add("Equilizer");
if (!addPermission(permissionsList, Manifest.permission.VIBRATE))
permissionsNeeded.add("Vibrate");
if (permissionsList.size() > 0) {
if (permissionsNeeded.size() > 0) {
// Need Rationale
String message = "App need 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) {
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
}
});
return;
}
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
return;
}
Toast.makeText(SplashActivity.this, "No new Permission Required- Launching App .You are Awesome!!", Toast.LENGTH_SHORT)
.show();
displaySplashScreen();
}
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(SplashActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
@TargetApi(Build.VERSION_CODES.M)
private boolean addPermission(List permissionsList, String permission) {
if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
permissionsList.add(permission);
// Check for Rationale Option
if (!shouldShowRequestPermissionRationale(permission))
return false;
}
return true;
}
/**
* Display main content with little delay just so that user can see
* efforts I put to make this page
*/
private void displaySplashScreen() {
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
@Override
public void run() {
startActivity(new Intent(SplashActivity.this, AudioPlayerActivity.class));
finish();
}
}, 500);
}
}