0

在我的应用程序中,用户将请求位置更新。发生这种情况时,主要活动将启动一个服务,该服务将启动一个位置请求。

我的问题是,如果服务启动,比如在两分钟内启动 3 次,那么它就无法正常运行。

所以,基本上,我需要该服务暂停启动请求,直到当前启动完全完成。

每次启动我的服务时,最多可能需要十分钟才能完成,并且该服务会根据传递给它的意图数据以不同的方式响应每个启动请求。

我会很高兴(实际上更喜欢)在两分钟内发生的三个启动请求中的每一个都有一个位置请求,但是意图数据可能与三个启动请求中的每一个都不同。

所以,我尝试使用 IntentService 来克服这个问题并一次处理所有三个请求,但是随后

LocationManager.requestLocationUpdates(...)

不叫。我知道这可能是由于 onHandleIntent(...) 几乎立即完成并且基本上没有给位置请求时间做出响应。我有一个处理程序,它在 7 分钟后停止位置请求(3 分钟用于测试),然后位置信息被传递给其他将 UI 更新返回给用户的方法。UI 更新返回,它们只是 null 因为位置更新失败。

我想我可以在 IntentService 中使用 onHandleIntent 来启动我的服务并将意图数据传递给它,但这似乎是一种非常糟糕的做法,并且可能有更好的方法来做到这一点。 编辑:这绝对行不通,因为 onHandleIntent 无论如何都会立即启动我的 locationService,所以不会有等待期。

此外,在 onHandleIntent 中调用的任何 Toast 都不会显示,尽管我的 onCreate() 中的所有内容都可以正常工作。

我很难找到任何答案,任何帮助将不胜感激。这是我的代码的要点:

public class myService extends IntentService {

public findMyDroidService() { 
                super("findMyDroidService"); 
            }

@Override
 protected void onHandleIntent(Intent intent){

        intent.getExtras();
        fromIntentString = intent.getStringExtra("toIntentStringExtra");
        Toast.makeText(this, fromIntentString, Toast.LENGTH_SHORT).show();
        Toast.makeText(this, "Intent Handled", Toast.LENGTH_SHORT).show();
                    locMan.requestLocationUpdates(locListener,...);      
 }
@Override
public void onCreate(){
    super.onCreate();
     locListener = new LocationListener(){
              //code goes here
     }
}
}

此外,我的位置监听器在 onCreate(); 中实例化。

4

1 回答 1

0

我肯定是想多了。

在我的情况下,问题是我需要将所有数据分开,因此字符串 fromIntentString 不会被新的启动请求覆盖。我用一个数组来做到这一点,很容易。

public class myClass extends Service {
      int arrayInt;
      ArrayList<String> arrayStringList;
      int myInt;

      @Override
      public void onCreate() {
            super.onCreate()
      arrayStringList = new ArrayList<String>();
      myInt = 0;
  // additional code not important for this example
      }
@Override
public void onStart(...){
handleCommand(intent);
}
@Override
public void onStartCommand(...){
handleCommand(intent);
return START_STICKY;
}
//I call both onstart and onStartCommand for backwards compatibility
public void handleCommand(Intent intent){
   intent.getExtras();
   arrayStringList.add(intent.getStringExtra("fromIntentString"));
// so I know it worked
   Toast.makeText(this, arrayStringList.get(arrayInt).toString(), 
   Toast.LENGTH_SHORT).show();
//So I can keep track of the size of arrayStringList
   ++arrayInt;
//code for getting the location
useMyData();
}
public void useMyData(){
   // do location getting code
  // Here's where I actually use my array. For this answer, I will just show a toast.
Toast.makeText(getApplicationContext(), arrayStringList.get(myInt).toString(), 
Toast.LENGTH_SHORT).show();
    ++myInt;
    if (myInt < arrayInt) useMyData();
    //I was going to use a for loop, but I couldn't get it to work, and I like this method
    //better 

编辑:我以前使用 Array[String] 而不是 ArrayList,但这不起作用,因为我必须能够不断增加数组的大小,这是不可能的。数组有一个固定的大小,所以我使用了一个 ArrayList,它现在可以工作了。

于 2011-07-05T23:37:30.957 回答