Custom splash screen for loading variables in Android

I'm starting to learn this: Android SplashScreen and my application works great with this method. Unfortunately, I developed a special screen saver (not a progress dialog, as in another post), and I cannot use the same approach. My splashscreen is another action that I call from onCreate as another thread.

Instead of this.pd = ProgressDialog.show(this, "Working..", "Downloading Data...", true, false); in onCreate I do:

  Thread splashThread = new Thread() { @Override public void run() { // start the splash screen activity } }; splashThread.start(); } 

The splashscreeen action starts correctly. Therefore, I call AsyncTask as in the default method (doInBackground, onPostExecute), and I cannot finish the action that launches the splashscreen and return to the main one after all the variables are loaded into doInBackground.

Any suggestion?

+2
source share
2 answers

Well, I finally thank JPM for offering to use handlers, and I decided with this:

 public class SplashScreen extends Activity { private static Handler mHandler ; private static Handler mainHandler ; protected static final int CLOSE_SPLASH = 0 ; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash_screen); mHandler = new Handler(){ @Override public void handleMessage(Message msg){ switch(msg.what){ case CLOSE_SPLASH: finish(); break; } } }; } @Override public void onStart(){ super.onStart(); if(mainHandler != null){ mainHandler.sendEmptyMessage(MainActivity.START_LOAD); } } @Override public boolean onKeyDown (int keyCode, KeyEvent event){ if(keyCode == KeyEvent.KEYCODE_BACK){ mainHandler.sendEmptyMessage(MainActivity.ABORT_LOAD); } return super.onKeyDown(keyCode, event) ; } public static void setMainHandler(Handler h){ mainHandler = h ; } public static void sendMessage(Message msg){ mHandler.sendMessage(msg); } public static void sendMessage(int w){ mHandler.sendEmptyMessage(w); } } 

In MainActivity, I control the back and forth handlers:

 public class MainActivity extends Activity { private MainActivity _this; private Handler mHandler; protected static final int FINISH_LOAD = 0 ; protected static final int START_LOAD = 1 ; protected static final int ABORT_LOAD = 2 ; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); _this = this; mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case FINISH_LOAD: SplashScreen.sendMessage(SplashScreen.CLOSE_SPLASH); break; case START_LOAD: initializing(); break; case ABORT_LOAD: finish(); } } }; startSplash(); } private void startSplash() { Intent intent = new Intent(this, SplashScreen.class); SplashScreen.setMainHandler(mHandler); startActivity(intent); } private void initializing() { new Thread() { @Override public void run() { long start_time = android.os.SystemClock.uptimeMillis(); doTheHeavyJob(); long duration = android.os.SystemClock.uptimeMillis() - start_time; if (duration <=3000) { try { wait(3000-duration); } catch (InterruptedException e) { e.printStackTrace(); } } mHandler.sendEmptyMessage(FINISH_LOAD); } }.start(); } } 

In this way, I can control the doTheHeavyJob () function and end SplashScreen in both cases: after finishing work and at least after 3000 milliseconds, the minimum duration of my pop-up screen will be shown. I also want to tank Teskio on anddev italian for most of the work done here.

+2
source

I don’t know if that would be the case, since Android has some good Handler methods that can do some of what you are looking for, then you can block the thread until your items load. This is how I make a standard Splash screen. This Splash activity is the main activity in the manifest, and when its action causes the following actions.

 import java.io.File; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.content.Intent; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.util.Log; import android.widget.Toast; /** * Splash screen to start the app and maybe check if some things enabled * If not enabled popup and if user enables then continue else close app. */ public class Splash extends Activity { private static final String TAG = "Splash"; @Override public void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.splash); // load some stuff (new Handler()).postDelayed(new splashHandler(), 2000); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d(TAG, "OnActivity result code = " + resultCode); super.onActivityResult(requestCode, resultCode, data); if (resultCode == -1) { // class of the next activity you want to display after the Splash screen Class<?> className = MainApp.class; startActivity(new Intent(getApplication(), className )); Splash.this.finish(); } else { Toast.makeText(Splash.this, getResources().getText(R.string.stringSplashonActivity), Toast.LENGTH_LONG).show(); finish(); // the program... return; } } /** * Runnable class to check if user is logged in already * and then start the correct activity. * */ class splashHandler implements Runnable{ @Override public void run() { .....Do something..... } } } 

Then, don't forget to determine the activity between the application tags in Android Manifest.xml (for those who are new to Android)

  <activity android:name="Splash" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 

Hope this helps ...

+1
source

Source: https://habr.com/ru/post/901792/


All Articles