1

I want my alarm(s) to resume/restart after the device is rebooted. After a lot of research on Google I found that it doesnt work without saving the alarms to the database. I have 2 questions:

2.How am I supposed to read the alarms from the database on reboot since the BOOT_COMPLETED intent isnt received by the broadcastreceiver.

1.How do I save the alarms to the database (How do I know that the DB is created and the values are inserted)?

EDIT:

I already have the necessary permission and receiver and this code: Tell me what to change in this code? MainActivity.java

 onClick:

int intHrs = SetResetIntervalActivity.intHours;
        int intMins = SetResetIntervalActivity.intMinutes;
        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.HOUR_OF_DAY, intHrs);
        calendar.set(Calendar.MINUTE, intMins);
        calendar.set(Calendar.SECOND, 0);

        Intent myIntent = new Intent(
                "com.sang.mobiledata.IntentAction.RECEIVE_RESETCONN_UPDATE");
        myIntent.putExtra("FLAG_KEY", false);
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, myIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) this
                .getSystemService(Context.ALARM_SERVICE);

        calendar.setTimeInMillis(System.currentTimeMillis());

        calendar.add(Calendar.SECOND, 10);
        long interval = intHrs * 3600000 + intMins * 60000;
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                calendar.getTimeInMillis(), interval, pi);
        long mins = interval / 60000;
        Toast.makeText(
                MainActivity.this,
                "Data Connection will be reset every " + mins
                        + " minute(s).", Toast.LENGTH_SHORT).show();

broadcast receiver:

 @Override
public void onReceive(Context context, Intent intent) {

   if (intent.getAction().equalsIgnoreCase(
            "android.intent.action.BOOT_COMPLETED")) {
 //control never enters here

        boolean blnVar=true;
        blnVar=false;
               ....

Manifest:

  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
 <receiver
        android:name="com.sang.mobiledata.ResetBroadcastReceiver"
        android:exported="true" >
        <intent-filter>
            <action android:name="com.sang.mobiledata.IntentAction.RECEIVE_RESETCONN_UPDATE" />
        </intent-filter>
        <intent-filter>
            <action android:name="com.sang.mobiledata.IntentAction.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
who-aditya-nawandar
  • 1,334
  • 9
  • 39
  • 89

2 Answers2

1

Save the values of hour,minute,seconds and id of alarm in sqlite database when you set Alarm at particular time.After reboot,start a Service from BroadCast and in onStart() method of that Service retrieve alarm values and again set alarm using pending intent.

You are welcome to ask if you have any further queries.

Jigar Pandya
  • 2,129
  • 2
  • 17
  • 27
  • I know it logically, its the implementation that I am struggling with. – who-aditya-nawandar Nov 19 '13 at 13:02
  • How to send it DB and how to retrieve is the question. – who-aditya-nawandar Nov 19 '13 at 13:05
  • Where are you facing problems? In resetting alarms after reboot?Plz tell where you are exactly striggling.. – Jigar Pandya Nov 19 '13 at 13:07
  • In the same way you are storing other values,insert hour,minute and second in sqlite database and get those values after boot is completed – Jigar Pandya Nov 19 '13 at 13:08
  • I am facing problems in inserting the values, receiving the BOOT_COMPLETED intent, reading the values and resetting the alarms. – who-aditya-nawandar Nov 19 '13 at 13:11
  • This is the first time I am working with SQLite and I dont know how to insert the values. I dont know how to check if the database is created or not. Currently I am getting the error "Unable to open database" – who-aditya-nawandar Nov 19 '13 at 13:13
  • Okay no worries plz check out these before going ahead in your app. http://chintankhetiya.wordpress.com/2013/06/01/sqlite-database-example/ http://karanbalkar.com/2013/07/tutorial-41-using-alarmmanager-and-broadcastreceiver-in-android/ – Jigar Pandya Nov 19 '13 at 13:14
  • Bro then you should not facing problems in using sqlite database and alarm manager. – Jigar Pandya Nov 19 '13 at 13:20
  • That code gives nothing about alarm management and receiving the BOOT_COMPLETED intent. – who-aditya-nawandar Nov 19 '13 at 13:28
  • -- add this code to your manifest,create Receiver and Service and now run,after boot is completed service will be called. – Jigar Pandya Nov 19 '13 at 13:31
  • I am getting a problem in my service in this line: PendingIntent pi = PendingIntent.getBroadcast(this, 0, myIntent, // PendingIntent.FLAG_UPDATE_CURRENT); since I am calling this servie from my MainActivity.java, the "this" (in pending intent) is not acceptable. – who-aditya-nawandar Nov 19 '13 at 13:56
0

Instead of saving Alarms to DB you can re-set the Alarms on Device boot as follows,

// BroadcastReceiver

public class StartApplication extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        // TODO Auto-generated method stub
        context.startService( new Intent ( context, AlarmService.class ) );
    }
}

// Alarm Class

public class AlarmService extends Service
{
    @Override
    public int onStartCommand ( Intent intent,int flags, int startId )
    {
        Intent alarmIntent = new Intent ( this,RepeaterService.class );
        PendingIntent pendingIntent = PendingIntent.getService( this, 0, alarmIntent, 0 );

        AlarmManager alarm = ( AlarmManager )getSystemService( Context.ALARM_SERVICE );
        alarm.setRepeating( AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 60*1000, pendingIntent );

        return Service.START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) 
    {
        // TODO Auto-generated method stub
        return null;
    }
}

You need to add following permission in AndroidManifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

and following way you can declare your BroadcastReceiver in AndroidManifest.xml ,

<receiver android:name="com.example.stackoverflow.StartApplication" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>
Vigbyor
  • 2,568
  • 4
  • 21
  • 35