I am new in android. Please tell me whether it is possible to send Intent after 5 minute, 10 minutes in android?
How would I do that?
Thanks in advance.
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// Launch new Intent here
}
}, ((1000 * 60) * 5)); // 5 minutes delay before execute run()
see below code it may help you. use this timer for 5 minute.
final Timer myt = new Timer();
myt.schedule(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
Intent intent= new Intent(currentActivity.this, new_activity.class);
startActivity(intent);
} catch (Exception e) {
// TODO: handle exception
}
myt.cancel();
}
}, 300000);
in above code after call intent timer terminated automatically.
Hm this can go bad if the OS decides to kill your app at some point. If you will really need that intent to still be passed after 5 minutes, you should use alarms. Take a look at this answer
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent= new Intent(youractivity.this, activitytostart5minuteslater.class);
startActivity(intent);
}
}, ((1000 * 60) * 5));
did you ever read the comments???
Related
I'm working on React Native and i want to create a never ending service that run every (n) seconds on Native Modules (on this topic is android).
I've create a simple service like this
public void startTimer() {
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
Log.v(TAG, "SERVICE RUN");
try{
if(haveNetworkConnection()){
db.openDB();
if(db.countRowNewStructure()>0){
//send data to server
} else{
Log.v(TAG, "No data should be send to server");
}
} else {
Log.v(TAG, "Ga ada sinyal");
}
} catch (JSONException e){
e.printStackTrace();
}
}
}, 0, 1000);
}
above code run every second to check, but i'm facing the problem when i re-run the service from React Native, the log showing me those code every 0.5 seconds
the simulation may be like :
0----------------1 //seconds
startTimer()
re-run startTimer() conscious
0----------------1 //seconds
startTimer()
//now i have two startTimer() that will run every 1 sec on different interval
I want to keep my startTimer() just running once even I trigger startService() every 0.5 sec.
how can i do that? is it possible?
this may help you. Timmer to perform the certain action in android after the particular time.
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask timertaskforsynctime = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
#Override
public void run() {
// your action here
}
});
}
};
timer.schedule(timertaskforsynctime,5000,10000);// decide the time when it will start and after how much time it will run continusly.
}`
for one time
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
// your code that will run after 1 sec
}
}, 1000);
You could make use of the cancel method to cancel the previous Timer.
public class YourModule extends ReactContextBaseJavaModule {
Timer tim = new Timer();
public void startTimer() {
tim.cancel();
tim = new Timer();
tim.scheduleAtFixedRate(
new TimerTask() {
public void run() {
// Run tasks
}
},
0,
1000);
}
}
when I app start, start A timer.
Timer execute every 5 second.
Question is:
how to stop timer, when I start new Activity?
Here is what I have tried so far -
public boolean isDevDetect;
Timer timer = new Timer();
final TimerTask devTimertask = new TimerTask() {
#Override
public void run() {
if (//device detect) {
Intent intent = new Intent(A.this, B.class);
startActivity(intent);
isDevDetect = true;
}
else {
Log.d(TAG, "timer timer timer");
}
}
};
if (!isDevDetect) {
Log.d(TAG, "repetition timer");
devTimer.schedule(devTimertask, 3000, 5000);
} else if (isDevDetect) {
devTimertask.cancel();
devTimer.cancel();
Log.d(TAG, "stop timer");
}
this source when connected device. start B class.
but timer not stop.
how to when start activity, stop timer?
thanks.
public boolean isDevDetect;
Timer timer = new Timer();
final TimerTask devTimertask = new TimerTask() {
#Override
public void run() {
if (//device detect) {
Intent intent = new Intent(A.this, B.class);
startActivity(intent);
devTimertask.cancel();
devTimer.cancel();
}
else {
Log.d(TAG, "timer timer timer");
}
}
};
devTimer.schedule(devTimertask, 3000, 5000);
According to what you stated let me rewrite the problem statement , bascially you are trying to stop the timeTask as soon as the startActivity is called.The above code will suffice your problem statement.basically you need to stop timer when startActivity is called.Hope this solved your problem.
------------------- **
UPDATE
** -----------------
Please read the below blog , your Timertask is initialized .onRun() would be called only after devTimer.schedule(devTimertask, 3000, 5000); after which your Timertask is initialized .
http://android-er.blogspot.in/2013/12/example-of-using-timer-and-timertask-on.html
Hi every one can you help to create a 5 seconds timer that runs for every 3 or 5 seconds.. im new in android pls help me
i have only created a 5 seconds timer but it only run once.. i want it to run for every 5 seconds..
and here is my code :
txt1 = (TextView)findViewById(R.id.textView1);
final Handler handler = new Handler();
TimerTask task = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
// pos=rand.nextInt(10);
//txt1.setText(""+pos);
if(Integer.parseInt(txt1.getText().toString()) != 0)
{
txt1.setText("" + (Integer.parseInt(txt1.getText().toString()) - 1));
}
}
});
}
};
Timer timer = new Timer();
timer.schedule(task, 1000, 1000);
Thx :)
You can do it using below code, handler calls itself again and again.
final Handler handler = new Handler();
Runnable runable = new Runnable() {
#Override
public void run() {
try{
if(Integer.parseInt(txt1.getText().toString()) != 0)
{
txt1.setText("" + (Integer.parseInt(txt1.getText().toString()) - 1));
}
handler.postDelayed(this, 3000);
}
catch (Exception e) {
// handle exception
}
}
};
handler.postDelayed(runable, 5000);
How to disable the android service for few hours and restart it again ?
daybtn.setOnClickListener(new View.OnClickListener() {
//hide for 2 hr
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(c, "Service disabled for 5min", Toast.LENGTH_LONG).show();
Intent i = new Intent(context, Service.class);
context.stopService(i);
new Handler().postDelayed(new Runnable() {
public void run () {
// Do delayed stuff!
Intent intent2 = new Intent(c, Service.class);
c.startService(intent2);
}
}, 300000L); //5 min delay
}
});
The above code works for 5 min, but if i try to execute the same for 1hr or more(3600000L) then its not working. How can i achieve this for more than an hour or 2 ?. Thank you
Could anybody help me what is the best method if I need to control some things in Android device each 3 seconds, I think I have only 2 methods, these are AlarmManager or Service, but I read that everlasting Service or AlarmManager is deprecated.
So which method should I use?
You can add a Timer with an Asynctask. Here is a code for it:
//global variables:
Timer timer = new Timer();
final Handler pHandler = new Handler();
//onCreat:
TimerTask task = new TimerTask() {
#Override
public void run() {
pHandler.post(new Runnable() {
#Override
public void run() {
try {
// your code
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
};
timer.schedule(task, 0, 3000);