I have a SplashScreen activity at my android app that lasts for 3 seconds, and then it switches to MainActivity.
MainActivity plays a sound, and it has a button. My problem is that the sound plays when the SplashScreen activity is visible.
onResume at MainActivity calls for run(), run() sleeps for 2 seconds and plays the sound.
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
m.start();
try {
Thread.sleep((int) randomValue * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
m.seekTo(0);
m.start();
cpu.setText(EranThatWillShortYourDouble(randomValue));
btn.setClickable(true);
}
#Override
protected void onResume() {
super.onResume();
run();
}
Instead of SplashScreen displaying for 3 seconds and switches to MainAcivity, It lasts 5 seconds and plays the sound when SplashScreen is visible.
What do I do?
EDIT: This is my SplashScreen activity:
public class SplashScreen extends Activity {
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
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, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
}
Please take a look at AsyncTasks, Android Runnables and the ability to use a Handler to postDelayed.
Don't do thread.sleep in the main thread, it's really bad. :)
Do not use splash screens in Android. They are against the design guidelines and provide a poor UX.
http://developer.android.com/design/patterns/help.html#your-app
Related
My app shows a splash screen for 4 seconds and then opens another activity with a web view.
I am using a thread to splash the screen and finish() the first activity as soon as web view activity is started. I have also used animations like fade in and fade out.
It's CPU usage is fluctuating between 8% to 23%.What could be the reason. I want to reduce the CPU usage.
My first activity that shows splash screen and starts web view activity-
public class MainActivity extends Activity {
Thread splashThread;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.animation.fadein,R.animation.fadeout);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
//-----------------------------------------
splashThread=new Thread(){
public void run()
{
try
{
sleep(1000);
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
Intent intent = new Intent(getApplicationContext(),OpenWeb.class);
startActivity(intent);
}
}
};
splashThread.start();
//-----------------------------------------
super.onStart();
overridePendingTransition(R.animation.fadein, R.animation.fadeout);
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
finish();
super.onRestart();
}
}
Could be that you used alot of imports something like. I heard that uses alot of cpu?
import android.graphics.*
Are you using a AVD?
I want to add and clear window flag inside a thread but not working. Basically i want my thread to keep the screen on for two seconds and then clear the screen on flag.
Here's my code:
public class WriteCommThread extends Thread {
private long time=2000;
public WriteCommThread(float count) {
time = (long) count;
}
public void run() {
while(connectionUnAbort==true){
// Lock screen
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
slleep();
//Unlock screen
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
connectionUnAbort=false;
}
}
public void slleep(){
try {
Thread.sleep(time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I solved it after doing research, i am posting the referred link.
Answer Link
Here's my solution to my problem and it worked perfectly. I added these runnable classes in my background thread (run method). And after sleeping i could clear the flag to keep the screen on.
runOnUiThread(new Runnable() {
public void run() {
//stuff that updates ui
MainActivity.this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
});
runOnUiThread(new Runnable() {
public void run() {
//stuff that updates ui
MainActivity.this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
});
At the app I'm developing, I'm interested in an intro of a loading screen. Which automatically move to the next screen after a duration.
The intro itself, is working just fine. As well as the thread who delaying the system.
My problem is to make them work together.
The code:
public class MainActivity extends Activity implements OnClickListener {
ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loading_screen);
final Thread t1=new Thread(new Runnable() {
public void run() {
iv=(ImageView)findViewById(R.id.imgBtn1);
iv.setBackgroundResource(R.anim.loading_i_animation);
AnimationDrawable anim=(AnimationDrawable) iv.getBackground();
anim.start();
}
});
t1.start();
try {
Thread.sleep(2000);
finish();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
Intent st=new Intent(MainActivity.this,Welcome.class);
startActivity(st);
}
}
The result of this code is opening a white screen for the thread sleep-time duration. and after that open the "Welcome.class" screen via the intent.
It's just skipping the loading_screen, as is wasn't even exist.
I hope you guys could please help my with that.
You put your sleep on the UI thread which prevents Android to show anything until it finishes. Try the following code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loading_screen);
iv=(ImageView)findViewById(R.id.imgBtn1);
iv.setBackgroundResource(R.anim.loading_i_animation);
AnimationDrawable anim=(AnimationDrawable) iv.getBackground();
anim.start();
new Handler().postDelayed(
new Runnable() {
public void run() {
Intent st=new Intent(MainActivity.this,Welcome.class);
startActivity(st);
finish();
}
}, 2000);
}
This way the delay will run on a separated thread, but after 2 seconds it changes back to the main thread and runs the code you specified in your Runnable
This question already has answers here:
How do I make a splash screen? [closed]
(31 answers)
Closed 8 years ago.
So, I just read this question: How do I make a splash screen?
But instead of adding a fixed delay (like in the top answer), I wanted to keep the splash screen on while the MainActivity (with a MapFragment) loads.
public class SplashScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Thread t = new Thread(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
synchronized (this) {
try {
wait(3000);
System.out.println("Thread waited for 3 seconds");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
try {
t.start();
t.join();
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
I added the wait(3000) line, because I noticed before that the thread didn't live for long. However, if I make it wait for longer, there's just a black screen that lasts longer. For some reason, the SplashScreen activity won't show the ImageView.
What should I do?
Thanks.
Easy way to do it..
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.example.tabs.R;
public class Splash extends Activity implements Runnable
{
Thread mThread;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
mThread = new Thread(this);
mThread.start();
}
#Override
public void run()
{
try
{
Thread.sleep(2000);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
startActivity(new Intent(Splash.this, MainActivity.class));
finish();
}
}
}
splash.xml if you want to show a image
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/splash" >
</LinearLayout>
Note if you want to do some UI operation in splash .Then you have to create a handler and update UI in it.
The main thread cannot be blocked for a long time. You should use Handler to trigger another event if you want to start another event in 3 seconds. You can use sendMessageDelayed. In addition, startActivity should be called in main thread.
make a splash screen like this:
while(counter < 1300) {
try {
Thread.sleep(1000);
}catch(InterruptedException e) {
e.printStackTrace();
}
counter+=100;
}
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
Hope it helps!
EDIT:
Anotehr way to make a splash would be like this:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
Intent intent = new Intent(getApplicationContext(), YourNextActivity.class);
startActivity(intent);
}
},3000); -> the splash will show for 3000 ms, you can reduce this.
I'm making an activity where I'm planning to display a progress bar and move to a second activity after five seconds. I used Thread.sleep to do this.
Here is the code for the activity with the spinner.
public class WaitScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.waitscreen);
}
#Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
try{
for(int i=0;i<5;i++){
Thread.sleep(1000);
}
Toast.makeText(getApplicationContext(), "Spinner complete!", Toast.LENGTH_LONG).show();
}
catch(InterruptedException e){
}
Intent i = new Intent("emergency.app.NEWACTIVITY");
startActivity(i);
}
}
My main activity only has a button that leads to this page on being clicked. Problem is, it displays the main activity for five seconds, then this activity very briefly and then the third activity. How do I make the thread start after WaitScreen is fully loaded? As you can see, I tried using the onStart method to do the trick. Or if you could tell me another way to introduce a five second delay, that would be helpful too!
Use a Handler to post a Runnable, like so:
public class WaitScreen extends Activity {
private Runnable task = new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "Spinner complete!", Toast.LENGTH_LONG).show();
Intent i = new Intent("emergency.app.NEWACTIVITY");
startActivity(i);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.waitscreen);
Handler handler = new Handler();
handler.postDelayed(task, 5000);
}
}
The Runnable task will execute after 5 seconds.
http://developer.android.com/reference/android/os/Handler.html