How to make a well-coded screensaver

We all know that there are many tutorials on how to make a splash screen on Android. But we also know that these are pseudo-splashscreens. I searched for many and I always saw Thread.sleep(x) . It is not very well coded, it is just to make the application beautiful and look like a professional application, this is not what I want!
Another problem with these screensavers is that they do not solve my problem, because they only show after the activity is started and the content is displayed.

I have an application that does a lot of things during initialization, and when the application starts, the user sees a black screen for several seconds, enough time to annoy. Therefore, I want to show a well-coded splash screen that removes the black screen that appears before the content is presented.

I tried something. I included the screensaver (a RelativeLayout ) in the layout that is set to MainActivity , but as far as I know, Android only shows content after everything has been loaded, so if I try to show some kind of view from the content view, I need to wait until it will not end. However, I will send my code, it can somehow help ...

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new SplashTask().execute(); } private class SplashTask extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { initializeViews(); mSplashScreen.setVisibility(View.VISIBLE); } @Override protected Void doInBackground(Void... params) { return null; } @Override protected void onPostExecute(Void params) { Standard.Initiate(MainActivity.this); verifyListStats(); loadListAdapters(); setOnClickListeners(); mSplashScreen.setVisibility(View.GONE); } } 

I tried loading some resources in doInBackground(...) , but since I am doing some operations in onResume() that need those resources that I cannot do (or at least I think I cannot) .

Any idea? I heard about a built-in mechanism similar to iOS launch images, maybe this might be the way.

+7
java android android-asynctask splash-screen
source share
8 answers

When the action starts, Android launches Zygote, an empty activity that does nothing, and sets its own activity theme on it, and then launches it. After your activity is ready to be shown, it will change the displayed activity to yours. For more information about Zygote, you can read this article by Cyril Motier

To answer your question, you can do this:

  • Create a small theme in which the custom window background displays splash information (you can use the 9 patch to center the unscaled content);
  • In your manifest, use this surge theme for your activity;
  • in your activity, the onCreate () method, call setTheme (R.id.yourActivityTheme) (call it before setContentView ());
  • enjoy ...

Thus, your “splash screen” (that is: zygote with your splash theme) will be displayed until your activity is ready for display.

+5
source share

Editing: here is a great tutorial for a screensaver where you want the screensaver to appear for a set period of time or for any kind of processing. This gives the pros and cons of several approaches.

http://blogactivity.wordpress.com/2012/02/24/writing-splash-screens-the-right-way/

+3
source share

Create a SplashScreen action and declare it by default in your application, i.e. add it to your AndroidManifest.xml

  <activity android:name="package.SplashScreen" android:icon="@drawable/icon" android:label="@string/app_name" android:noHistory="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> 

In your SplashActivity onCreate, launch the new AsyncTask:

 public class SplashScreen extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash_image); new InitAsyncTask().execute(); } private class InitAsyncTask extends AsyncTask<?, ?, ?> { protected ? doInBackground(?... params) { // PERFORM YOUR INITIALIZATION HERE } protected void onPostExecute(? result) { // Initialization is completed, close SplashScreen // and launch your MainActivity: SplashScreen.this.finish(); startActivity(new Intent(MainActivity.class, SplashScreen.this); } } } 
+2
source share

I did this by creating a popup screen dialog, then close it when I finished loading

 splashDialog = new Dialog(getActivity(), android.R.style.Theme_Black_NoTitleBar_Fullscreen); splashDialog.setContentView(R.layout.yourSplashLayout); 
0
source share

The best way to open a screensaver is to create a new action for it, and then in this event you can set a timer to open a new activity, which will be your main activity. I think using this, you can get rid of your black screen at startup. The manifest manifests burst activity as the default startup activity. Here is an example: -

SplashActivity.java

 public class SplashActivity extends Activity { private Timer timer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_splash_activity); } @Override protected void onResume() { timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { Intent intialScreenIntent = new Intent(SplashActivity.this, MainActivity.class); startActivity(intialScreenIntent); } }, 2000l); super.onResume(); } @Override public void onBackPressed() { try { timer.cancel(); timer = null; } catch (Exception e) { } super.onBackPressed(); }} 
0
source share

I was looking for the same thing too. In the end, I will implement this to fully satisfy my requirements.

Note:

  • Do all the hard work of creating your second action. Therefore, when all work is completed, the activity will automatically begin after the call onStart() .
  • You can put some checks in onCreate() if you want some code to run only once in the life of the application.
  • Do not do the hard work in the ApplicationClass onCreate () method, because it runs when your application starts. Therefore, during this execution there is a black screen.

This is the xml splash screen:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/splash_screen" tools:context=".SplashScreenActivity"> </RelativeLayout> 

This is an activity class.

