samedi 26 mars 2016

How to access SQLite database in a service that gets called with no instance and right after boot

I'm making a reminder app and with the help of BroadcastReceiver. Note setAlarm() is called to set an alarm. The onReceive of my BroadcastReceiver checks the intent if the intern's action equals to boot completed if so It starts an intent to my Service class where I would want to access my Database and restart my alarms, but don't know how. So how can I accomplish this? My goal is to to create persistent alarms even after rebooting

My BroadcastReceiver class:

 public class AlarmManagerBroadcastReceiver extends BroadcastReceiver{

private static final String TAG = AlarmManagerBroadcastReceiver.class.getSimpleName();

private static Context mContext;
// Database
private static MyDBHandler mMyDBHandler;


public AlarmManagerBroadcastReceiver(){}

public AlarmManagerBroadcastReceiver(Context context){
    this.mContext = context;
    // Database only gets used when user sets reminder (gets initialized)
    mMyDBHandler = new MyDBHandler(context, "", null, 0);
}

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


    if (mMyDBHandler == null){
        mMyDBHandler = new MyDBHandler(context, "", null, 0);
    }



    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
        // Starting the Service / Handles asynchronous requests on demand
        Intent serviceIntent = new Intent(context, AlarmService.class);
        context.startService(serviceIntent);





    } else {

        int id = intent.getIntExtra("id", 0);
        String title = intent.getStringExtra("title");


        // Alarm got called
        mMyDBHandler.updateAlertTime(0, id);


        // CreateNotification() / needs onReceive()'s context
        createNotification(context, "", title, id);
    }
}

// HOW I SET MY ALARMS 
public static void setAlarm(int id, long alertTime){

    Toast.makeText(mContext,alertTime + "", Toast.LENGTH_SHORT).show();

    Movie movie = mMyDBHandler.getByID(id);

    // if DB finds the video game we're talking about
    if (movie != null) {
        // Make sure to set long in PageActivity for the game

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

        // onReceive()
        Intent intent = new Intent(mContext, AlarmManagerBroadcastReceiver.class);
        intent.putExtra("id", id);
        intent.putExtra("title", movie.getTitle());

        // Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent.
        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, pendingIntent);

    } else {
        Toast.makeText(mContext, "Error setting reminder", Toast.LENGTH_SHORT).show();
    }

}
}

My Service class:

public class AlarmService extends IntentService {

private MyDBHandler mMyDBHandler;
private static final String TAG = AlarmManagerBroadcastReceiver.class.getSimpleName();

public AlarmService() {
    super("AlarmService");

}

@Override
protected void onHandleIntent(Intent intent) {

    try {
        mMyDBHandler = new MyDBHandler(getApplication(), "", null, 0);
        initializeAlarms();
    } catch (Exception e){
        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
    }
}

public void initializeAlarms(){
    ArrayList<Movie> moviesList = mMyDBHandler.getAllHyped();
    for (Movie movie: moviesList){
        int ID = movie.getId();
        AlarmManagerBroadcastReceiver.setAlarm(ID , mMyDBHandler.getReminderTime(ID));
    }
}
}

Note: in my re initialize alarms method I call setAlarm from my BroadcastReceiver class

Thanks!

Aucun commentaire:

Enregistrer un commentaire