1

I have a problem regarding about re-scheduling the tasks (with alarm) I tried to set the task on exact time and date in the future then I closed the emulator then re-open it after that waiting to popup the layout and when the time and date is on due I got error which says unable to open the (app/activity) on the screen, also in the logcat I can't see the errors (which probably the app is closed), So I'm not sure where exactly I did something wrong.

Here is the code I used for the service

public class ChibiReminderService extends WakefulIntentService {
 Date date;
 private long milliseconds = 0;
 public ChibiReminderService() {
        super("ChibiReminderService");`
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        DatabaseSched db = DatabaseSched.getInstance(this); //get access to the instance of DatabaseSched
        sched_lists alarm = new sched_lists();

        List<DatabaseSource> tasks = db.getListSched(); //Get a list of all the tasks there
        for (DatabaseSource task : tasks) {
            // Cancel existing alarm
            alarm.cancelAlarm(this, task.getId());
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm a");

             try {
                  date = format.parse(task.getDueDateTime());
                 milliseconds = date.getTime();
             } catch (Exception e) {
                 e.printStackTrace();
             }

            //regular alarms
            if(milliseconds >= System.currentTimeMillis()){
                alarm.setAlarm(this, task.getId());
            }
        }
        super.onHandleIntent(intent);
    }
}

The OnBootReceiver

public class OnBootReceiver extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    WakefulIntentService.acquireStaticLock(context); //acquire a partial WakeLock
    context.startService(new Intent(context, ChibiReminderService.class)); //start ChibiReminderService
 }

}

Manifest

  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.majimechibireminder2"
    android:versionCode="1"
    android:versionName="1.0" android:installLocation="preferExternal">

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" android:persistent="true">
      <receiver android:name=".OnBootReceiver" >
         <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
         </intent-filter>
     </receiver>

      <receiver android:name=".AlarmReceiver"></receiver>
       <service android:name=".ChibiReminderService" >
     </service>

Here

1 Answers1

1

You need to explicitly call close on your database connection which is missing, so add it either at the end of getListSched function

List<DatabaseSource> getListSched(){
.. 
..// your code 
     yourSQLiteDatabaseObject.close();
}

or

if your yourSQLiteDatabaseObject instance is public then you can do this before looping

 List<DatabaseSource> tasks = db.getListSched();
 db.yourSQLiteDatabaseObject.close();
        for (DatabaseSource task : tasks) {

Note: yourSQLiteDatabaseObject will be a object of SQLiteDatabase inside your DatabaseSched class or you need to look for the chain if it's not in DatabaseSched class;

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68