我有一个警报,我需要每 30 天执行一次。为了避免手机关机的问题,我决定这应该是基于日历的。我尝试设计它的方式是在应用程序打开时 - 它通过短信发送 wifi 数据使用情况。然后,根据第一次运行的日期 - 安排警报从该日期起 30 天后再次运行。
出于测试目的,我已将 30 天缩短为一小时 - 发送(和接收)初始短信,但一小时后 - 没有任何反应 - 所以似乎再次出现的警报有问题。
附言
我正在使用以下示例来构建它:
但是我在实现重复警报时遇到了一些麻烦。我认为我的问题在于示例部分显示:
// 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);
}
}
警报:
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);
}
}