Splash screen with 2 images on Android

I have a splash screen set up in my android app that takes the length of the network call and then switches to the main (map) activity.

I am wondering if I can use the 2nd image on this popup screen to list sponsors. The splash I have lasts up to 5 or 6 seconds, so I would rather not add a second Splash activity.

I hope I can just add a second image to the current activity. The first is for 2 seconds, the second is for the rest of the network call.

here is part of my code. Please let me know if it can be added to the existing code. Thank!

   

   

    // Splash screen timer
    private static int SPLASH_TIME_OUT = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        wmbPreference = PreferenceManager.getDefaultSharedPreferences(this);
        ConnectivityManager conectivtyManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        if (conectivtyManager.getActiveNetworkInfo() != null
                && conectivtyManager.getActiveNetworkInfo().isAvailable()
                && conectivtyManager.getActiveNetworkInfo().isConnected()) {
            isConnected = true;
        } else {
            isConnected= false;
        }

        if(isConnected)
            new LoadAllVendors().execute();
        else
        {
            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent intent1 = new Intent(SplashScreen.this, ShoppingListActivity.class);
                    finish();
                    startActivity(intent1);
                }
            }, 5000);
        }

    }

    class LoadAllVendors extends AsyncTask<String, String, String> {

        /**
         * getting All vendors from url
         */
        protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url_all_vendors, "GET", params);
            JSONObject json1 = jParser.makeHttpRequest(url_all_products, "GET", params);
            JSONObject json2 = jParser.makeHttpRequest(url_all_vps, "GET", params);

            dataSource = new VendorsDataSource(getApplicationContext());
            dataSource1 = new ProductsDataSource(getApplicationContext());
            dataSource2 = new VandPDataSource(getApplicationContext());

            dataSource.open();
            dataSource1.open();
            dataSource2.open();



            // Check your log cat for JSON reponse
            //Log.d("All Products: ", json.toString());

            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);
                int success1 = json1.getInt(TAG_SUCCESS);
                int success2 = json2.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // products found
                    // Getting Array of Products
                    vendors = json.getJSONArray(TAG_VENDORLOCNS);
                    vendorIDs = new ArrayList<>();

                    // looping through All Products
                    List<Vendor> allVendors = new ArrayList<>();
                    for (int i = 0; i < vendors.length(); i++) {
                        JSONObject c = vendors.getJSONObject(i);

                        Vendor vendor = new Vendor(c.getInt(TAG_VENDORID), c.getString(TAG_NAME), c.getDouble(TAG_VENDORLOCNLONG), c.getDouble(TAG_VENDORLOCNLAT), c.getInt(TAG_VENDORHEARTS), c.getInt(TAG_VENDORTODOS)
                                , c.getString(TAG_VENDOROWNER), c.getString(TAG_VENDOREMAIL), c.getString(TAG_VENDORPHONE), c.getString(TAG_ABOUT), c.getString(TAG_WEBSITE), c.getString(TAG_VENDORIMAGE));
                        allVendors.add(vendor);
                        MarkedVendor v = new MarkedVendor(c.getInt(TAG_VENDORID), c.getString(TAG_NAME), false, false);
                        vendorIDs.add(vendor.getId());
                        dataSource.createVendor(vendor);
                        dataSource.createSavedVendor(v);
                    }
                    dataSource.deleteVendorTable(vendorIDs);
                    dataSource.close();
                }

                if (success1 == 1) {
                    // products found
                    // Getting Array of Products
                    products = json1.getJSONArray(TAG_PRODUCTS);

                    // looping through All Products
                    for (int i = 0; i < products.length(); i++) {
                        JSONObject c = products.getJSONObject(i);

                        Product product = new Product(c.getInt(TAG_PRODUCTID), c.getString(TAG_PRODUCTNAME), c.getString(TAG_DISTINGUISHER));
                        dataSource1.createProduct(product);
                    }

                    dataSource1.close();
                }

                if (success2 == 1) {
                    // products found
                    // Getting Array of Products
                    vandps = json2.getJSONArray(TAG_VPS);
                    vpIDs = new ArrayList<>();
                    // looping through All Products
                    List<VandP> allVandPs = new ArrayList<>();
                    for (int i = 0; i < vandps.length(); i++) {
                        JSONObject c = vandps.getJSONObject(i);

                        VandP vandp = new VandP(c.getInt(TAG_VPID), c.getInt(TAG_VP_VENDORID), c.getInt(TAG_VP_PRODUCTID));
                        dataSource2.createVandP(vandp);
                        allVandPs.add(vandp);
                        vpIDs.add(vandp.getVpID());
                    }
                    dataSource2.deleteVandPTable(vpIDs);
                    dataSource2.close();

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                        Intent intent = new Intent(SplashScreen.this, MapsActivity.class);
                        Intent helpIntent = new Intent(SplashScreen.this, HelpActivity.class);

                    boolean isFirstRun = wmbPreference.getBoolean("FIRSTRUNMapsActivity", true);
                    if(!isFirstRun) {
                        startActivity(intent);
                        finish();
                    }
                    else {
                        startActivity(helpIntent);
                        finish();
                    }
                    }
            });

        }
    }

}
      

Run codeHide result


+3


source to share


1 answer


I would just add another handler here

Handler mHandler;
...

if(isConnected){
     new LoadAllVendors().execute();
     mHandler = new Handler();
     mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
            //set the second image
            }
        }, 2000);
}

protected void onDestroy(){
   ...
   // make sure to clean up handlers when activity is destroyed
   if (mHandler != null)
     mHandler.removeCallbacksAndMessages(null);       
}

      



Also you don't need to do runOnUiThread

in block onPostExecute

. It is already running on the UI thread, so you must remove it.

+1


source







All Articles