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
Related
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
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.
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);
I know there are many threads on Stackoverflow on this topic, and I have read each and every single one, but I am still very confused and am very sorry but I feel like I should post this.
When the application is run nothing really happens with the receivers, as if they are not receiving anything
My service:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
int timerValue = pv.getTimerValues();
aCounter = new CountDownTimer((timerValue), 1000) { // set to
// timerValue
// later
public void onTick(long millisUntilFinished) {
int minutes, seconds;
String sSeconds, sMinutes, toSend;
seconds = (int) (millisUntilFinished / 1000) % 60;
minutes = (int) ((millisUntilFinished / (1000 * 60)) % 60);
sSeconds = Integer.toString(seconds);
sMinutes = Integer.toString(minutes);
toSend = (sMinutes + ":" + sSeconds);
sendMessage(toSend);
countDownCheck();
}
public void onFinish() {
MediaPlayer dingeffect = MediaPlayer.create(
getApplicationContext(), R.raw.timerfinished);
// mPlayer0.stop();
try {
dingeffect.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dingeffect.start();
// Intent intent2 = new Intent(ServiceTest.this, Meditate.class);
//startActivity(intent2);
sendMessage("finished");
}
};
aCounter.start();
return START_NOT_STICKY;
}
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
private void sendMessage(String send) {
Intent intent1 = new Intent("countdowntimer");
// You can also include some extra data.
intent1.putExtra("message1", send);
sendBroadcast(intent1);
}
My receiving activity:
Inside the onCreate:
registerReceiver(mMessageReceiver1, new IntentFilter("countdowntimer"));
final Intent countDownService = new Intent(Meditate2.this,
ServiceTest.class);
startService(countDownService);
and my broadcastreceiver inside the same activity
private BroadcastReceiver mMessageReceiver1 = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra("message1");
setTimer(message);
}
};
In the manifest I have declared the service as such:
<service android:name=".ServiceTest"></service>
The weird thing is, it was working then after I fixed some other stuff this stopped working, so I really have no clue why it is not. Help please!
I will tell you what is different between your code and my (working) code.
In the activity onCreate:
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver1, new IntentFilter("countdowntimer"));
In the activity onDestroy:
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver1);
In sendMessage:
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
I don't know if this makes a lot of difference, but I used LocalBroadcastManager.getInstance.
Your broadcast receiver should know what intents to listen to.
Register your receiver with an intentFilter that listens to your countdowntimer intent like this:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("countdowntimer");
activity.registerReceiver(this, intentFilter);
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???