共享首选项值不在服务中更新

时间:2016-11-04 05:02:20

标签: android android-service sharedpreferences

在我的班级文件中,我呼叫以下功能,在一秒钟后启动我的服务,服务将每隔24小时重复一次。

 private void showFileChooser1() {

        Runnable mRunnable;
        Handler mHandler = new Handler();
        mRunnable = new Runnable() {
            @Override
            public void run() {

                SharedPreferences sharedPreferences1 = Mnst.this.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);

                SharedPreferences.Editor editor = sharedPreferences1.edit();

                //Adding values to editor
                editor.putBoolean(LOGGEDIN_SHARED_PREF, true);
                editor.putString(MYlAST_SHARED_PREF, txtDate.getText().toString());
                editor.putString(NORMALCYCLE_SHARED_PREF, txtnormal.getText().toString());
                editor.apply();

                AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                Intent serviceIntent = new Intent(Mnst.this, MyService.class);
                pi = PendingIntent.getService(Mnst.this, 0, serviceIntent, 0);
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
                        START_AFTER_SECONDS * 1000, pi); // MyService.class will repeat for every 24hrs. START_AFTER_SECONDS = 24*3600;

            }
        };
        /**on checking the check box,the service will initiate in 1sec,then the service will be triggered every 24hrs bcz of
        START_AFTER_SECONDS. INITIAL_START_AFTER_SECOND = 1*
         */
        mHandler.postDelayed(mRunnable, INITIAL_START_AFTER_SECONDS * 1000);
    }

以下是我的服务。我正在计算当前日期和输入日期之间的差异(硬编码)。我正在保存共享偏好的差异。第一次启动服务时,正确保存该值。但是在24小时之后,该值不会更新。

编辑:AlarmManager正常运行。但我怀疑我的服务......差异是计算。我认为价值不是保存到SharedPreferences。可能是什么问题..?

服务

public class MyService extends Service
{

    String dayDifference;
    String mylast;
    String normalcycle;

    public static final String SHARED_PREF_NAME = "myloginapp";
    public static final String DAYSLEFT_SHARED_PREF = "daysleft";

    int dt;

    NotificationManager manager;
    Notification myNotication;

    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }//End of onBind method

    public void onStart(Intent intent, int startId)
    {
        super.onStart(intent, startId);
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        try {

            SharedPreferences sharedPreferences = getSharedPreferences(Mnst.SHARED_PREF_NAME, MODE_PRIVATE);
            mylast = sharedPreferences.getString(Mnst.MYlAST_SHARED_PREF,"Not Available");
            normalcycle = sharedPreferences.getString(Mnst.NORMALCYCLE_SHARED_PREF,"Not Available");

            try {
                dt = Integer.parseInt(normalcycle);
            } catch(NumberFormatException nfe) {

            }

            SimpleDateFormat dates1 = new SimpleDateFormat("dd-MM-yyyy");
            Calendar c = Calendar.getInstance();
            try {
                c.setTime(dates1.parse(mylast));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            c.add(Calendar.DATE, dt);

            SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");
            String output = sdf1.format(c.getTime());

            //Dates to compare
            String CurrentDate;
            //String FinalDate = "08/24/2016";

            Date date1 = new Date();
            date1.setTime(System.currentTimeMillis());

            Date date2;

            SimpleDateFormat dates = new SimpleDateFormat("dd-MM-yyyy");

            //Setting dates

            CurrentDate = dates.format(date1);
            date2 = dates.parse(output);
            date1 = dates.parse(CurrentDate);

            //Comparing dates
            long difference = Math.abs(date2.getTime() - date1.getTime());
            long differenceDates = difference / (24 * 60 * 60 * 1000);

            //Convert long to String
            dayDifference = Long.toString(differenceDates);

            Log.e("HERE.................", "HERE: " + dayDifference);
            System.out.println("..............difference date " + dayDifference);


            SharedPreferences sharedPreferences1 = getApplicationContext().getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences1.edit();
//here i am saving the difference in sharedpref.
            editor.putString(DAYSLEFT_SHARED_PREF, dayDifference);
            editor.apply();


        } catch (Exception exception) {
            Log.e("DIDN'T WORK............", "exception " + exception);
        }
    }
}

0 个答案:

没有答案