Android开发者:有没有更好的方式获得timezon? [英] Android developers: is there a better way to get timezon?

查看:109
本文介绍了Android开发者:有没有更好的方式获得timezon?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在确切时间做出通知在后台工作,Android应用程序。



但是开放循环占用Battry的所有生命!...所以我'我正在寻找新的方式让它成为ryt,有更好的方式吗?



我尝试过:



  public   void  run() {
BckGround ,Toast.LENGTH_LONG)。show();
日历c =日历。 getInstance();
通知通知=新通知();
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();
小时= c.get(Calendar.HOUR_OF_DAY);
minites = c.get(MINUTE);
Min = M是: + minites;
hou = H是: +小时;

if ((hours == x)&&(minites == y)&&(f!= minites) ){
this .nb.setContentTitle( 字符串);
this .nb.setContentText( Another字符串);
this .mn.notify( 100 ,nb.build());
f = minites;
尝试 {
Thread.sleep(( long )Marroftime);
} catch (InterruptedException e){
e.printStackTrace();
}


}
其他 如果((小时== x2)&&(minites == y2)&&(f!= minites)){
this .nb.setContentTitle( time);
.nb.setContentText( now是时间);
this .mn.notify( 100 ,nb.build());
f = minites;
尝试 {
Thread.sleep(( long )Marroftime);
} catch (InterruptedException e){
e.printStackTrace();
}
}
其他 如果((小时== x3) )&&(minites == y3)&&(f!= minites)){
this .nb.setContentTitle( now);
.nb.setContentText( now是时间);
this .mn.notify( 100 ,nb.build());
f = minites;
尝试 {
Thread.sleep(( long )Marroftime);
} catch (InterruptedException e){
e.printStackTrace();
}
}
其他 如果((小时== x4) )&&(minites == y4)&&(f!= minites)){
this .nb.setContentTitle( String);
this .nb.setContentText( String );
this .mn.notify( 100 ,nb.build());
f = minites;
尝试 {
Thread.sleep(( long )Marroftime);
} catch (InterruptedException e){
e.printStackTrace();
}
}
其他 如果((小时== x5) )&&(minites == y5)&&(f!= minites)){
this .nb.setContentTitle( String);
this .nb.setContentText( String新的);
this .mn.notify( 100 ,nb.build());
f = minites;
尝试 {
Thread.sleep(( long )Marroftime);
} catch (InterruptedException e){
e.printStackTrace();
}
}

}
}


}

解决方案

建议:

- 使用系统计时器在下次通知时唤醒程序。

- 不要分开小时和分钟,它们是一天的时间。

- 当你为5次通知重复代码5次时,你知道出了什么问题。你会用20个通知做什么。

- 将所有通知存储在数组中。



每时每刻,你应该知道哪个通知是下一个,接下来只在发布时进行初始化测试。



在Java android中创建一个间隔计时器 - Stack Overflow [ ^ ]

Android计时器?怎么样? - Stack Overflow [ ^ ]

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:

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();
                }
            }

        }
    }


}

解决方案

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[^]


这篇关于Android开发者:有没有更好的方式获得timezon?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