Give some delay after calling an Intent in Android - android

I want to add some delay after calling an intent from a service. Ex: as shown below
How can i do it?
Any Help would be appreciated.
if (yAccel > 500 || yAccel < -500)
{
gMeasure = true;
lockStatus = 1; //lock done;
Intent intent = new Intent(this, ScreenLock.class);
startActivity(intent);
//delay for min 10 min
}

Have you tried with an Handler?
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(this, ScreenLock.class);
startActivity(intent);
}
}, 1000);

Related

when the Intent startActivity, how to stop the timer on android?

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

Finish() or moveTaskToBack() doesn't work

I'm trying to put an activity in the background or simply kill it. I'm creating the activity inside a runnable, because I need to wait 2 seconds before it opens:
case TelephonyManager.CALL_STATE_RINGING:
long user_logged = AppSettings.getLoggedUserSQLiteID(context);
if (user_logged != 0) {
final Handler h = new Handler();
Runnable r1 = new Runnable() {
#Override
public void run() {
String phoneNr = inte.getStringExtra("incoming_number");
Intent phonecall = new Intent(ctx, PhoneCall.class);
phonecall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
phonecall.putExtra("phone_number", phoneNr);
ctx.startActivity(phonecall);
}
};
h.postDelayed(r1, 2000); // 1 second delay
}
break;
In the button I specify the method to run on click, like android:onClick="acceptCall":
public void acceptCall(View v) {
try {
Runtime.getRuntime().exec("input keyevent " + Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK));
} catch (IOException e) {
// Runtime.exec(String) had an I/O problem, try to fall back
String enforcedPerm = "android.permission.CALL_PRIVILEGED";
Intent btnDown = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(
Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_HEADSETHOOK));
Intent btnUp = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(
Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_HEADSETHOOK));
this.sendOrderedBroadcast(btnDown, enforcedPerm);
this.sendOrderedBroadcast(btnUp, enforcedPerm);
super.finish();
}
}
To finish, or put it in the background, I tried these lines of code:
super.finish();
super.moveTaskToBack(true);
this.finish();
this.moveTaskToBack(true);
However, none of them seems to work for me. Can anyone help me understand why?
In default case to kill activity, we should use:
MyActivity is your activity.
MyActivity.this.finish();
//try to kill your activity after this line of your code:
ctx.startActivity(phonecall);

Intent can't move to another activity using IF

I'm having a problem with my program. The process is when the client send a message,the server sends back a message, if the message is "OK", the client will move to the next activity but it didn't. This is my code:
OnClickListener SendOnClickListener = new OnClickListener() {
public void onClick(View v) {
thread.SendSocket(Message.getText().toString());
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// Actions to do after 3 seconds
if(Response.getText().toString().equals("OK")){
Intent i = new Intent(MainActivity.this,LOGIN.class);
startActivity(i);
}
Response.setText(thread.Socketdata );
}
}, 3000);
}
};
Change your if condition as
if(thread.Socketdata.toString().equals("OK")){
Intent i = new Intent(MainActivity.this,LOGIN.class);
startActivity(i);
}
and check.

Disable the Service for 3 hours in Android

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

Is it possible to send Intent after 5 minutes in android?

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???

Categories

Resources