After SplashScreen finish() app gets minimized - java

I'm using finish() to end a thread in my SplashScreen activity and when I do the app gets minimized before going to Activity2. How can I end the SplashScreen activity and go to Activity2 without calling finish() so the app won't minimize?
public class SplashScreen 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);
// Splash screen view
setContentView(R.layout.splash);
final SplashScreen sPlashScreen = this;
// The thread to wait for splash screen events
mSplashThread = new Thread(){
#Override
public void run(){
try {
synchronized(this){
// Wait given period of time or exit on touch
wait(5000);
}
}
catch(InterruptedException ex){
}
finish();
// Run next activity
Intent intent = new Intent();
intent.setClass(sPlashScreen, MainActivity.class);
startActivity(intent);
//stop();
}
};
mSplashThread.start();
}
/**
* Processes splash screen touch events
*/
#Override
public boolean onTouchEvent(MotionEvent evt)
{
if(evt.getAction() == MotionEvent.ACTION_DOWN)
{
synchronized(mSplashThread){
mSplashThread.notifyAll();
}
}
return true;
}
}

call finish after startActivity
mSplashThread = new Thread(){
#Override
public void run(){
try {
synchronized(this){
// Wait given period of time or exit on touch
wait(5000);
}
}
catch(InterruptedException ex){
}
// Run next activity
Intent intent = new Intent();
intent.setClass(sPlashScreen, MainActivity.class);
startActivity(intent);
//call finish after startActivity
finish();
//stop();
}
};
mSplashThread.start();

