我有问题。每隔一段时间,IntentServiceUpdateService02
就会运行失败。我把日志条目放进去,这样我就可以调试了,这就是我得到的......
02-28 21:37:32.461:主要 - 广播发送
02-28 21:37:32.484:BroadcastReceiver - 主启动;设置闹钟
02-28 21:37:32.539: BroadcastReceiver - 收到警报服务
02-28 21:38:32.500: BroadcastReceiver - 收到警报服务
通常这应该发生:
02-28 21:37:32.461:主要 - 广播发送
02-28 21:37:32.484:BroadcastReceiver - 主启动;设置闹钟
02-28 21:37:32.539: BroadcastReceiver - 收到警报服务
02-28 21:38:32.500:更新服务——onHandleIntent()
有任何想法吗?这是我的广播接收器代码...
广播接收器:
public class AlarmReceiver extends BroadcastReceiver {
private static final int INTERVAL = 60*1000; // check every 60 seconds
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction() != null) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.v(TAG, "BroadcastReceiver - Received Boot Completed; Set Alarm");
setRecurringAlarm(context);
}else if(intent.getAction().equalsIgnoreCase(Main.BROADCAST_STARTUP)){
Log.v(TAG, "BroadcastReceiver - Main Started; Set Alarm");
setRecurringAlarm(context);
}else{
Log.v(TAG, "BroadcastReceiver - Received " + intent.getAction());
}
}else{
Log.v(TAG, "BroadcastReceiver - Received AlarmService");
Intent i = new Intent(context, UpdateService02.class);
context.startService(i);
}
}
private void setRecurringAlarm(Context context) {
Intent receiver = new Intent(context, AlarmReceiver.class);
PendingIntent recurringDownload = PendingIntent.getBroadcast(context, 0, receiver, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarms = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarms.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), INTERVAL, recurringDownload);
}
}
意向服务:
public class UpdateService02 extends IntentService {
static DefaultHttpClient mClient = Client.getClient();
private static final int LIST_UPDATE_NOTIFICATION = 100;
public UpdateService02() {
super("UpdateService02");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.v(TAG, "UpdateService -- onHandleIntent()");
try {
HttpGet httpget = new HttpGet(url);
HttpResponse response;
response = mClient.execute(httpget);
BufferedReader in = new BufferedReader (new InputStreamReader(response.getEntity().getContent()));
Intent i = new Intent(BROADCAST_UPDATE);
i.putExtra("text", in.readLine().toString() + " updated");
sendBroadcast(i);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我认为可能我需要将 intentservice 启动的上下文设置为应用程序,但老实说我不知道。