Splash screen wont destoy itself? Java - android

At first my splash screen works perfectly, however later I tried to put in a code which would destroy the activity splash. I did this by putting the onPause method into the end of the protected void.
This is the splash screen before putting in the method
'package com.shipment.emulatorfix;
'import android.app.Activity;
'import android.content.Intent;
'import android.media.MediaPlayer;
'import android.os.Bundle;
'public class Splash extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
} catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent openMain = new Intent("android.intent.action.TESTINGEMULATORACTIVITY");
startActivity(openMain);
}
}
};
timer.start();
}
'}
this is the after code
'package com.shipment.emulatorfix;
'import android.app.Activity;
'import android.content.Intent;
'import android.media.MediaPlayer;
'import android.os.Bundle;
'public class Splash extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
} catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent openMain = new Intent("android.intent.action.TESTINGEMULATORACTIVITY");
startActivity(openMain);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
'}
Any help would be greatly appreciated, thank you.

try this
public class SplashScreen extends Activity {
protected int _splashTime = 2000;
private Thread splashTread;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
splashTread = new Thread() {
#Override
public void run() {
try {
synchronized(this) {
wait(_splashTime);
}
} catch(InterruptedException e) {
System.out.println("EXc=" + e);
}
finally {
startActivity(new Intent(SplashScreen.this, Login.class ));
//stop();
finish();
}
}
};
splashTread.start();
}
}

public class Welcome extends Activity
{
/** Called when the activity is first created. */
Handler mHandler,actHandler;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
new Thread(){
public void run(){
try{
Thread.sleep(3000);
}
catch(Exception ex){
Log.e("Welcome Exception :",ex.toString());
}
try{
Message msg=mHandler.obtainMessage();
mHandler.sendMessage(msg);
}
catch(NullPointerException ex){
Log.e("Handler Exception :",ex.toString());
}
}
}.start();
mHandler=new Handler(){
public void handleMessage(Message msg){
super.handleMessage(msg);
Intent i=new Intent(Welcome.this,M_chat.class);
startActivity(i);
finish();
}
};
}
}

Finish the activity and then start the other activity.
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
} catch(InterruptedException e){
e.printStackTrace();
}finally{
finish();
Intent openMain = new Intent("android.intent.action.TESTINGEMULATORACTIVITY");
startActivity(openMain);
}
}
};

This should work for you.
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread() {
public void run() {
try {
sleep(5000);
} catch(InterruptedException e){
e.printStackTrace();
} finally {
Intent openMain = new Intent("android.intent.action.TESTINGEMULATORACTIVITY");
startActivity(openMain);
Splash.this.finish();
}
}
};
timer.start();
}

Related

