我有关于重新安排任务的问题(有警报)我试图在未来的确切时间和日期设置任务然后关闭模拟器然后在等待弹出布局之后重新打开它当时间和日期到期时,我得到错误,表示无法在屏幕上打开(app / activity),同样在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>
答案 0 :(得分:1)
您需要在缺少的数据库连接上显式调用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
课程中;