赞
踩
谢谢你的回答 . 问题是应用程序上下文不可用 . 我找到了解决方案 . 我启动一个Alarmmanager并每15分钟发送一次意图 . 这个意图由我自己的接收器处理,该接收器在AndroidManifest.xml中声明 . 如果处理了意图,则应用程序上下文可用,我可以启动该服务 .
启动AlarmManager
Intent alarmIntent = new Intent(MyApp.getAppContext(),AlarmBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(MyApp.getAppContext(), 0, alarmIntent, 0);
AlarmManager alarmMgr = (AlarmManager)MyApp.getAppContext().getSystemService(Context.ALARM_SERVICE);
alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
AlarmManager.INTERVAL_FIFTEEN_MINUTES,
AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi);
接收方代码
package com.example;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AlarmBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context arg0, Intent arg1) {
}
}
在AndroidManifest.xml中声明Receiver
android:enabled="true"
android:name="com.example.AlarmBroadcastReceiver"
android:exported="false">
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。