Click here to Skip to main content
15,897,315 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I wanna make Notification in Exact time "Working in background",Android App.

but that open Loop Take all the Battry lifetime !.. So I'm looking for New Way To make it ryt ,is there Better Way ?

What I have tried:

Java
public void run() {
       BckGround",Toast.LENGTH_LONG).show();
        Calendar c = Calendar.getInstance();
        Notification notification=new Notification();
      notification.sound=      Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE+"://"+getPackageName()+"/row/a zan");
       // Toast.makeText(null, "start",Toast.LENGTH_LONG).show();
        int sec = c.get(Calendar.SECOND);
        int hours=  c.get(Calendar.HOUR_OF_DAY);
        int minites=c.get(MINUTE);

        int x,y,x2,y2,f,x3,y3,x4,x5,y4,y5;
        String Min,hou;
        x=19;y=1;
        x2=16;y2=1;
        x3=18;y3=1;
        x4=20;y4=1;
        x5=22;y5=1;
        f=0;
        //التاري
         int d,mo;
        d=18;
        mo=7;
        double Marroftime=720000;
        Calendar cn= Calendar.getInstance();
      int  day=cn.get(Calendar.DAY_OF_WEEK);
        int month=cn.get(Calendar.MONTH);
        while   (true) {

            c = Calendar.getInstance();
            hours = c.get(Calendar.HOUR_OF_DAY);
            minites = c.get(MINUTE);
            Min="M is: "+ minites;
            hou="H is: "+hours;

            if ((hours==x) && (minites==y)&&(f!=minites)){
                this.nb.setContentTitle("string");
                this.nb.setContentText("Another String");
                this.mn.notify(100, nb.build());
                f=minites;
                try {
                    Thread.sleep((long) Marroftime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }


            }
          else  if ((hours==x2) && (minites==y2)&&(f!=minites)){
                this.nb.setContentTitle("time");
                this.nb.setContentText("now is time");
                this.mn.notify(100, nb.build());
                f=minites;
                try {
                    Thread.sleep((long) Marroftime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
          else  if ((hours==x3) && (minites==y3)&&(f!=minites)){
                this.nb.setContentTitle("now ");
                this.nb.setContentText("now is time");
                this.mn.notify(100, nb.build());
                f=minites;
                try {
                    Thread.sleep((long) Marroftime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
         else   if ((hours==x4) && (minites==y4)&&(f!=minites)){
                this.nb.setContentTitle("String");
                this.nb.setContentText("String");
                this.mn.notify(100, nb.build());
                f=minites;
                try {
                    Thread.sleep((long) Marroftime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
         else   if ((hours==x5) && (minites==y5)&&(f!=minites)){
                this.nb.setContentTitle("String");
                this.nb.setContentText("String NEw");
                this.mn.notify(100, nb.build());
                f=minites;
                try {
                    Thread.sleep((long) Marroftime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }


}
Posted
Updated 21-Jul-16 1:12am

1 solution

Advice:
- Use system timer to wake up the program on next notification.
- Do not separate hours and minutes, together they are the time od the day.
- You know something is wrong when you repeat code 5 times for 5 notifications. What would you do with 20 notifications.
- Store all notifications in an array.

At every moment, you should know which notification is the next one, and next to test only at launch time to initialize.

Making a interval timer in Java android - Stack Overflow[^]
Android timer? How? - Stack Overflow[^]
 
Share this answer
 
v2
Comments
Member 12509947 21-Jul-16 7:17am    
thanks For answer ...
ok let's Start with System Timer .. Can u give me an example to use system Timer?
u see I use the open while loop to get the time .."when the app is close"(Background work)
system timer will help ?
2- what if i need to separate the Hours and minutes ... I need the Notification is Specific Hour and minutes ?
3-please hlep to make good array ?
I'm just started Developing android Apps .. so I'm looking for ur help ...
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900