1

In my class file i am calling the below function to initiate my Service after one second, and the Service will repeat for every 24hrs.

 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);
    }

Below is my Service.here i am calculating the difference between the current date and the entered date(hardcoded). and i am saving the difference in shared preference. When the service is started for the first time the value is saved correctly. But after 24hrs the value is not updated.

EDIT : AlarmManager is working correctly. but i am having doubt in my service..the difference is getting calculated.i think the value is not saving to SharedPreferences. what might be the issue..?

Service

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);
        }
    }
}
sunil y
  • 343
  • 3
  • 9
  • 29
  • Try using `.commit()` instead of `.apply()`. I understand most people will tell you otherwise and before people start attacking me here, look at this: http://stackoverflow.com/a/13910783/3866010 I've seen this myself as well – ᴛʜᴇᴘᴀᴛᴇʟ Nov 04 '16 at 05:10
  • first time the data is saved correctly,then after 24hrs the value is not updated..so will that be done if i use `commit()`..? – sunil y Nov 04 '16 at 05:15
  • Then the problem is not with SharedPreferences , its with AlarmManager or Service, cross check for errors there! – OBX Nov 04 '16 at 05:18
  • @superman..AlarmManager is working correctly. but i am having doubt in my service..the difference is getting calculated.i think the value is not saving to SharedPreferences . – sunil y Nov 04 '16 at 05:22
  • you said "you think value is not saving"? Have you confirmed it? you can do `adb pull /data/data/my_package_names/files/filename.xml` command to pull the shared preference file and see the value yourself at anytime – ᴛʜᴇᴘᴀᴛᴇʟ Nov 04 '16 at 05:45
  • i mean after 24hrs i checked the value..it is showing "Not Available". – sunil y Nov 04 '16 at 05:51
  • I also agree, you should use editor.commit(); and then see the result – Nilabja Nov 04 '16 at 06:46

0 Answers0