Question

I have an Activity that I want to run a Delay Function when it runs. I [i]don't[/i] want a delay in OnClick function, I want delay when activity [i]starts[/i].

I have tried the following solutions :

  1. How to set delay in Android onClick function
  2. How to pause/delay on Android?
  3. Android: Timer/Delay Alternative
  4. Android timer set delay

...and some more and none of them did what I want. I want a delay when activity starts (for my project , when The Game Starts) but with that codes, it starts the delay (e.g 10s) when I put my finger on the screen.

Was it helpful?

Solution

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 

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top