Android 从IntentService使用ContentProvider

Android 从IntentService使用ContentProvider,android,android-contentprovider,android-broadcast,android-intentservice,Android,Android Contentprovider,Android Broadcast,Android Intentservice,我在使用来自IntentService的ContentProvider时遇到问题 我计划在我的应用程序中使用IntentService来重新安排手机启动后的一些报警,但首先我需要从ContentProvider获取一些数据。据我所知,我可以通过注册一个BroadcastReceiver并从那里启动IntentService来实现这一点。我就是这么做的: onbootceiver.java public class OnBootReceiver extends BroadcastReceiver

我在使用来自IntentService的ContentProvider时遇到问题

我计划在我的应用程序中使用IntentService来重新安排手机启动后的一些报警,但首先我需要从ContentProvider获取一些数据。据我所知,我可以通过注册一个BroadcastReceiver并从那里启动IntentService来实现这一点。我就是这么做的:

onbootceiver.java

public class OnBootReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Intent scheduleServiceIntent = new Intent(context, ScheduleService.class);
    context.startService(scheduleServiceIntent);
}
ScheduleService.java

public class ScheduleService extends IntentService implements Loader.OnLoadCompleteListener<Cursor> {

private AlarmManager alarmManager;

@Override
public void onCreate() {
    super.onCreate();

    alarmManager = (AlarmManager) this.getApplicationContext().getSystemService(Context.ALARM_SERVICE);

}

@Override
protected void onHandleIntent(Intent intent) {
    String[] projection = new String[] {
    Contract._ID,
            Contract.START_TIME,
    Contract.DATE,
            Contract.NAME,
            Contract.TYPE};

    mCursorLoader = new CursorLoader(this, MyContentProvider.MyURI,
            projection, selection, selectionArgs, SOME_ID);
    mCursorLoader.registerListener(ID, this);
    mCursorLoader.startLoading();
}

@Override
public void onLoadComplete(Loader<Cursor> cursorLoader, Cursor cursor) {

    //pull data from the Cursor and set the alarms
}

@Override
public void onDestroy() {
    super.onDestroy();

    if (mCursorLoader != null) {
        mCursorLoader.unregisterListener(this);
        mCursorLoader.cancelLoad();
        mCursorLoader.stopLoading();
    }
}}
公共类ScheduleService扩展IntentService实现Loader.OnLoadCompleteListener{
私人报警管理器报警管理器;
@凌驾
public void onCreate(){
super.onCreate();
alarmManager=(alarmManager)this.getApplicationContext().getSystemService(Context.ALARM\u服务);
}
@凌驾
受保护的手部内容无效(意图){
字符串[]投影=新字符串[]{
合同.\u ID,
合同开始时间,
合同日期,
合同名称,
合同类型};
mCursorLoader=new CursorLoader(这个,MyContentProvider.MyURI,
投影、选择、选择、某些ID);
mCursorLoader.registerListener(ID,this);
mCursorLoader.startLoading();
}
@凌驾
public void onLoadComplete(加载器游标加载器,游标游标){
//从光标中提取数据并设置报警
}
@凌驾
公共空间{
super.ondestory();
如果(mCursorLoader!=null){
mCursorLoader.unregisterListener(此);
mcursorload.cancelLoad();
mCursorLoader.stopLoading();
}
}}
通过调试,我发现ScheduleServie.OnLoadComplete方法从未被调用。首先,调用onCreate方法,然后调用onHandleContent,最后调用onDestroy。我做错什么了吗?

根据文档:

要使用它,请扩展IntentService并实现onHandleIntent(Intent)。IntentService将接收意图,启动工作线程,并根据需要停止服务

这意味着只要
handleIntent()
完成,服务就会停止

由于
handleIntent()
已在后台线程中,因此应使用同步方法加载数据,而不是异步方法,如
CursorLoader
。确保在方法完成之前关闭由
query
返回的
光标

根据文件:

要使用它,请扩展IntentService并实现onHandleIntent(Intent)。IntentService将接收意图,启动工作线程,并根据需要停止服务

这意味着只要
handleIntent()
完成,服务就会停止

由于
handleIntent()
已在后台线程中,因此应使用同步方法加载数据,而不是异步方法,如
CursorLoader
。确保在方法完成之前关闭由
query
返回的
光标