实施每月重复报警

2023-09-04 03:54:10 作者:隔世亦相拥

我有我需要执行每30天报警。为了避免出现问题的手机被关机我已经decieded这应该是基于日历。我试图设计它是应用程序打开时的方式 - 它通过短信发送无线数据使用量。然后,根据日期,它是第一次运行 - 时间表报警从该日起再次运行30天。

有关测试的目的,我已经缩短了30天降低到一个小时 - 在inital短信发送(或接收),但一个小时后 - 没有任何反应 - 因此它出现在reoccuring报警是有问题的。

P.S。

我用下面的例子来构建这样的:

How实行年度和月度重复报警?

但我有一点麻烦实施的reocurring报警。我想我的问题就出在这个例子中的部分,它表明:

今天+1天

  //调度报警
    台历挂历= Calendar.getInstance();
    calendar.add(Calendar.DATE,1);
 

这似乎增加时间的实际时间,我从日历中,而不是将时间报警时,应执行得到(谢谢你Lexandro帮助指出这一点。),但我似乎仍不能得到报警要执行基于一个指定的从当前时间小时(天)量。

新 安全生产法 9月1日起已正式实施,安装可燃气体报警器刻不容缓

来源:

 公共类WifiMonitor延伸活动{

    按钮sendButton;

    的EditText msgTextField;

    私人PendingIntent pendingIntent;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        TextView中的InfoView =(TextView中)findViewById(R.id.traffic_info);

        双totalBytes =(双)TrafficStats.getTotalRxBytes()
                + TrafficStats.getTotalTxBytes();
        双mobileBytes = TrafficStats.getMobileRxBytes()
                + TrafficStats.getMobileTxBytes();
        totalBytes  -  = mobileBytes;
        totalBytes / = 1000000;
        mobileBytes / = 1000000;

        NumberFormat的NF =新的DecimalFormat(###);
        字符串totalStr = nf.format(totalBytes);
        字符串mobileStr = nf.format(mobileBytes);
        字符串信息=的String.Format(
                无线数据用法:%s的MB \的Tmobile数据用法:%s的MB,totalStr,
                mobileStr);
        infoView.setText(信息);

        SmsManager的SmsManager的= SmsManager.getDefault();
        smsManager.sendTextMessage(7862611848,空,信息,NULL,NULL);
        串报警= Context.ALARM_SERVICE;

        AlarmManager AM =(AlarmManager)getSystemService(Context.ALARM_SERVICE);
        台历挂历= Calendar.getInstance();
        日历= Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_WEEK,0);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,1);
        calendar.set(Calendar.SECOND,0);

        意图Aintent =新的意向书(REFRESH_THIS);
        PendingIntent圆周率= PendingIntent.getBroadcast(此,0,Aintent,0);
        am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY,PI);

        //重新安排再次检查明天
        意图serviceIntent =新的意图(WifiMonitor.this,Alarm.class);
        PendingIntent restartServiceIntent = PendingIntent.getService(
                WifiMonitor.this,0,serviceIntent,0);
        AlarmManager报警=(AlarmManager)getSystemService(ALARM_SERVICE);

        //取消previous报警
        alarms.cancel(restartServiceIntent);

        //调度报警再次运行
        calendar.add(Calendar.HOUR,1);

        //调度报警
        alarms.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),
                restartServiceIntent);




    }
}
 

报警:

 公共类报警延伸服务{

// compat的支持旧设备
@覆盖
公共无效ONSTART(意向意图,诠释startId){
    onStartCommand(意向,0,startId);
}

@覆盖
公众诠释onStartCommand(意向意图,诠释标志,诠释startId){

    //检查,以确保一切运行正常

    吐司面包= Toast.makeText(这一点,无线网络使用情况发送,2000年);
    toast.show();

    //发送短信
    字符串短信=;
    短信+ =(\ tWifi数据用法:
            +(TrafficStats.getTotalRxBytes()
                    + TrafficStats.getTotalTxBytes() - (TrafficStats
                    .getMobileRxBytes()+ TrafficStats.getMobileTxBytes()))
            /百万+MB);

    SmsManager的SmsManager的= SmsManager.getDefault();
    smsManager.sendTextMessage(7862611848,空,短信,NULL,NULL);

    返回START_STICKY;
}

@覆盖
公共无效的onCreate(){

    // TODO自动生成方法存根
}

@覆盖
公众的IBinder onBind(意向意图){

    // TODO自动生成方法存根

    返回null;

}

@覆盖
公共布尔onUnbind(意向意图){

    // TODO自动生成方法存根

    返回super.onUnbind(意向);

}

}
 

解决方案

从日历API的getInstance()方法:

       

使用默认的时区和语言环境的日历。返回的日历是基于当前时间的默认语言环境的默认时区。

  

所以无需设置任何字段,只需使用add方法来增加日历的任何时候你想要的报警进行设置。

I have an alarm which I need to execute every 30 days. To avoid issues with the phone being powered off I have decieded this should be calendar based. The way I'm attempting to design it is when the application is opened - it sends the wifi data usage via sms. Then, depending on date which it is first run - schedules the alarm to be run again 30 days from that date.

For testing purposes I have shortened the 30 days down to one hour - the inital sms is sent (and received) however an hour later - nothing happens - so it appears the reoccuring alarm is having issues.

P.S.

I'm using the following example to build this:

How to implement yearly and monthly repeating alarms?

But I'm having a bit of trouble getting the reocurring alarm implemented. I think my problem lies in the section of the example which shows:

// schedule alarm for today + 1 day
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, 1);

Which seems to add time to the actual time I'm getting from the calendar instead of adding time to when the alarm should be executed (Thank you Lexandro for helping to point this out.) but I still cannot seem to get the alarm to execute based upon a specified amount of hours (or days) from the current time.

SOURCE:

public class WifiMonitor extends Activity {

    Button sendButton;

    EditText msgTextField;

    private PendingIntent pendingIntent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView infoView = (TextView) findViewById(R.id.traffic_info);

        double totalBytes = (double) TrafficStats.getTotalRxBytes()
                + TrafficStats.getTotalTxBytes();
        double mobileBytes = TrafficStats.getMobileRxBytes()
                + TrafficStats.getMobileTxBytes();
        totalBytes -= mobileBytes;
        totalBytes /= 1000000;
        mobileBytes /= 1000000;

        NumberFormat nf = new DecimalFormat("#.##");
        String totalStr = nf.format(totalBytes);
        String mobileStr = nf.format(mobileBytes);
        String info = String.format(
                "Wifi Data Usage: %s MB\tMobile Data Usage: %s MB", totalStr,
                mobileStr);
        infoView.setText(info);

        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage("7862611848", null, info, null, null);
        String alarm = Context.ALARM_SERVICE;

        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance();
        calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_WEEK, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 1);
        calendar.set(Calendar.SECOND, 0);

        Intent Aintent = new Intent("REFRESH_THIS");
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, Aintent, 0);
        am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, pi);

        // reschedule to check again tomorrow
        Intent serviceIntent = new Intent(WifiMonitor.this, Alarm.class);
        PendingIntent restartServiceIntent = PendingIntent.getService(
                WifiMonitor.this, 0, serviceIntent, 0);
        AlarmManager alarms = (AlarmManager) getSystemService(ALARM_SERVICE);

        // cancel previous alarm
        alarms.cancel(restartServiceIntent);

        // schedule alarm to run again
        calendar.add(Calendar.HOUR, 1);

        // schedule the alarm
        alarms.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                restartServiceIntent);




    }
}

ALARM:

public class Alarm extends Service {

// compat to support older devices
@Override
public void onStart(Intent intent, int startId) {
    onStartCommand(intent, 0, startId);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    // check to ensure everything is functioning

    Toast toast = Toast.makeText(this, "WiFi Usage Sent", 2000);
    toast.show();

    // send SMS
    String sms = "";
    sms += ("\tWifi Data Usage: "
            + (TrafficStats.getTotalRxBytes()
                    + TrafficStats.getTotalTxBytes() - (TrafficStats
                    .getMobileRxBytes() + TrafficStats.getMobileTxBytes()))
            / 1000000 + " MB");

    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage("7862611848", null, sms, null, null);

    return START_STICKY;
}

@Override
public void onCreate() {

    // TODO Auto-generated method stub
}

@Override
public IBinder onBind(Intent intent) {

    // TODO Auto-generated method stub

    return null;

}

@Override
public boolean onUnbind(Intent intent) {

    // TODO Auto-generated method stub

    return super.onUnbind(intent);

}

}

解决方案

From the Calendar API for the getInstance() method:

Gets a calendar using the default time zone and locale. The Calendar returned is based on the current time in the default time zone with the default locale.

So no need to set any of the fields, just use the add method to increment the calendar to whatever time you want the alarm to be set for.

相关推荐