如何在没有实例且在引导后立即调用的服务中访问SQLite数据库

时间:2016-03-27 01:07:43

标签: java android sqlite broadcastreceiver intentservice

我正在制作一个提醒应用,并在BroadcastReceiver的帮助下。 注意调用setAlarm()来设置警报。我的BroadcastReceiver的onReceive检查意图,如果实习生的动作等于启动完成,如果是这样它启动我的服务类的意图,我想访问我的数据库并重新启动我的警报,但不知道如何。那我该怎么做呢?我的目标是即使在重新启动后也能创建持久警报

我的BroadcastReceiver类:

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

}
}

我的服务类:

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

注意:在我的初始化报警方法中,我从BroadcastReceiver类中调用setAlarm

谢谢!

0 个答案:

没有答案