簡體   English   中英

我在多個意圖上收到“未知服務啟動結果”錯誤

[英]I get “Unknown service start result” error on multiple intents

我試圖用我的活動發送的意圖來調用服務的某些功能。

這是我從活動(在UI線程中)發送意圖的方式:

Intent it = new Intent(MyService.INTENT_ACTIVITY_POSITION_CHANGED);
it.setClass(getApplicationContext(), MyService.class);
it.putExtra("posPercentX", x);
it.putExtra("posPercentY", y);
startService(it);

這是我的onStartCommand在MyService中的樣子:

    @Override
    public int onStartCommand (Intent intent, int flags, int startId){
       super.onStartCommand(intent, flags, startId);

       if(intent.getAction().equals(INTENT_ACTIVITY_START)){
        Toast.makeText(this,"onStartCommand ...", Toast.LENGTH_SHORT).show();
       }else if(intent.getAction().equals(INTENT_ACTIVITY_POSITION_CHANGED)){
        // here comes some code to get extras from intent
        Log.d("INTENT_ACTIVITY_POSITION_CHANGED", "x=" + posX + " y=" + posY);
        //TODO preveri
       }
       return startId;
    }

經過幾次電話,我得到:

ERROR/ActivityManager(52): java.lang.IllegalArgumentException: Unknown service start result: 4

為什么會出現此錯誤? 任何想法?

為什么您認為應該返回startId onStartCommand()的文檔是

intent提供給startService(Intent)的Intent。 如果服務在其進程消失后正在重新啟動,並且它先前已返回除START_STICKY_COMPATIBILITY以外的任何內容,則該值為null。

標志有關此啟動請求的其他數據。 當前為0,START_FLAG_REDELIVERY或START_FLAG_RETRY。

startId表示此特定啟動請求的唯一整數。 與stopSelfResult(int)一起使用。 返回

 * The return value indicates what semantics the system should use for 

服務的當前啟動狀態。 它可能是與START_CONTINUATION_MASK位關聯的常數之一

因此,您需要返回START_STICKY或http://developer.android.com/reference/android/app/Service.html#START_CONTINUATION_MASK中的值之一

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM