Android Splashscreen when starting AsyncTask

I am currently working on an Android-phonegap project.

Before the application loads index.html, there is native code that does the work. This is detailed in AsyncTask, which knocks on an SD card (if any).

I managed to show a progress bar when A.Task is running, but I would like to add a splash screen in the background. I mainly worked with Phonegap and just started with my own code. Therefore, I am confused by all these layouts, themes, and what else can be defined in XML files. I'm sure this is also a good idea for large user interface designs, but for the simple Splash screen that I want right now, it feels like a complete kink.

This is a fragment from the source. Just straight. onCreate () calls AsyncTask, which does some work and launches PhoneGap in it using the PostExecute method. I want the screen to display either in the onCreate method or onPreExecute. After the task is completed, I will turn off the screen in onPostExecute (). I also added comments to illustrate my idea.

public class myAct extends DroidGap { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Show Splash here or in onPreExecute()! new myAsTa().execute(); } } class myAsTa extends AsyncTask<Void, Integer, Boolean> { ProgressDialog dialog = new ProgressDialog(myAct.this); @Override protected void onPreExecute() { //or show splash here! dialog.setMessage("Cool Progressbar!"); dialog.show(); super.onPreExecute(); } protected Boolean doInBackground(Void... values) { //magician at work! return true; } @Override protected void onProgressUpdate(Integer... values) { //insult user here } protected void onPostExecute(Boolean result) { dialog.dismiss(); //dismiss splashscreen //start Phonegap } } 

Thanks for reading and your help :)

+2
source share
2 answers

I made such a splash screen here . This loads the splash layout and runs directly from the system when the application loads ( AndroidManifest.xml )

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

What is shown on the screen is defined in setContentView(R.layout.splash); - this layout is basically

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/desktop_rhq" > <TextView android:layout_gravity="bottom" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginBottom="10dp" android:text="@string/loading" /> </LinearLayout> 

Specifies the background image and text to be displayed. Honestly, I didn’t think much about changing the orientation during the burst launch - I think this will restart the background task.

+5
source

splash.xml

 <?xml version="1.0" encoding="utf-8"?> <ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/splash_image"> </ImageView> 

SplashActivity.class

 public class SplashActivity extends Activity { protected boolean _active = true; protected int _splashTime = 1000; // time to display the splash screen in ms /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash); // thread for displaying the SplashScreen Thread splashTread = new Thread() { @Override public void run() { try { int waited = 0; while(_active && (waited < _splashTime)) { sleep(100); if(_active) { waited += 100; } } } catch(InterruptedException e) { } finally { startActivity(new Intent(SplashActivity.this, NextActivity.class)); finish(); //stop(); } } }; splashTread.start(); } } 

To change the orientation: I Set in AndroidManifest.xml screenOrientation="portrait"

  <activity android:label="@string/app_name" android:screenOrientation="portrait" android:theme="@android:style/Theme.NoTitleBar" android:name=".SplashActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 
+1
source

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


All Articles