0

我在线上遇到错误:

cal.setTime(myDate);

声明“myDate 无法解析为变量”,但是当我尝试移动它引用的行时:

Date myDate = new Date(prefs.getLong("time", 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);
    }

}
4

2 回答 2

2

您必须声明Date myDate为该类的成员。

     private Date myDate;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            /**/
            myDate = new Date(prefs.getLong("time", 0));
         }

public void invokeAlarm(long invokeTime, long rowId) {
        /**/
        Calendar cal = Calendar.getInstance();
        if(myDate != null)
           cal.setTime(myDate);
        cal.add(Calendar.DATE, 30);
        invokeAlarm(cal.getTimeInMillis(), rowId);
    }

或者,如果您直接从 sharedPrefs 获取日期:

public void invokeAlarm(long invokeTime, long rowId) {
        /**/

        SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(getApplicationContext());
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(prefs.getLong("time", 0));
        cal.add(Calendar.DATE, 30);
        invokeAlarm(cal.getTimeInMillis(), rowId);
    }
于 2013-06-18T14:13:38.407 回答
0

您在一个函数(函数的本地范围)中声明了 myDate,并试图在另一个函数(不同的范围)中使用它。

为了能够做到这一点,而无需进行重大更改,请在类级别声明 myDate,在一个函数中分配它,在另一个函数中使用它。一定要换行:

Date myDate = new Date(prefs.getLong("time", 0));

至:

myDate = new Date(prefs.getLong("time", 0));

当你这样做的时候。

于 2013-06-18T14:19:04.463 回答