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);
Related
I am now trying to delay for a certain time before going to the next intent. I tried with postdelayed method but nothing works.
final Handler mHandler = new Handler();
runInBackground(
new Runnable() {
#Override
public void run() {
if(condition has met){
Runnable runnable = new Runnable() {
public void run() {
Intent i=new Intent(DetectorActivity.this,Main4Activity.class);
startActivity(i);
finish();
}
};
mHandler.postDelayed(runnable, 1000000);
}
}
});
You have 2 Runnable but you're just running internal one by using postDelayed().
You should run the first one too.
like this
runInBackground(new Runnable() {
.
.
.
.
.
}.run());
I am trying to restart the same Activity after a specific time say after 2 min when a button is clicked. However, it does close the activity, however does not launch in the specified time , here is the code:
public void snoozeup(View view)
{
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
#Override
public void run()
{
//start your activity here
startActivity(new Intent(Time_Date.this, Time_Date.class));
}
}, a); //where a is integer with value 120000
mp.stop();
mp.release();
voicePlayer.stop();
voicePlayer.release();
songPlayer.stop();
songPlayer.release();
this.finish();
}
You can't do it in that way - once your Activity is finished, all UI threads are stopped. Your Runnable will never be called.
If you want some functionality to run when your Activity is closed, you need to create a Service.
You should also take note of the problems with using postDelayed as described in postDelayed() in a Service.
I fixed it by using:
public void snoozeup(View view)
{
Handler handler = new Handler();
Runnable x=new Runnable() {
#Override
public void run()
{
startActivity(new Intent(Time_Date.this, Time_Date.class));
}
};
handler.postDelayed(x, 6000);
mp.stop();
mp.release();
voicePlayer.stop();
voicePlayer.release();
songPlayer.stop();
songPlayer.release();
finish();
}
I am developing and android application in that I want to start another application(my second application). I am doing the following code
Intent i= new Intent();
i.setComponent(new ComponentName("my second app package","my class name"));
startActivity(i);
It is working fine.
I want to hide the second application after 3 or 5 seconds for that I am following the below code
Timer t=new Timer();
t.schedule(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
Intent i = new Intent("first app package","first app class name" );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
}, 5000);
But this code is not working as I expected only for my second application. But other applications are working fine. Instead thread I also tried Handler, alaram manager also no sucess. Please any one help me in this.
Is I want to do any code change in my second application or what is the problem with my code?
Thanks
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// your code to start second activity. Will wait for 3 seconds before calling this method
startActivity(new Intent(FirstActivityClass.this,SecondActivityClass.class));
}
}, 3000);
Use above code after onCreate of first activity
Try postDelayed or Timer method
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 100ms
//Intent i = new Intent("first app package","first app class name" );
Intent i = new Intent (this , SecondActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
}, 100);
// Time method
import java.util.Timer;
...
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
// Your database code here
}
}, 2*60*1000);
you can use thread but main thing is you have to call finish() method to finish current activity.
Thread thread = new Thread( new Runnable() {
#Override
public void run() {
try
{
Thread.sleep(3000);
}
Intent intent = new Intent(1stActivity.this, SecondActivity.class);
startActivity(intent);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
finish();
}
}
});
thread.start();
in the try block i put Thread.sleep(3000) you can change it do your work in try block
Try this one:
try{
Thread.sleep(3000);
startActivity(secondActivityIntent);
}catch(Exception e){
//print error here
}
You want the second application to hide after few seconds, Please put this code inside onCreate() of your second application's main Activity:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
YourActivity.this.finish();
}
}, 3000);
handler.post(new Runnable() {
#Override
public void run() {
//Your Code here
}
});
Put this code inside run method of your timer task.And it will work.Hope it will help.Thanks.
I am developing an android application. In this application, I want to transition from one activity to another activity automatically after 4 seconds. I don't know how to do this without a button.
This is how you can proceed:
int timeout = 4000; // make the activity visible for 4 seconds
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
finish();
Intent homepage = new Intent(Activity1.this, Activity2.class);
startActivity(homepage);
}
}, timeout);
Add the code in your oncreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
Handler handler=new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(MainActivity.this, AnotherActivity.class);
startActivity(intent);
}
},4000);
}
You can add a Handler in you activity, like:
private Handler handler = new Handler();
Then in your onCreate() method of activity, you can call:
handler.postDelayed(new Runnable() {
#Override
public void run() {
startActivity(yourIntent);
}
}, 4000);
I want that a new activity should start with some delay on pressing a button.
Is it possible to do that , and whats the procedure.
Use a postDelayed() call with a runnable that launches your activity.
An example code could be
//will care for all posts
Handler mHandler = new Handler();
//the button's onclick method
onClick(...)
{
mHandler.postDelayed(mLaunchTask,MYDELAYTIME);
}
//will launch the activity
private Runnable mLaunchTask = new Runnable() {
public void run() {
Intent i = new Intent(getApplicationContext(),MYACTIVITY.CLASS);
startActivity(i);
}
};
Note that this lets the interface remain reactive. You should then care for removing the onclick listener from your button.
Use This code
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
final Intent mainIntent = new Intent(CurrentActivity.this, SecondActivity.class);
LaunchActivity.this.startActivity(mainIntent);
LaunchActivity.this.finish();
}
}, 4000);
You could call a Runnable using the Handler postDelayed() method.
Here's an example (http://developer.android.com/resources/articles/timed-ui-updates.html):
private Handler mHandler = new Handler();
...
OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
mHandler.postDelayed(mUpdateTimeTask, 100);
}
};
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
// do what you need to do here after the delay
}
};
Props to #mad for getting it right the first time around.
You can use the method postDelayed(Runnable action, long delayMillis) of a View to add a Runnable to the message queue to be run after an (approximate) delay.
Sometimes, u need to do it whenever your app process is killed or not. In that case you can not use handling runnable or messages inside your process. In this case your can just use AlarmManager to this. Hope this example helps anybody:
Intent intent = new Intent();
...
PendingIntent pendingIntent = PendingIntent.getActivity(<your context>, 0, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, <your delay>, pendingIntent);
runOnUiThread(new Runnable() {
#Override
public void run() {
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
}, 4000);
}
});
try this piece of code
new Timer().schedule(new TimerTask() {
#Override
public void run() {
// run AsyncTask here.
}
}, 3000);