I have an Android program that includes splash activity. when I run the program everything working perfectly but when press the back button I see my splash activity again .to be able to avoid this it seems I need to close my splash but I can't
here is my code
package com.tesbih;
import android.app.Activity;
import android.content.Intent;
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 openStartingPoint = new Intent ("com.tesbih.TESBIHMAINACTIVITY");
startActivity(openStartingPoint);
}
}
};
timer.start();
}
}
Add a TimerTask to your code.
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
TimerTask splash = new TimerTask(){
public void run() {
onDestroy();
startActivity(new Intent(Splash.this, MainActivity.class));
}
};
Timer splashScreenTimer = new Timer();
// Timer set to 4.5 seconds
splashScreenTimer.schedule(splash, 5000);
}
Call finish() to finish your Splash Activity.
Do like this
Intent openStartingPoint = new Intent ("com.tesbih.TESBIHMAINACTIVITY");
startActivity(openStartingPoint);
finish();
Related
I am implementing a splash screen as per the tutorial here, however the splash screen disappears very quickly almost instantly. What would be the best way to include a timer to only start the new activity after for example 1 second. My Splash screen Activity file looks as follows:
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
The approach relies on a drawable and style resource.
Code for Splash :-
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;
public class Splash extends AppCompatActivity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_activity);
// Showing splash screen with a timer.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// Start your application main_activity
Intent i = new Intent(Splash.this, MainActivity.class);
startActivity(i);
// Close this activity
finish();
}
}, SPLASH_TIME_OUT); // Timer
}
}
The simplest way would be to use Handler:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}, DateUtils.SECOND_IN_MILLIS);
public class SplahActivity extends Activity {
public static final int Tick = 1000;
public static final int Complete = 5000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new CountDownTimer(Complete, Tick) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
//start Activity
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}.start();
}
Use this
timerHandler = new Handler();
getmi_runnable = new Runnable() {
#Override
public void run() {
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
finish();
}
};
timerHandler.postDelayed(getmi_runnable, 4000L);
When I try running this code it says "Error:not an enclosing class:MainActivity" and "Error:Missing method body or declare abstract." And there's probably a really simple answer for this but I am new to Android and Java programming, so sorry for my noob-ishness.
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Intent myIntent = new Intent (MainActivity.this, MainActivity2.class);
startActivityForResult(myIntent, 0);
Button btn = (Button) findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v);
});
}
//Activities
public void onStart() {
super.onStart();
Log.i("TaskActivity", "MainActivity Started");
}
public void onResume() {
super.onResume();
Log.i("TaskActivity", "MainActivity Resumed");
}
public void onPause() {
super.onPause();
Log.i("TaskActivity", "MainActivity Paused");
}
public void onStop() {
super.onStop();
Log.i("TaskActivity", "MainActivity Stopped");
}
#Override
public void onDestroy() {
super.onDestroy();
Log.i("TaskActivity", "MainActivity Destroyed");
}
}
The first argument of your Intent() should be the current (enclosing) class. The second argument is the Activity to load. Check the Developer Docs here
If you want to launch MainActivity from MainActivity2, reverse the arguments so your Intent looks like:
Intent myIntent = new Intent(MainActivity2.this, MainActivity.class);
startActivity(myIntent);
The error relating to your method likely comes from using the incorrect syntax when assigning your onClickListener. If you are trying to start MainActivity from a button press, you will need to re-write this section:
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity2.this, MainActivity.class);
startActivity(intent);
}
});
You need to implement the onClick Method
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
}
});
I want to create a small android app that would show the system time in periodic intervals after clicking on a button ( i.e. setting the activity up )...The code for button creation and setting the periodic activity via Intent goes like this :
package com.example.timeupdate;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button button;
TextView show;
#Override
protected void onCreate(Bundle I_Love_Biriyani) {
super.onCreate(I_Love_Biriyani);
setContentView(R.layout.activity_main);
button = (Button) findViewById (R.id.pressButton);
show = (TextView) findViewById (R.id.Show);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent openTimeUpdater = new Intent("com.example.timeupdate.TIMEUPDATER");
startActivity(openTimeUpdater);
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
#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;
}
}
And here is the code for repeating the timer( for say 5 seconds ) where I used TimerTask class to perform the job :
package com.example.timeupdate;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class TimeUpdater extends Activity {
TextView Show;
TimerTask timer= new TimerTask(){
#Override
public void run() {
// TODO Auto-generated method stub
Date d = new Date();
Show.setText(""+d);
}
};
#Override
protected void onCreate(Bundle hotovaga) throws IllegalStateException {
// TODO Auto-generated method stub
super.onCreate(hotovaga);
setContentView(R.layout.new_update);
Show = (TextView) findViewById (R.id.time);
Timer t = new Timer();
t.scheduleAtFixedRate(timer , 0 , 5000);
}
}
After clicking on the button the time is shown only once then application is getting stopped showing a dialog-message. Need explanations to do this job in the same fashion.
You are trying to access an UI element inside non-UI thread.
Show.setText(""+d);
Instead, wrap it up in runOnUiThread interface to get proper output.
Use below code for your TimeUpdater class
public class TimeUpdater extends Activity {
TextView Show = null;
Calendar c;
int seconds;
int minutes;
int hours;
TimerTask timer= new TimerTask(){
#Override
public void run() {
c = Calendar.getInstance();
seconds = c.get(Calendar.SECOND);
minutes = c.get(Calendar.MINUTE);
hours = c.get(Calendar.HOUR);
runOnUiThread(new Runnable() {
#Override
public void run() {
Show.setText(hours + ":" + minutes + ":" + seconds);
}
});
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_update);
Show = (TextView) findViewById (R.id.time);
Timer t = new Timer();
t.scheduleAtFixedRate(timer , 0 , 5000);
}
}
Using an actual Timer (java.util.Timer) in conjunction with runOnUiThread() is one way to solve this issue, and below is an example of how to implement it.
public class myActivity extends Activity {
private Timer myTimer;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
#Override
public void run() {
TimerMethod();
}
}, 0, 1000);
}
private void TimerMethod()
{
//This method is called directly by the timer
//and runs in the same thread as the timer.
//We call the method that will work with the UI
//through the runOnUiThread method.
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable() {
public void run() {
//This method runs in the same thread as the UI.
// Set your textView data here.
//Do something to the UI thread here
}
};
}
use PeriodicTask from Play-Service, it is the newest tool from Google to schedule a job background.
I am a newbie in android app development. For now my challenge is to add two buttons in my UI one for playing an mp3 file and the other for stopping it. I am able to do the first successfully and as I try to do the stop one, I see no result. My code is as following:
package ir.polyglotcenter.childrenmostfrequentwords;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button myButtonOne = (Button) findViewById(R.id.btn_audio);
myButtonOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer media = MediaPlayer.create(Main.this, R.raw.audio);
media.start();
}
});
//this is to stop the audio flow
Button myButtonTwo = (Button) findViewById(R.id.btn_stop);
myButtonTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer media = MediaPlayer.create(Main.this, R.raw.audio);
media.stop();
}
});
}
}
Try this code:
public class Main extends Activity {
protected MediaPlayer mMediaPlayer;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMediaPlayer = MediaPlayer.create(Main.this, R.raw.audio);
Button myButtonOne = (Button) findViewById(R.id.btn_audio);
myButtonOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mMediaPlayer.prepare();
mMediaPlayer.start();
}
});
//this is to stop the audio flow
Button myButtonTwo = (Button) findViewById(R.id.btn_stop);
myButtonTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(mMediaPlayer.isPlaying())
mMediaPlayer.stop();
}
});
}
}
Play and Stop all song for all rows.
The Point is implement MediaPlayer in the loop of if or any loop you have:
if(playandstop==true){
mp=MediaPlayer.create(MainActivity.this, resourceIIIDD);
mp.start();
playandstop=false;
}else{
mp.stop();
playandstop=true;
}
Once my splash screen display for 1000ms I receive an error stating "The application has stopped unexpectedly. Please try again." It seems an activity that is supposed to start after the splash screen is not working. before the splash screen, everything worked fine. Logcat
shows the following error " E/AndroidRuntime(5480): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxxx.home/com.xxxxx.home.xxxxx}: java.lang.NullPointerException. I beleive the issue is with my Splash Class but cannot pin point where. Any insight would be greatly appreciated.
public class Splash extends Activity{
private final int SPLASH_DISPLAY_LENGTH = 1000;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
Intent openxxxxx = new Intent("com.xxxxx.home.XXXXX");
startActivity(openxxxxx);
}
}, SPLASH_DISPLAY_LENGTH);
}
}
Here is the complete code you can use this,
package com.fsp.slideview;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
public class ImageSplashActivity extends Activity {
/**
* The thread to process splash screen events
*/
private Thread mSplashThread;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash);
final ImageSplashActivity sPlashScreen = this;
mSplashThread = new Thread() {
#Override
public void run() {
try {
synchronized (this) {
wait(2000);
}
} catch (InterruptedException ex) {
}
finish();
Intent intent = new Intent();
intent.setClass(sPlashScreen, SlideMainActivity.class);
startActivity(intent);
}
};
mSplashThread.start();
}
#Override
public boolean onTouchEvent(MotionEvent evt) {
if (evt.getAction() == MotionEvent.ACTION_DOWN) {
synchronized (mSplashThread) {
mSplashThread.notifyAll();
}
}
return true;
}
}