Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splash Screen with 2 Images in Android

I have a Splash Screen set up in my Android app that runs the length of a network call and then switches to the main (maps) activity.

I am wondering if I can use a 2nd image in this Splash Screen as a way to list sponsors. The splash I have now lasts up to 5 or 6 seconds so I would rather not add a 2nd Splash activity.

What I am hoping is that I can just add a second image to the current activity. The first one for 2 seconds and the second for the remainder of the network call.

here is part my code. Please let me know if this is possible to add to the existing code. Thanks!

   

   

    // 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();
                    }
                    }
            });

        }
    }

}
like image 973
Patrick Olson Avatar asked Nov 26 '25 16:11

Patrick Olson


1 Answers

I would simply 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 onPostExecute block. It's already running on UI thread so you should remove it.

like image 198
inmyth Avatar answered Nov 29 '25 06:11

inmyth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!