Android IntentService中的循环

Android IntentService中的循环,android,android-intent,intentservice,Android,Android Intent,Intentservice,我的IntentService中有一个无限循环,根据主活动的输入,每30秒更新一次视图 public class IntentServiceTest extends IntentService { String Tag = "IntentServiceTest"; String ACTION_RCV_MESSAGE = "com.jsouptest8.intent.action.MESSAGE"; public IntentServiceTest(){ super("IntentSe

我的IntentService中有一个无限循环,根据主活动的输入,每30秒更新一次视图

public class IntentServiceTest extends IntentService {

String Tag = "IntentServiceTest";
String ACTION_RCV_MESSAGE = "com.jsouptest8.intent.action.MESSAGE";

public IntentServiceTest(){
    super("IntentServiceTest");
    Log.d(Tag, "IntentServiceTest constructor");
}

@Override
protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub
    Log.d(Tag, "in onHandleIntent");
    String url = intent.getStringExtra("URL");
    Document doc;
    int i=0;
    try{
      while(true){
         Log.d(Tag, "entered try block...");
         Log.d(Tag, "url = "+url);
         doc = Jsoup.connect(url)
         .get();

       Log.d(Tag, "past Jsoup.connect");
         Element data = doc.select("table").get(1).attr("bgcolor", "#f4f36f");
         Log.d(Tag, data.toString());
         Log.d(Tag, data.text());
         Log.d(Tag, "creating intent...");
         Intent broadcastIntent = new Intent();
         Log.d(Tag, "setting action...");
         broadcastIntent.setAction(ACTION_RCV_MESSAGE);
         broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
         broadcastIntent.putExtra("OUTPUT", data.toString());
         Log.d(Tag, "sending broadcast: "+(i++));
         sendBroadcast(broadcastIntent);
         Thread.sleep(30*1000);
      }
    }
    catch(StackOverflowError e){
        Log.d(Tag, "in StackOverflowError block...");
        Log.d(Tag, "creating intent...");
        Intent broadcastIntent = new Intent();
        Log.d(Tag, "setting action...");
        broadcastIntent.setAction(ACTION_RCV_MESSAGE);
        broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
        broadcastIntent.putExtra("OUTPUT", "系統忙線中, 請稍後再試");
        Log.d(Tag, "sending broadcast...");
        sendBroadcast(broadcastIntent);
    }
    catch(Exception e){
        Log.d(Tag, "in catch Exception block...");   
        onHandleIntent(intent);
    }
}

}
问题是,我被困在这个循环中。即使我杀死了主活动,然后返回到它以输入新的输入,并且IntentService仍然基于旧的输入返回


我需要知道如何每隔30秒从URL更新自己,而不被卡住。谢谢

在这个链接中,您会发现一个使用计时器进行自我更新的服务

如果您对while循环感到满意,只需编写一条存在于该循环中的If语句

if(thisIsTrue)
 {
   break; // this will exit the loop!
 }

最好在
main活动
本身中保留循环、计时器或任何此类正在运行的任务,并每次执行IntentService。因为
IntentService
每次都会执行任务并自行完成,或者
queue
会进一步交付任务

来自文档-

IntentService将接收意图,启动工作线程,然后 根据需要停止服务


它使用
工作队列处理器
模式来维护任务。

在IntentService或任何类型的服务中使用while语句都不是好主意。在IntentService内部,这是一个特别糟糕的想法,因为IntentService应该完成一项任务,然后自动终止,这实际上是在破坏使用IntentService的整个目的

我建议删除IntentService中的循环,并使用警报每30秒唤醒一次IntentService。这样,你的服务肯定会每30秒被调用一次,在它没有处理的时候,它实际上可以回到睡眠状态。此外,要处理在IntentService为旧请求提供服务时接收到对IntentService的新调用的情况,可以向IntentService的onStartCommand方法添加代码,以查看调用是否应排队处理或完全忽略

使用此方法设置报警:

public void setRepeating(int类型,长triggerAtMillis,长 间隔时间(分钟、挂起操作)

链接:

要获得更有效的方法,请使用setInexactRepeating(但这并不保证30秒的唤醒)


另外,我们通常不会覆盖IntentService的onStart命令,但如果您的应用程序确实具有该功能,则可以覆盖该命令。IntentService的
意味着完成任务并返回。它在新线程中执行此任务。不要在IntentService中使用while循环。您的
IntentService
将在一段时间后被杀死。我是根据个人经验说的。我试着用了一个while循环。在while循环结束时,我使用了
sleep(60000)
1分钟。但是我发现我的IntentService在一段时间后被杀了。

我建议您在30秒内不要使用AlarmManager,正如一些人暗示的那样。因为30秒太短了。它会耗尽电池电量。对于AlarmManager,使用至少1分钟的时间


如果您仍然希望时间为30秒,请使用服务。在服务中使用您的逻辑。但是要在一个单独的线程中完成这项工作,即在您的服务中生成一个新线程,并在那里使用while循环和
sleep()
。别忘了使用。这大大降低了android杀死你的服务的可能性

如何将此ISTRUE从主活动发送回IntentService?IntentService似乎只是停留在循环中,不再接受任何新的输入。我不想要一个持续运行的服务。当我输入一个新的查询输入,或者当我通过后退按钮终止活动时,我希望它停止。但是当我输入新参数时,IntentService根据旧参数返回。我能做什么?@Soham:每30秒使用一次alarm manager会耗尽电池电量。你有alarmmanager每30秒执行intentservice的教程吗@Ashwin或者如果我想让服务每12秒运行一次,您的解决方案是什么?如果您应该在每分钟运行一次AlarmManager和使用自己的循环线程的服务之间进行选择。。。你会选择什么?