I do it like this:
private class IntentLauncher extends Thread {
#Override
/**
* Sleep for some time and than start new activity.
*/
public void run() {
try {
// Sleeping
Thread.sleep(SLEEP_TIME*1000);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
// Start main activity
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
SplashActivity.this.startActivity(intent);
SplashActivity.this.finish();
}
}

Related

Android Studio on click button after a splash screen and a main screen [duplicate]

I have an application in which I'm receiving a sms containing his location.On receiving sms it calls another activity to start and passes that location to that activity to plot it on the map.Before calling the second activity it shows a toast like notification on the screen but somehoe due to calling second activity that toast doesn't come up.My question is how can we delay the calling of second activity from this activity ?
You can use something like this:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i=new Intent(SearxhJobs.this,JobsTypes.class);
startActivity(i);
}
}, 5000);
Here it waits upto 5 seconds to launch activity.
Hope it helps
You can do it with a Handler like this
Handler h = new Handler(){
#Override
public void handleMessage(Message msg) {
Intent i = new Intent().setClass(ctx, MainActivity.class);
startActivity(i);
}
};
h.sendEmptyMessageDelayed(0, 1500); // 1500 is time in miliseconds
Make an AsyncClass that does Thread.sleep() in the doInBackground() method, then navigate to your new activity in the your onPostExecute() method.
Call your toast message and then execute the AsyncClass.
For Kotlin
Handler().postDelayed({
val i = Intent(this, MainActivity::class.java)
startActivity(i)
}, 5000)
Try:
Runnable r = new Runnable() {
#Override
public void run() {
// if you are redirecting from a fragment then use getActivity() as the context.
startActivity(new Intent(SplashActivity.this, MainActivity.class));
// To close the CurrentActitity, r.g. SpalshActivity
finish();
}
};
Handler h = new Handler();
// The Runnable will be executed after the given delay time
h.postDelayed(r, 1500); // will be delayed for 1.5 seconds
Simply set the layout!
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
setContentView(R.layout.next); //where <next> is you target activity :)
}
}, 5000);
An example would be the following:
Handler TimeDelay=new Handler();
if(previous=="geofence"){
tts.speak(previous,TextToSpeech.QUEUE_ADD,null, null);
Runnable r = new Runnable() {
#Override
public void run() {
/*
Intent intent = new Intent(
MyBroadcastMessageReceiver.class.getName());
intent.putExtra("some additional data", choice);
someActivity.sendBroadcast(intent);*/
tts.speak(previous,TextToSpeech.QUEUE_ADD,null, null);
}
};
TimeDelay.postDelayed(r, 150000);

Deactivate SplashScreen after start

When i start my application comes a SplashScreen for 3 seconds after that i am on my MainActivity when i press now the back button from my mobile im back on the SplashScreen. How can i adjust when i press the back button that, that close the app?
I have programmed this
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == event.KEYCODE_BACK){
if (!pressedOnce){
pressedOnce = true;
Toast.makeText(getApplicationContext(), "Erneut drücken,um zu beenden.", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
pressedOnce = false;
}
}, 3000);
}else if (pressedOnce){
pressedOnce = false;
onBackPressed();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
Start your MainActivity from the Splash screen with a flag and finish the splash Activity:
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
When you launch Intent to start your MainActivity write this.finish() to terminate SplashScreen activity and to remove it from your heap
Put finish(); after the startActivity code where you start MainActivity in SplashScreen.class.

Activity whith pitch detect TarsosDSP no restart

I wrote a guitar tuner in music application. The main activity launch the tuner when click on button. The tuner works fine. But if I come back to main activity.
The Tuner activity start, but pitch detection does not.
The thread don't stop for the recorder.
I launch an activity Act_Accordage from the main activity with
public void clicAccordage(View v){
Intent intentAct_Accordage = new Intent(this, Act_Accordeur .class);
startActivity(intentAct_Accordage);
}
Here is my Act_Accordage activity
public final AudioDispatcher dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(22050, 2048, 0);
....
dispatcher.addAudioProcessor(new PitchProcessor(PitchEstimationAlgorithm.YIN, 22050, 2048, new PitchDetectionHandler() {
#Override
public void handlePitch(PitchDetectionResult pitchDetectionResult,
AudioEvent audioEvent) {
final float pitchInHz = pitchDetectionResult.getPitch();
if (Logo.interrupted()) {
return;
}
if (pitchInHz > -1) {
runOnUiThread(new Runnable() {
#Override
public void run() {
// display name note and cursor ou UI thread
textFreqNote.setText(String.valueOf(pitchInHz) + " CREATE ");
Affcurseur(pitchInHz);
});
}
}
}));
new Thread(dispatcher, "Audio Dispatcher");
2***/I come back main with button or backbutton
public void onclicSetBackMain(View v) {
Act_Accordeur.this.finish();
Toast.makeText(Act_Accordeur.this, "Guitare accordée", Toast.LENGTH_LONG).show();}
Use dispatcher.stop(); to stop the Audio Dispatcher thread.It took me a long time in the official documentation but it was worth it.

Stop/Pause/Sleep Service if connection unavailable and resume if available

I have an application in which I have created a service MyService.class
Now MyService.class is tied to my activity using bindService() but I want my service to run in the background even if activity destroys itself.
So I started the service and then bind it like below:
private void doBindService() {
if (!isServiceBound){
Log.d(LOG_TAG, "Binding Service...");
if (mBtAdapter != null && mBtAdapter.isEnabled()){
Intent intent = new Intent(MyActivity.this, MyService.class);
startService(intent);
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}
}
}
In MyActivity's onDestroy method I am unbinding the service
Now my service is running smoothly until the connection with the remote device breaks. I want to pause/sleep/stop this service if connection breaks, then after every 60 seconds it tries to start the service/connection.
I have tried this but doesn't work.
private void stopService() {
doUnbindService();
stopService(new Intent(MyActivity.this, MyService.class));
startService(new Intent(MyActivity.this, MyService.class));
}
Please any help will be much appreciated. Thanks in advance!!!
Try this
create a method that perform the Operation you want
create a Thread or Runnable class
Call the Helper method you created in the run() of the Thread or Runnable
Inside the Service onStartCommand start the Thread if connection is available
thread.wait/sleep if no connection
I'm not sure I have understanding your request.
With this solution you can play, pause and stop service.
And the service do some work every 60seconds
public class MyService extends Service {
public static boolean testConnexion;
private Timer timer;
#Override
public void onCreate() {
super.onCreate();
timer = null;
testConnexion = true;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (timer != null) {
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
if (testConnexion) {
//StartConnexion
}
}
}, 1000, 60000);
}
return START_NOT_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
timer.cancel();
}
}
In Any activity
To start or stop service.
(Your can call startService many times as you want, there will be just one who will run.)
if (v.getId() == R.id.startService) {
Intent intent = new Intent(this, MyService.class);
startService(intent);
} else if (v.getId() == R.id.stopService) {
Intent intent = new Intent(this, MyService.class);
stopService(intent);
}
To pause action (but not kill the service)
MyService.testConnexion = false;
And for restarting
MyService.testConnexion = true;
Your service is not related to your activity.
If your killing your activity, your service continues to run.
I hope this can help you

How to terminate an activity and switch to another one?

I'm doing something like this:
public class MyActivity extends Activity {
#Override
public void onStart() {
super.onStart();
User user = new Entrance().enter();
// continue with user, since it's a valid one
}
}
// I'm using this class from many activities
public class Entrance {
public User enter() {
try {
User user = new Hub().enter();
} catch (AuthenticationFailedException ex) {
this.startActivity(new Intent(this, LoginActivity.class));
this.finish();
}
return user;
}
}
This code doesn't work because finish() doesn't throw an exception and let the execution flow continue. What is the proper design for this scenario?
You could use the Activity.isFinishing() method to seperate your flow of logic.
public void onStart() {
super.onStart();
User user = new Entrance().enter();
// continue with user, since it's a valid one
if (isFinishing()) {
...
} else {
...
}
}
Since you're using the Entrance class from multiple Activities, you should pass the Context of the Activity to the enter() method in the Entrance class. It should be as follows:
public User enter(Context context) {
try {
User user = new Hub().enter();
} catch (AuthenticationFailedException ex) {
context.startActivity(new Intent(context, LoginActivity.class));
context.finish();
}
return user;
}
Try this, first create an intent ,2nd finish the activity start next/desired activity through intent
Intent loginIntent=new Intent(MyActivity.this, LoginActivity.class);
MyActivity.this.finish();
startActivity(loginIntent);

Categories