-12

I wanna do just like Alarm in time , but as I said in the question I wanna make smth like this :( if time " now " = 5:50 pm) do // Something i tried this but it's take all my battery life time : what I've Done is Set int Hours and int minutes and then

enter code here
while (true){
if (Hours==x &minutes==y )
//do somethin

}
so any better way ?
AbdoHurbli
  • 11
  • 1
  • 6

1 Answers1

0

Never do a while(true){} loop, is takes to much memory.

A thing you could do is use a Handler. It will call Runnable you gave him after the delay you want :

 Handler handler = new Handler();
Runnable r = new Runnable() {
    public void run() {
        //do your stuff here, even start a new handler
  }
};
handler.postDelayed(r, 1000);

You can launch him every minut for exemple and check if it is the good time or not, or every hour, just calcul when you want him to wake up.

Phoenix
  • 135
  • 3
  • 9
  • U mean handler Will launch the Activity In specific time ! ? Need it in background I use service to work in background .. my Activity work find but as u said it take memory and Battery sooo much so looking for better way Can u help ?\ – AbdoHurbli Jul 21 '16 at 15:58
  • It will depend what you tell the handler to do and if you app is on bg or not.. – Phoenix Jul 21 '16 at 15:59
  • ok can u give me an example how to make the handler give toast in specific time ? – AbdoHurbli Jul 21 '16 at 16:07
  • Just make your runnable show a toast, something like `Toast.makeText(getActivity(), yourString, Toast.LENGTH_LONG).show();` – Phoenix Jul 21 '16 at 16:10
  • The Problem is ..that If i use if () in run() it will give the Toast when i run the App in that time I specified but if not .. will do nth ... that y I use wihle(true ).... – AbdoHurbli Jul 21 '16 at 16:13
  • Did you check the alarm manager? – Phoenix Jul 21 '16 at 16:15
  • As chol said, here http://stackoverflow.com/questions/17885198/repeat-alarm-everyday-at-specific-time-alarm-manager – Phoenix Jul 21 '16 at 16:16
  • So I should use : Calendar c = Calendar.getInstance(); int h = c.set(Hours,18); int m = c.set(Minute,30) in Hours = 18; int Minuts=30; now how should make the Toast appear in that time (Hours,Minuts)? – AbdoHurbli Jul 21 '16 at 16:20