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);
Related
I have an android app in which I have an activity and a service.
The service is supposed to play music and switch between four songs (depends on what button you press)
The problem is that a few minutes/seconds (it's not always at the same time) after you start the service by playing a song a notification pop ups which claims that the 'app has stopped responding' (with the option to wait and the option to exit). If you press the wait button the notification just goes away and the app and music continues the same. The notification will keep coming back though and unfortunately I am not sure why.
Should probably mention that this only happens to me when the service is on. I have tested this app many times. Never have a notification like that. Only when music is played.
My service:
package com.example.project25112021;
import android.app.Service;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.widget.Toast;
import androidx.annotation.Nullable;
public class MyService extends Service {
MediaPlayer mediaPlayer;
MediaPlayer mediaPlayer2;
MediaPlayer mediaPlayer3;
MediaPlayer mediaPlayer4;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
mediaPlayer = MediaPlayer.create(this, R.raw.stronger);
mediaPlayer.setLooping(true); // Set looping
mediaPlayer.setVolume(100, 100);
mediaPlayer2 = MediaPlayer.create(this, R.raw.silksonic);
mediaPlayer2.setLooping(true); // Set looping
mediaPlayer2.setVolume(100, 100);
mediaPlayer3 = MediaPlayer.create(this, R.raw.davidguetta);
mediaPlayer3.setLooping(true); // Set looping
mediaPlayer3.setVolume(100, 100);
mediaPlayer4 = MediaPlayer.create(this, R.raw.beatit);
mediaPlayer4.setLooping(true); // Set looping
mediaPlayer4.setVolume(100, 100);
}
public int onStartCommand(Intent intent, int flags, int startId) {
int number = intent.getIntExtra("key", 0);
mediaPlayer.start();
mediaPlayer2.start();
mediaPlayer3.start();
mediaPlayer4.start();
mediaPlayer.seekTo(0);
mediaPlayer2.seekTo(0);
mediaPlayer3.seekTo(0);
mediaPlayer4.seekTo(0);
mediaPlayer.pause();
mediaPlayer2.pause();
mediaPlayer3.pause();
mediaPlayer4.pause();
if (number == 0){
mediaPlayer.start();
}
if (number == 1){
mediaPlayer2.start();
}
if (number == 2){
mediaPlayer3.start();
}
if (number == 3){
mediaPlayer4.start();
}
return startId;
}
//public void onStart(Intent intent, int startId) { }
#Override
public void onDestroy() {
mediaPlayer.stop();
mediaPlayer.release();
}
#Override
public void onLowMemory() {
}
}
The activity in which you choose what song to play:
package com.example.project25112021;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class SettingsMain extends AppCompatActivity {
Button button;
Button song1;
Button song2;
Button song3;
Button song4;
ImageView album1;
ImageView album2;
ImageView album3;
ImageView album4;
Drawable drawable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings_main);
drawable = CameraBackground.getInstance().getBitmap();
LinearLayout relative = (LinearLayout) findViewById(R.id.ButtonFinBack);
if(drawable!= null) {
relative.setBackgroundDrawable(drawable);
}
else
relative.setBackgroundResource(R.drawable.bacckga1);
button = findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(SettingsMain.this, MainActivity.class);
startActivity(intent);
}
});
song1 = findViewById(R.id.song1);
song2 = findViewById(R.id.song2);
song3 = findViewById(R.id.song3);
song4 = findViewById(R.id.song4);
album1 = findViewById(R.id.album1);
album2 = findViewById(R.id.album2);
album3 = findViewById(R.id.album3);
album4 = findViewById(R.id.album4);
album1.setImageResource(R.drawable.kanye);
album2.setImageResource(R.drawable.silksonic);
album3.setImageResource(R.drawable.davidguetta);
album4.setImageResource(R.drawable.mjthriller);
song1.setText("Play");
song2.setText("Play");
song3.setText("Play");
song4.setText("Play");
song1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(SettingsMain.this, MyService.class);
intent.putExtra("key",0);
startService(intent);
}
});
song2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(SettingsMain.this, MyService.class);
intent.putExtra("key",1);
startService(intent);
}
});
song3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(SettingsMain.this, MyService.class);
intent.putExtra("key",2);
startService(intent);
}
});
song4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(SettingsMain.this, MyService.class);
intent.putExtra("key",3);
startService(intent);
}
});
}
}
Thanks
I'm trying to switch actvities. Each activity only has one image view so I didn't find it necessary to show xml. It's a relatively simple program. Based on the IF-STATEMENT the program should switch to its appropriate view. All the images are 72dpi jpegs.
package app.com.example.android.chancetheoracle;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this, "Think Of Your Question, Then Tap Anywhere.",Toast.LENGTH_LONG).show();
}
public void onClick(View view){
Random answer = new Random();
int ask = answer.nextInt(6) + 1;
if (ask == 1){
Intent intent = new Intent(this, redThree.class);
startActivity(intent);
Toast.makeText(this, "2/5! Click Anywhere To Ask Again.",Toast.LENGTH_SHORT).show();
}
else if (ask == 2){
Intent intent = new Intent(this, greenThree.class);
startActivity(intent);
Toast.makeText(this, "3/5! Click Anywhere To Ask Again.",Toast.LENGTH_SHORT).show();
}
else if (ask == 3){
Intent intent = new Intent(this, greenFour.class);
startActivity(intent);
Toast.makeText(this, "4/5! Click Anywhere To Ask Again.",Toast.LENGTH_SHORT).show();
}
else if (ask == 4){
Intent intent = new Intent(this, redFour.class);
startActivity(intent);
Toast.makeText(this, "1/5! Click Anywhere To Ask Again.",Toast.LENGTH_SHORT).show();
}
else if (ask == 5){
Intent intent = new Intent(this, redFive.class);
startActivity(intent);
Toast.makeText(this, "0/5! Click Anywhere To Ask Again.",Toast.LENGTH_SHORT).show();
}
else if (ask == 6){
Intent intent = new Intent(this, greenFive.class);
startActivity(intent);
Toast.makeText(this, "5/5! Click Anywhere To Ask Again.",Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, "Think Of Your Question, Then Tap Anywhere.",Toast.LENGTH_LONG).show();
}
}
public static class greenThree extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_green_three);
}
}
public static class redThree extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_red_three);
}
}
public static class greenFour extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_green_four);
}
}
public static class redFour extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_red_four);
}
}
public static class greenFive extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_green_five);
}
}
public static class redFive extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_red_five);
}
}
}
You haven't assigned the onClick listener to anything. It's not going to fire on it's own.
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
For the memory issue, refer to this thread:
Android:java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM
https://developer.android.com/reference/android/widget/Button.html
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 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();
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;
}
}