How to show a screensaver using a timer in Android?

Could you tell me how to show the screensaver using a timer in android.I can show using the stream, But the stream is a good choice, can you please tell me the best way to handle this? Stream usage like this

package com.example.splash_test;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

public class MainActivity extends Activity {
    protected int _splashTime = 5000; 

    private Thread splashTread;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         splashTread = new Thread() {
             @Override
             public void run() {
                 try {                       
                     synchronized(this){
                             wait(_splashTime);
                     }

                 } catch(InterruptedException e) {} 
                 finally {
                     finish();

                     Intent i = new Intent();
                     i.setClass(MainActivity.this, SecondActivity.class);
                             startActivity(i);

                     //stop();
                 }
             }
         };

         splashTread.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
+5
source share
5 answers

This snippet is very useful for briefly creating a timer. Try it...

new Timer().schedule(new TimerTask() {          
    @Override
    public void run() {
        startActivity(new Intent(getApplicationContext(), MainActivity.class));
    }
}, 5000);

Here I start MainActivityin 5 seconds. Just enter this code in onCreate()your surge activity.

+2
source
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;

public class SplashScreen extends Activity {

protected int _splashTime = 2000;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.splash);

    int secondsDelayed = 1;
    new Handler().postDelayed(new Runnable() {
        public void run() {
            startActivity(new Intent(SplashScreen.this,
                    SecondActivity.class));
            finish();
        }
    }, secondsDelayed * 1000);
}

}
+1
source

onCreate:

SystemClock.sleep(TimeUnit.SECONDS.toMillis(3));

p.s. SplashScreen .

0
out_marginLeft="50dp"
            android:layout_marginTop="50dp"
            android:layout_marginRight="50dp"
            android:gravity="center"
            android:orientation="vertical">

            <ProgressBar
                android:id="@+id/progressBar"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_above="@+id/txtProgress"
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:progress="50"
                android:progressTint="@android:color/black" />

            <TextView
                android:id="@+id/txtProgress"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:text="Progress"
                android:textColor="@android:color/black" />
        </LinearLayout>  

Java
0
new Handler().postDelayed(new Runnable() {

/*
 * Showing splash screen with a timer. This will be useful when you
 * want to show case your app logo / company
 */

    @Override
    public void run() {
        // This method will be executed once the timer is over
        // Start your app main activity

            Intent i = new Intent(SplashScreen.this, YourActivity.class);
            startActivity(i);

        // close this activity
        finish();
    }
}, 1000);
0

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


All Articles