2015-07-10 743 views
2

我試圖在啓動時啓動我的服務,但我一直收到1245-1263/? I/ActivityManager﹕ Waited long enough for: ServiceRecord{3d8aa3b0 u9 myPackage/.myService}異常。如何設置服務的優先級以確保它在啓動時啓動?

我看過這個:My service always getting Waited long enough for: ServiceRecord error in Kitkat 它告訴我,ActivityManager正在啓動我的服務暫停。我已通過日誌聲明確認我的接收器確實正在工作。這是在任何情況下的代碼有興趣:

public class PhoneStateReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.d("OnStartUp", "Service is about to be started"); 
     Intent myIntent = new Intent(context, myService.class); 
     context.startService(myIntent); 
    } 
} 

那麼,我該怎麼設置我的服務的優先級爲「必要的」,這樣的Android確保它啓動?我看了這個:How to set the priority of android service?,但它涉及重新啓動服務,而不是初始啓動。

這裏是我的清單的相關位:

<receiver android:name=".PhoneStateReceiver" 
    android:enabled="true" 
    android:label="PhoneStateReceiver" 
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 
</receiver> 
<service 
    android:name=".myService" 
    android:enabled="true" 
    android:exported="true" > 
</service> 

在此先感謝。

回答

2

你必須註冊一個啓動完成的廣播接收器,然後從那裏開始你的服務。這是一個工作實施。也許是你錯過的<uses-permission>部分?

編輯

在我的清單(重要部件),我有一個開頭<uses-permission>部分。

<manifest xmlns:android="http://schemas.android.com/apk/res/android" ...> 

    <!-- for services (must be rescheduled after boot) --> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

    <receiver android:name=".services.BootCompletedReceiver" > 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
    </receiver> 

</manifest> 

我的接收器類看起來像這樣。請注意,我正在啓動一個IntentService。

public class BootCompletedReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Intent msgIntent = new Intent(context, ReminderService.class); 
     msgIntent.setAction(ReminderService.ACTION_RESCHEDULE); 
     context.startService(msgIntent); 
    } 
} 
+0

我有,我想。我將編輯問題以顯示我的清單。 –

0

從所發佈的代碼,它看起來像清單正在尋找一個叫做.myService服務,並在廣播接收器你試圖啓動一個名爲LocationService.class服務。

這可能是問題。

+0

對不起,我打錯了。這不是原因。不過謝謝。 –

相關問題