简体   繁体   English

BOOT_COMPLETED 后无法启动意图

[英]Unable to start the intent after BOOT_COMPLETED

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

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

You need to explicitly call close on your database connection which is missing, so add it either at the end of getListSched function您需要在缺少的数据库连接上显式调用close ,因此将其添加到getListSched函数的末尾

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

or或者

if your yourSQLiteDatabaseObject instance is public then you can do this before looping如果您的yourSQLiteDatabaseObject实例是公开的,那么您可以在循环之前执行此操作

 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;注意: yourSQLiteDatabaseObject将是DatabaseSched类中SQLiteDatabase的对象,或者如果它不在DatabaseSched类中,则需要查找链;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM