gpt4 book ai didi

android - IntentService 在某些情况下无法运行

转载 作者:行者123 更新时间:2023-11-30 04:19:25 27 4
gpt4 key购买 nike

我遇到了一个问题。 IntentService UpdateService02 偶尔会运行失败。我放入了日志条目以便进行调试,这就是我得到的...

02-28 21:37:32.461: Main - Broadcast Sent

02-28 21:37:32.484: BroadcastReceiver - Main Started; Set Alarm

02-28 21:37:32.539: BroadcastReceiver - Received AlarmService

02-28 21:38:32.500: BroadcastReceiver - Received AlarmService

通常这应该发生:

02-28 21:37:32.461: Main - Broadcast Sent

02-28 21:37:32.484: BroadcastReceiver - Main Started; Set Alarm

02-28 21:37:32.539: BroadcastReceiver - Received AlarmService

02-28 21:38:32.500: UpdateService -- 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);
}
}

Intent 服务:

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 启动的上下文设置为应用程序,但老实说我不知道​​。

最佳答案

我想通了问题...

我改变了这个:

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

为此:

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);
in.close();
} catch (ClientProtocolException e) {

有时读取器会“卡住”,下次调用它时仍会卡住,尝试处理上一个请求。添加 in.close(); 确保我在每次使用后关闭它。现在效果很好。

关于android - IntentService 在某些情况下无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9493725/

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