Public class SplashScreenActivity extends action {

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash_screen); new Handler().postDelayed(new Runnable() { @Override public void run() { Intent mainIntent = new Intent(SplashScreenActivity.this, MyLauncherActivity.class); SplashScreenActivity.this.startActivity(mainIntent); // SplashScreenActivity.this.finish(); No need to finish it as "noHistory" tag is true in the manifest } }, 1500); } @Override public void onBackPressed() { } } 

Manifest Entries:

  <activity android:name="com.wokomoco.test.activities.SplashScreenActivity" android:label="@string/app_name" android:noHistory="true" android:screenOrientation="portrait" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 

Hope this helps you. Enjoy the coding!

0
source share

One of my unlicensed projects required a pop-up screen, although this was not necessary. Perhaps this may be useful for your project, as it is based on dialogue, not on activity. If the splash screen is displayed or gestured, it will be fired, as well as after the animation is completed (timeout). Modifications can be made to the class, so you check some kind of “ready state logical” before allowing the tap or gestures to reject the image.

class file: AppIntro.java

 import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.Window; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.view.animation.AnimationUtils; public class AppIntro extends Dialog { protected int mLayoutRes = 0; protected int mAnimRes = 0; protected Animation mIntroAnim = null; protected View mLayout = null; public AppIntro(Context aContext, int aLayoutRes, int aAnimRes) { super(aContext); mLayoutRes = aLayoutRes; mAnimRes = aAnimRes; } @Override protected void onCreate(Bundle aSavedState) { super.onCreate(aSavedState); mLayout = LayoutInflater.from(getContext()).inflate(mLayoutRes,null); mLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AppIntro.this.dismiss(); } }); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(mLayout); mIntroAnim = AnimationUtils.loadAnimation(getContext(),mAnimRes); mIntroAnim.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { //nothing to do } @Override public void onAnimationRepeat(Animation animation) { //nothing to do } @Override public void onAnimationEnd(Animation animation) { AppIntro.this.dismiss(); } }); } @Override public boolean onGenericMotionEvent(MotionEvent event) { dismiss(); return true; } @Override public void show() { super.show(); mLayout.startAnimation(mIntroAnim); } } 

Then we determine that our animation disappears (change the duration to what you need to load your application) in the file "res / anim / intro_anim.xml". 4200 = 4.2 seconds.

 <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:interpolator="@android:anim/accelerate_interpolator" android:duration="4200" android:repeatCount="0" > </alpha> </set> 

Finally, we define our splash screen layout (using any image (s) you want) in "layout / intro.xml". In my specific screen saver, the name of the application was displayed with an image along with 3 logos from different funding sources.

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout_intro" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/intro_Text_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:gravity="center" android:text="@string/title_intro" android:textAppearance="?android:attr/textAppearanceLarge" /> <ImageView android:id="@+id/intro_Image_myproject" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/intro_Text_title" android:layout_centerHorizontal="true" android:src="@drawable/intro_image" /> <FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/intro_Image_myproject" android:layout_alignRight="@id/intro_Image_myproject" android:layout_alignLeft="@id/intro_Image_myproject"> <ImageView android:id="@+id/intro_Image_logo1" android:layout_width="80dp" android:layout_height="50dp" android:scaleType="fitXY" android:src="@drawable/logo1" android:layout_gravity="left|center_vertical"/> <ImageView android:id="@+id/intro_Image_logo2" android:layout_width="150dp" android:layout_height="wrap_content" android:src="@drawable/logo2" android:layout_gravity="center" android:scaleType="centerInside"/> <ImageView android:id="@+id/intro_Image_logo3" android:layout_width="70dp" android:layout_height="70dp" android:scaleType="fitXY" android:src="@drawable/logo3" android:layout_gravity="right|center_vertical"/> </FrameLayout> </RelativeLayout> 

The code used to pop up the dialog box:

 @Override protected void onCreate(Bundle aSavedState) { super.onCreate(aSavedState); if (aSavedState==null) { //only show splash screen at app start, not on rotate screen new AppIntro(this,R.layout.intro,R.anim.intro_anim).show(); } setContentView(R.layout.main); //...rest of onCreate() } 

My application displayed my main view at the same time as the splash screen, so ensure that this dialog does not appear as soon as you call .show ().

0
source share

Below is the Splash Screen function that I use in all projects. Please check if you are helpful.

 public class SplashActivity extends Activity { private SharedPreferences myPrefs; private SharedPreferences.Editor prefsEditor; private boolean login; private boolean connectivityState; private String connectedNetworkType; private ConnectivityManager connectivityManager; private CheckInternet checkInternet; private File file = new File(API.file_dir); public static Location loc; Configuration newConfig; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.splash_layout); //---------------------- myPrefs = getSharedPreferences("myPrefs", MODE_WORLD_READABLE); boolean login = myPrefs.getBoolean("login", false); checkForInternetAndNextFlow(); } // Check for the Internet Connection private void checkForInternetAndNextFlow() { connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); checkInternet = new CheckInternet(getApplicationContext()); connectivityState = checkInternet.isConnected(connectivityManager); if (connectivityState) { connectedNetworkType = checkInternet.getNetworkType(); // Toast.makeText(getApplicationContext(), "Connected via : " + // connectedNetworkType, Toast.LENGTH_LONG).show(); // check for the login or not from preference myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE); login = myPrefs.getBoolean("login", false); System.out.println("Loging value is:" + login); Thread t = new Thread() { public void run() { try { Thread.sleep(1500); } catch (InterruptedException e) { e.printStackTrace(); } finally { if (login) { Intent i = new Intent(SplashActivity.this,MainTabActivity.class); startActivity(i); } else { Intent i = new Intent(SplashActivity.this,LoginActivity.class); startActivity(i); } } } }; t.start(); } else { // Toast.makeText(getApplicationContext(), "NOT connected", // Toast.LENGTH_LONG).show(); final Dialog dialog = new Dialog(SplashActivity.this,R.style.CustomDialogTheme); dialog.getWindow(); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.internet_dialog); dialog.setCancelable(false); Button retryBtn = (Button) dialog.findViewById(R.id.retryBtn); Button cancel = (Button) dialog.findViewById(R.id.cancelBtn); retryBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { dialog.dismiss(); checkForInternetAndNextFlow(); } }); cancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { dialog.dismiss(); finish(); } }); dialog.show(); } } } 

Hope this helps you. Enjoy the coding ... :)

0
source share

All Articles