我正在构建一个 android 应用程序,它每月通过短信发送一次 wifi 使用数据。到目前为止,我已经设法通过短信发送了 wifi 数据使用量,通过共享首选项保存了应用程序首次启动的日期,以便以后可以访问它 - 现在我需要找到一种方法来使警报过期从我通过共享偏好保存之日起 30 天。这应该不会太难——除了每次设备启动时我需要检查它是否已经过了 30 天——以补偿手机可能已经关闭的时间。
有人可以帮我完成这个吗?
源代码段:
// 获取当前日期 Date date = new Date(System.currentTimeMillis());
// convert the date to milliseconds
long millis = date.getTime();
// save the date to shared preferences
SharedPreferences prefs = millis;
SharedPreferences.Editor editor = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
editor.putLong("time", date.getTime());
editor.commit();
// get the saved date
Date myDate = new Date(prefs.getLong("time", 0));
}
// set the alarm to expire 30 days from the date stored in sharePreferences (this portion is not functional - and it is what I need help with)
public void invokeAlarm(long invokeTime, long rowId) {
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, Alarm.class);
i.putExtra("rowId", String.valueOf(rowId));
am.set(AlarmManager.RTC_WAKEUP, invokeTime, PendingIntent.getService(
this, (int) System.currentTimeMillis(), i, 0));
完整来源:
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);
// get 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);
// send traffic info via sms
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("7865555555", null, info, null, null);
String alarm = Context.ALARM_SERVICE;
// get the current date
Date date = new Date(System.currentTimeMillis());
// convert the date to milliseconds
long millis = date.getTime();
// save the date to shared preferences
SharedPreferences prefs = millis;
SharedPreferences.Editor editor = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
editor.putLong("time", date.getTime());
editor.commit();
// get the saved date
Date myDate = new Date(prefs.getLong("time", 0));
}
// set the alarm to expire 30 days from the date stored in sharePreferences (this portion is not functional - and it is what I need help with)
public void invokeAlarm(long invokeTime, long rowId) {
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, Alarm.class);
i.putExtra("rowId", String.valueOf(rowId));
am.set(AlarmManager.RTC_WAKEUP, invokeTime, PendingIntent.getService(
this, (int) System.currentTimeMillis(), i, 0));
}
}
第一反应后更新来源:
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);
// get 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);
// send traffic info via sms
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("7862611848", null, info, null, null);
String alarm = Context.ALARM_SERVICE;
// get the current date
Date date = new Date(System.currentTimeMillis());
// convert the date to milliseconds
long millis = date.getTime();
// save the date to shared preferences
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
// SharedPreferences prefs = millis;
// SharedPreferences.Editor editor = PreferenceManager
// .getDefaultSharedPreferences(getApplicationContext());
editor.putLong("time", date.getTime());
editor.commit();
// get the saved date
Date myDate = new Date(prefs.getLong("time", 0));
}
// set the alarm to expire 30 days from the date stored in sharePreferences
public void invokeAlarm(long invokeTime, long rowId) {
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, Alarm.class);
i.putExtra("rowId", String.valueOf(rowId));
am.set(AlarmManager.RTC_WAKEUP, invokeTime, PendingIntent.getService(
this, (int) System.currentTimeMillis(), i, 0));
Calendar cal = Calendar.getInstance();
cal.setTime(myDate);
cal.add(Calendar.DATE, 30);
invokeAlarm(cal.getTimeInMillis(), rowId);
}
}