2011-07-05 37 views
0

在我的應用程序中,用戶將請求位置更新。發生這種情況時,主要活動將啓動將啓動位置請求的服務。服務和意向服務的位置問題

我的問題是,如果服務啓動,在兩分鐘內說3次,那麼它不能正常運行。

所以,基本上,我需要服務來啓動請求,直到當前啓動完成。

每次我的服務啓動時,它最多可能需要10分鐘才能完成,並且服務會根據傳遞給它的意向數據對每個啓動請求作出不同響應。

對於在兩分鐘內發生的三個啓動請求中的每一個都有一個位置請求(實際上更喜歡),但意圖數據可能與三個啓動請求中的每一個都不相同。

所以,我嘗試使用IntentService克服這一點,並處理所有三個請求一次一個,但隨後

LocationManager.requestLocationUpdates(...) 

不叫。我明白這可能是由於onHandleIntent(...)幾乎立即完成,基本上不給位置請求響應時間。我有一個處理程序在7分鐘後停止位置請求(3分鐘用於測試),然後位置信息被傳遞給將用戶界面更新返回給用戶的其他方法。 UI更新返回,它們只是空,因爲位置更新失敗。

我在想我可以在IntentService中使用onHandleIntent來啓動我的服務並將意圖數據傳遞給它,但這似乎是一個非常糟糕的做法,並且可能有更好的方法來做到這一點。 編輯:這絕對不會工作,因爲onHandleIntent將立即啓動我的locationService,所以不會有等待期。

此外,在onHandleIntent中調用的任何Toasts都不會顯示,儘管我的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中實例化();

+0

對於我的吐司問題,我發現http://stackoverflow.com/questions/3955410/create-toast-from-intentservice,但我並不真正關心吐司的事情太多,所以我沒有嘗試過。 – Jakar

回答

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 

EDITS:我以前使用的Array [字符串]代替ArrayList中,但沒有工作,因爲我必須要能夠持續增長我的數組,這不是大小可能。數組的大小是固定的,所以我使用了一個ArrayList,它現在可以工作。