1

i am having an application where i need to schedule an alarm for every fifteen minute this is the code

public void scheduleAlarm() {
    // Construct an intent that will execute the AlarmReceiver
    Intent intent = new Intent(getApplicationContext(), MyAlarmReceiver.class);
    // Create a PendingIntent to be triggered when the alarm goes off
    final PendingIntent pIntent = PendingIntent.getBroadcast(this, MyAlarmReceiver.REQUEST_CODE,
        intent, PendingIntent.FLAG_UPDATE_CURRENT);
    // Setup periodic alarm every 5 seconds
    long firstMillis = System.currentTimeMillis(); // alarm is set right away
    AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    // First parameter is the type: ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC_WAKEUP
    // Interval can be INTERVAL_FIFTEEN_MINUTES, INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_DAY
    alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis,
        AlarmManager.INTERVAL_HALF_HOUR, pIntent);
  }
}

and i will call this method in the onCreate method of my mainActivity, my problem is i am not getting how to restrict the method being called only once since the onCreate method will be called everytime the activity is created. If i use a sharedpreference to store whether the method is called or not, what to do if the user clear the appdata from settings.

Peter Badida
  • 11,310
  • 10
  • 44
  • 90
Shaan_B
  • 1,788
  • 2
  • 12
  • 16
  • 1
    even google cant help if the user cleared the app data; hope u got the answer :) ; have fun coding – DJphy Apr 06 '16 at 07:20
  • @Shantanu Hi just check with this questions. http://stackoverflow.com/questions/24462279/alarm-manager-service-starting-every-oncreate. – Rameshbabu Apr 06 '16 at 07:23
  • then store the info in local database – Ranjithkumar Apr 06 '16 at 07:23
  • there is no sure way to get rid of this. if the user clears the prefs, it will fire again. also a database can be cleared by a root user, but it decreases the possible users. – Opiatefuchs Apr 06 '16 at 07:26
  • if you REALLY want to execute code for just once per user, you can pull counter from online server (with login) and use this to trigger code only once. – Dexter Apr 06 '16 at 11:25
  • @Dexter i did not get what you mean by a counter, do you mean to use a get/post method to set and reset a flag at the server. thanks – Shaan_B Apr 06 '16 at 11:47
  • @Shaan_B , yes. If it is very important, you can write code in such way that it will work only when you get flag from online server. You just change flag after code execution. – Dexter Apr 06 '16 at 12:26

1 Answers1

1

Create one Prefrence and set flag in to it.

SharedPreferences sharedPreferences = getSharedPreferences("MyPref", Context.MODE_PRIVATE);
                                SharedPreferences.Editor editor = sharedPreferences.edit();
                                editor.putBoolean("isFlag", false);
                                editor.commit();

Before call this method check he flag, if it is true then your method will called.

SharedPreferences sharedPreferences = getSharedPreferences("MyPref", Context.MODE_PRIVATE);
if (sharedPreferences.getBoolean("isFlag", true)) {
    scheduleAlarm();
}

I hope it may help you...

Ronak Selarka
  • 663
  • 1
  • 5
  • 13
  • You can create your own Activity like **RestrictClearData.java** And in this Activity set an Error message like.."You dont have an permition to clear app data" just add single line in to your Application tag in manifest file. **android:manageSpaceActivity=".RestrictClearData"** – Ronak Selarka Apr 06 '16 at 10:12
  • Instead of **Clear Data** button **Manage Space** Button will appeared. if user click on this then Restrict user Acticity will call. I hope it may helps you.. :) – Ronak Selarka Apr 06 '16 at 10:13
  • Okey.. If you asked this question then you can accept my answer. also you can increase your reputation to accepting my answer. – Ronak Selarka Apr 07 '16 at 10:56