gpt4 book ai didi

java - BOOT_COMPLETED 后无法启动 Intent

转载 作者:行者123 更新时间:2023-11-30 00:59:26 25 4
gpt4 key购买 nike

我有一个关于重新安排任务的问题(带警报)我试图在未来的确切时间和日期设置任务然后我关闭了模拟器然后在等待弹出布局之后重新打开它当时间和日期到期时,我收到错误消息,提示无法在屏幕上打开(应用程序/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
}

}

list

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

在这里

最佳答案

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

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

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

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

注意:yourSQLiteDatabaseObject 将是 DatabaseSched 类中 SQLiteDatabase 的对象,如果链不在 中,则需要查找链code>DatabaseSched类;

关于java - BOOT_COMPLETED 后无法启动 Intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39660064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com