0
public class BootUpReceiver extends BroadcastReceiver{
@Override
public void onReceive(final Context context, Intent intent) {

     //Delay 10 sec so that device could establish network
     Intent i = new Intent(context, SplashActivity.class);  
     i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     context.startActivity(i);                

         }

}

I m starting an activity on android startup but android takes some time to establish network connection therefore I want to delay 10 sec the app launch so that my app can use internet.

Ricky Watson
  • 133
  • 1
  • 4
  • 15

3 Answers3

0

Make it with a runnable

public class BootUpReceiver extends BroadcastReceiver{

@Override
public void onReceive(final Context context, Intent intent) {
    Handler handler = new Handler();
    int delay = 100;
    handler.postDelayed(startApp, delay);


    Runnable startApp = new Runnable() {

        @Override
        public void run() {

            Intent i = new Intent(context, SplashActivity.class);  
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i); 

        }
    };
  }

}
A.S.
  • 4,574
  • 3
  • 26
  • 43
0

Best practice for doing this kind of background processing stuff is to create a splash screen and load it using various animation and =attractive stuff at the start up of heavy activity for a particular amount of time. So that user can wait until background process done.

Your approach seems like lots of coding required.

Hope this would help.

Prashant Patel
  • 238
  • 2
  • 12
0

Instead of making some random delay (which is not dependable), you should use another BroadcastReceiver that can detect network state change. For example: NetworkStateReceiver. This will detect when network state is changed. See here: Check INTENT internet connection

So your application should have 2 BroadcastReceivers: BootupReceiver and NetworkStateReceiver.

In BootUpReceiver's onReceive(), set some flag in preferences as true. Then in NetworkStateReceiver's onReceive(), check the flag, if the flag is true, open your Activity and set the flag as false.

(To set values in SharedPreferences in a BroadcastReceiver, see: Shared preferences inside broadcastreceiver)

Although this will work, but this is not a good practice to automatically starting an Activity. User may not like it.

Community
  • 1
  • 1
ahsanul_k
  • 161
  • 8