【问题标题】:Android Alarm Based On a Time Stored in Shared Preferences基于存储在共享首选项中的时间的 Android 警报
【发布时间】:2013-06-17 22:04:24
【问题描述】:

我正在构建一个 Android 应用程序,它每月通过短信发送一次 wifi 使用数据。到目前为止,我已经设法通过短信发送 wifi 数据使用量,通过共享首选项保存应用程序首次启动的日期,以便以后可以访问 - 现在我需要找到一种方法来使警报过期从我通过共享偏好保存之日起 30 天。这应该不会太难——除了每次设备启动时我都需要检查它是否已经过了 30 天——以补偿手机可能已经关闭的时间。

有人可以帮我完成这个吗?

源代码段:

//获取当前日期 日期 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);
    }

}

【问题讨论】:

    标签: java android alarmmanager android-alarms repeatingalarm


    【解决方案1】:

    您无需检查经过了多少时间。 AlarmManager.RTC_WAKEUP 以实际 UTC 时间测量,与设备正常运行时间无关。

    所以首先你调用 invokeAlarm 应该使用类似

    Calendar cal = Calendar.getInstance();
    cal.setTime(myDate);
    cal.add(Calendar.DATE, 30);
    invokeAlarm(cal.getTimeInMillis(), rowId);
    

    请注意,AlarmManager 在重新启动时会丢失所有警报。您需要为“Intent.ACTION_BOOT_COMPLETED”意图设置一个广播接收器服务。

    在此服务中,只需使用相同的逻辑再次设置警报。

    【讨论】:

    • 我在上面更新了我的源代码 - 我收到一条错误消息,指出“myDate 无法解析为变量”但是当我移动 Date myDate = new Date(prefs.getLong("time" , 0));就在您提供的来源上方 - 我收到一个新错误,指出“首选项”无法解决(有什么建议吗?)
    • 该块只是您在原始代码中定义了 myDate 之后可以在 onCreate 方法末尾执行的操作的一个示例。
    猜你喜欢
    • 1970-01-01
    • 2016-04-04
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    • 2011-09-03
    • 2012-09-20
    • 1970-01-01
    相关资源
    最近更新 更多