Intents in android

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class Second extends AppCompatActivity {
TextView t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
t1 = (TextView)findViewById(R.id.textView);
Bundle b = getIntent().getExtras();
String S = b.getString("key1");
t1.setText(S);
try {
wait(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent i2 = new Intent();
i2.putExtra("reply","Everything is cool");
setResult(RESULT_OK,i2);
finish();
}
}
This is my second Activity code. Actually, I was seeing how intent works by implementing a very basic program. Program is working without wait, but the app is crashing if I put wait function. provide me some way so that i can stay on to the second activity for a longer time before sending back the r
Use Handler and try to start Intent from run() You can check below example,
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent i2 = new Intent();
i2.putExtra("reply","Everything is cool");
setResult(RESULT_OK,i2);
finish();
}
}, 3000);
you can use service or acyncTask instead of
try {
wait(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
You could try this code
public void onclick(View v) {
mt = new MyTask();
mt.execute();
}
class MyTask extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... params) {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
Hope it will be helpful.

Android interrupting a Sleep Thread

I'd appreciate your help in interrupting an Android/Java sleep. What I have in my layout is a button, which if clicked, calls the Skip method and starts a new activity. FYI The same activity would be called anyway when the Sleep method terminates.
Here's my failing code:
public class Splash extends Activity {
private Thread timer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
timer = new Thread() {
public void run() {
try {
sleep(5000);
} catch (Exception e) {
e.printStackTrace();
onPause();
return;
} finally {
onPause();
startActivity(new Intent("net.example.splashscreenexample.MainActivity"));
}
}
};
timer.start();
}
#Override
public void onPause() {
timer.interrupt();
super.onPause();
finish();
}
public void Skip() {
timer.interrupt();
startActivity(new Intent("net.example.splashscreenexample.MainActivity"));
}
Now Resolved!
I've now got it all working. In addition to #RocketSpock's suggestions there was also a stupid error in my code in that I'd failed to include the View view paramater into my Skip method call. So the fully working code now looks like this:
public class Splash extends Activity {
private Thread timer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
timer = new Thread() {
public void run() {
synchronized (this) {
try {
sleep(5000);
} catch (Exception e) {
e.printStackTrace();
onPause();
return;
} finally {
onPause();
startActivity(new Intent(
"net.rogw.splashscreenexample.MainActivity"));
}
}
}
};
timer.start();
}
#Override
public void onPause() {
timer.interrupt();
super.onPause();
finish();
}
public void Skip(View view) {
synchronized (this) {
this.notify();
}
startActivity(new Intent("net.rogw.splashscreenexample.MainActivity"));
}
}
If you want to be able to interrupt it you should be using a wait.
public class Splash extends Activity {
private Thread timer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
timer = new Thread() {
public void run() {
synchronized(this) {
try {
wait(5000);
} catch (Exception e) {
e.printStackTrace();
onPause();
return;
} finally {
onPause();
startActivity(new Intent("net.example.splashscreenexample.MainActivity"));
}
}
}
};
timer.start();
}
#Override
public void onPause() {
timer.interrupt();
super.onPause();
finish();
}
public void Skip() {
//You may need to replace this with the timer object
synchronized (this) {
//Informs the wait to interrupt.
this.notify();
}
startActivity(new Intent("net.example.splashscreenexample.MainActivity"));
}

How do I set a time limit to my splash screen? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am coding in Eclipse for an Android App. I have developed a splash screen which I need to display for 5 seconds before my app starts. How to do it?
Thread timer=new Thread()
{
public void run() {
try {
sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
Intent i=new Intent(SplashScreen.this,MainActivity.class);
finish();
startActivity(i);
}
}
};
timer.start();
Use the Async Class to perform the sleep operation in the doinbackground function and in the post function do the rest of the task
public class SplashScreenActivity extends Activity {
private final int SPLASH_DISPLAY_LENGHT = 5000;
#Override
public void onCreate(Bundle icicle)
{ super.onCreate(icicle);
try{
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splashscreen);
}catch(Exception e){
e.printStackTrace();
}
new MyAsyncTask().execute();
}
private class MyAsyncTask extends AsyncTask<Void, Void, Void>{
#Override
protected void onPreExecute(){
// show your progress dialog
}
#Override
protected Void doInBackground(Void... voids){
try {
Thread.sleep(SPLASH_DISPLAY_LENGHT);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void params)
{
startActivity(new Intent(SplashScreenActivity.this, HomeActivity.class));
finish();
}
}
}
use like that
public class SplaceScreenActivity extends Activity {
private static final int SPLASH_DISPLAY_TIME = 2500;
// SplashScreen Splash;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splacescreen);
new Handler().postDelayed(new Runnable() {
public void run() {
Intent intent = new Intent();
intent.setClass(SplaceScreenActivity.this,
HomeScreenActivity.class);
SplaceScreenActivity.this.startActivity(intent);
SplaceScreenActivity.this.finish();
}
}, SPLASH_DISPLAY_TIME);
}
}
Try below code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash);
final int welcomeScreenDisplay = 2000;
/** create a thread to show splash up to splash time */
Thread welcomeThread = new Thread() {
int wait = 0;
#Override
public void run() {
try {
super.run();
/**
* use while to get the splash time. Use sleep() to increase
* the wait variable for every 100L.
*/
while (wait < welcomeScreenDisplay) {
sleep(100);
wait += 100;
}
} catch (Exception e) {
} finally {
startActivity(new Intent(MainActivity.this,
HomeActivity.class));
finish();
}
}
};
welcomeThread.start();
}
}
Handler handler = new Handler();
Runnable run = new Runnable() {
public void run() {
// TODO Auto-generated method stub
startActivity(new Intent(SplaceActivity.this, New.class));
overridePendingTransition(0, 0);
finish();
}
};
handler.postDelayed(run, 3000);
Use AsyncTask or thread for this purpose.
http://www.androidhive.info/2013/07/how-to-implement-android-splash-screen-2/
hope it helps
I use Timer:
Timer timer = new Timer();
timer.schedule(new TimerTask(){
#Override
public void run() {
// TODO Auto-generated method stub
Intent home_page = new Intent(Splash.class,HomePage.class);
startActivity(home_page);
finish();
}}, 5000);
you can use Sleep method like this in your Splash Activity onCreate method:
Thread timer1 = new Thread(){
#Override
public void run(){
try{
sleep(4000);
}
catch (InterruptedException e){
e.printStackTrace();
}
finally{
Intent intent = new Intent(SplashActivity.this, NextActivity.class);
startActivity(intent);
}
}
};
timer1.start();
this take 4 sec to load NextActivity.
Add these few lines of code in your splashActivity and it will start your second activity after 5seconds.
new Handler().postDelayed(new Runnable(){
public void run() {
Intent mainIntent = new Intent(splashScreen.this,MainActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
splashScreen.this.startActivity(mainIntent);
splashScreen.this.finish();
}
}, 5000);
PostDelayed Causes the Runnable to be added to the message queue, to
be run after the specified amount of time elapses. The runnable will
be run on the thread to which this handler is attached.
Use handler
public class SplashActivity extends Activity {
int secondsDelayed = 5;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash);
Message msg = new Message();
msg.what = 0;
mHandler.sendMessage(msg);
}
Handler mHandler = new Handler()
{
public void handleMessage(android.os.Message msg) {
switch(msg.what)
{
case 1:
{
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
break;
case 0:
{
Message ms = new Message();
ms.what = 1;
mHandler.sendMessageDelayed(ms, secondsDelayed * 1000);
}
break;
}
};
};
protected void onDestroy() {
super.onDestroy();
mHandler.removeMessages(1);
};
}
Note:Donot make Splash screen for 5 sec user will get irritated make it for 2sec

