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.
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 creating a New Android application
I d like to switch from one activity to another activity after a time interval, How can i do this?
Kindly guide me
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// This method will be executed once the timer is over
// Start your app Next activity
Intent i = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(i);
// close this activity
finish();
}
}, TIME_OUT);
There are numerous ways to do this.
You could use postDelayed(), however that is not advised since you cannot STOP it, or control it reliably, between various phases of activity lifecycle, to prevent for example wierd behaviour when the user exits the activity, before the delay has passed.
You would need some locks, or other mechanism.
Most proper approach would be to simply start a timer on the 1st activity onPostResume() which will start another activity after some delay.
TimerTask mStartActivityTask;
final Handler mHandler = new Handler();
Timer mTimer = new Timer();
#Override
private protected onPostResume() { // You can also use onResume() if you like
mStartActivityTask = new TimerTask() {
public void run() {
mHandler.post(new Runnable() {
public void run() {
startNewActivity(new Intent(MyClass.class));
}
});
}};
// This will start the task with 10 seconds delay with no intervals.
mTimer.schedule(mStartActivityTask, 100000, 0);
}
private void startNewActivity(Intent i) {
mTimer.cancel(); // To prevent multiple invocations
startActivity(i); // Start new activity
// finish(); // Optional, depending if you want to return here.
}
Try this code
private Thread thread;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
thread = new Thread(this);
thread.start();
}
#Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent userName = new Intent(this, UserNameActivity.class);
startActivity(userName);
}
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);
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);