You should put a Thread.sleep(long) before the "setContentView(R.layout.xxxx..)" in the onCreate(..) function. In that way, it will actually delay before showing you the elements of the Activity.
If you want to delay even before the onCreate(...) is fired, the approach will need to be different, here is one suggestion:
Run a Service and check for the Foreground application using ActivityManager class (see sample code below). Keep checking for when your app is fired or brought to the 'foreground' (using code below) and then just go back to homescreen & start a timer (in the service itself). Once the timer expires, start your app.
You can run the function below inside an AsyncTask in the Service.
The two approaches are quite different and really depends on what you are looking to achieve exactly.
@SuppressWarnings("deprecation")
private void getRunningAppName() throws NameNotFoundException {
Log.v("neiltag", "Entered getRunningAppName()");
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
// The first in the list of RunningTasks is always the foreground task.
RunningTaskInfo foregroundTaskInfo = am.getRunningTasks(1).get(0);
String foregroundTaskPackageName = foregroundTaskInfo .topActivity.getPackageName();
PackageManager pm = this.getPackageManager();
PackageInfo foregroundAppPackageInfo = pm.getPackageInfo(foregroundTaskPackageName, 0);
String foregroundTaskAppName = foregroundAppPackageInfo.applicationInfo.loadLabel(pm).toString();
String packageName = foregroundAppPackageInfo.packageName;
if(foregroundTaskAppName.matches("<NAME OF YOUR APP HERE>")) {
//If your app is fired go back to the Homescreen(i.e. the delay)
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "You are not allowed to open Facbeook now, sorry!", Toast.LENGTH_SHORT).show();
}
});
}
//ADD A TIMER HERE
//ONCE TIMER EXPIRES, FIRE UP YOUR APP AGAIN
}