Switching a View/Activity after Few seconds

I want to change the view/Activity of my app after few seconds
I mean i have created a home View for my app and i want to move to the next Activity after like 3 seconds, How should I achieve that.
Thank You
try this,
Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
Intent i = new Intent(FirstActivity.this,SeconActivity.class);
startActivity(i);
};
};
mHandler.sendEmptyMessageDelayed(0, 3000);
You can make slash Activity. Try this code....hop your problem will solve
public class SplashActivity extends Activity {
private final int SPLASH_DISPLAY_LENGHT = 2000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(SplashActivity.this, NightClubMain.class);
SplashActivity.this.startActivity(mainIntent);
SplashActivity.this.finish();
}
}, SPLASH_DISPLAY_LENGHT);
}
view.postDelayed(Runnable r, int delay);
You can use Timer for that.
Timer myTimer;
startTimerTask();
public void startTimerTask() {
MyTimerTask myTask = new MyTimerTask();
myTimer = new Timer();
myTimer.schedule(myTask, 0, 3000);
}
#Override
public void onPause() {
super.onPause();
try {
myTimer.cancel();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void onStop() {
super.onStop();
try {
myTimer.cancel();
} catch (Exception e) {
e.printStackTrace();
}
}
class MyTimerTask extends TimerTask {
public void run() {
try {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
//
Do YOUR STUFF HERE
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}

how to display another activity

How to display another activity?
I want to display one activity which has one Image View splash displaying an image. I want to display that just for 5 seconds then move on to another activity.splash is displaying in emulator but another activity menu is not displaying.
Here is my code:
com.basic.android;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class androidbasics extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
}
public void toCallActivity() {
TimerTask startNewActivity;
final Handler handler = new Handler();
final Timer timer = new Timer();
startNewActivity = new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
handler.post(new Runnable() {
public void run() {
try {
timer.cancel();
startActivity(new Intent(androidbasics.this,menu.class));
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(startNewActivity, 0,5000);
}
}
You didn't call toCallActivity().So your new Activity is not coming in the front.Write like this.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
toCallActivity();
}
try this ....
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
BackgroundTask b = new BackgroundTask();
b.execute("Main");
}
/** Called when the activity is first created. */
class BackgroundTask extends AsyncTask<String , Void, Void>
{
#Override
protected void onPreExecute()
{
setContentView(R.layout.splash);
}
#Override
protected Void doInBackground(String... params)
{
// TODO Auto-generated method stub
int pause=5000;
int interval =1000;
int counter=0;
while(counter<pause)
{
try
{
Thread.sleep(interval);
counter+=interval;
}
catch(Exception e)
{
System.out.println(e);
}
}
return null;
}
#Override
protected void onPostExecute(Void result)
{
startActivity( new Intent(androidbasics.this,menu.class));
androidbasics.this.finish();
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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 {
finish();
startActivity(new Intent(firstactivity.class,secondActivity.class));
stop();
}
}
};
splashTread.start();
}

Categories

Resources