我有一个关于重新计划任务(带有警报)的问题,我试图在将来的确切时间和日期上设置任务,然后关闭模拟器,然后在等待弹出布局和时间后重新打开它由于日期错误,提示我无法打开屏幕上的(应用程序/活动),而且在logcat中也看不到错误(可能是应用程序已关闭),所以我不确定在哪里我确实做错了什么。

这是我用于服务的代码

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


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 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>


这里
java - BOOT_COMPLETED后无法启动意图-LMLPHP

最佳答案

您需要在缺少的数据库连接上显式调用close,因此请在getListSched函数的末尾添加它

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


要么

如果您的yourSQLiteDatabaseObject实例是公共实例,则可以在循环之前执行此操作

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


注意:yourSQLiteDatabaseObject将是SQLiteDatabase类中DatabaseSched的对象,或者如果它不在DatabaseSched类中,则需要查找链。

07-26 08:48