簡體   English   中英

基於共享首選項中存儲的時間的Android警報

[英]Android Alarm Based On a Time Stored in Shared Preferences

我正在構建一個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);
    }

}

您無需檢查已花費了多少時間。 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”意圖設置作為廣播接收器的服務。

在此服務中,只需使用相同的邏輯再次設置警報即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM