实施每月重复警报

时间:2013-06-17 15:54:24

标签: java android alarmmanager android-alarms repeatingalarm

我有一个警报,我需要每隔30天执行一次。为了避免手机关闭电源的问题我已经决定这应该是基于日历的。我试图设计它的方式是打开应用程序时 - 它通过短信发送wifi数据。然后,根据首次运行的日期 - 安排警报从该日期起30天后再次运行。

出于测试目的,我已将30天缩短至1小时 - 发送(并收到)初始短信但是一小时后 - 没有任何反应 - 因此看起来重新报警是有问题的。

P.S。

我正在使用以下示例来构建它:

How to implement yearly and monthly repeating alarms?

但我在实施重新警报时遇到了一些麻烦。我认为我的问题在于示例中显示的部分:

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

这似乎为我从日历中获取的实际时间增加了时间,而不是添加时间来执行警报(感谢Lexandro帮助指出这一点。)但我仍然无法得到根据当前时间的指定小时数(或天数)执行警报。

来源:

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

}

}

2 个答案:

答案 0 :(得分:0)

在日历设置器区域中,您获取具有实际日期的实例,然后更改其值,但不是添加或减少某些时间。它的值与预期值完全不同,因为您设置的绝对值不是亲戚。

我刚从您的代码中提取日历更改并执行以演示:

Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getTime());
calendar.set(Calendar.DAY_OF_WEEK, 0);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 1);
calendar.set(Calendar.SECOND, 0);
System.out.println(calendar.getTime());
calendar.add(Calendar.HOUR, 1);
System.out.println(calendar.getTime());

结果是:

Mon Jun 17 18:00:56 CEST 2013
Sat Jun 22 00:01:00 CEST 2013
Sat Jun 22 01:01:00 CEST 2013

答案 1 :(得分:0)

来自Calendar API的getInstance()方法:

  
    

使用默认时区和区域设置获取日历。返回的日历基于默认时区的当前时间和默认语言环境。

  

因此无需设置任何字段,只需使用add方法将日历增加到您希望设置警报的任何时间。