2013-01-15 87 views
0

我想在啓動設備時啓動服務。我已經搜遍了很多,並遵循了這些步驟,但仍然沒有成功。一旦手機重新啓動,它就會在開始時顯示「應用程序意外停止」。我知道它來自接收器文件,但無法弄清楚問題的具體位置。服務不在重新啓動

Receiver.java

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 

public class Receiver extends BroadcastReceiver{ 

    @Override 
    public void onReceive(Context context, Intent arg1) { 
     // TODO Auto-generated method stub 
     if(arg1.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) 
     { 
      Intent myIntent = new Intent(); 
      myIntent.setAction("com.androidhive.jsonparsing.UpdateService"); 
      context.startService(myIntent); 
     } 
    } 
} 

AndroidManifest

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity 
     android:label="@string/app_name" 
     android:name=".AndroidJSONParsingActivity" > 
     <intent-filter > 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <!-- Single List Item View --> 
    <activity 
     android:label="Single Menu Item" 
     android:name=".SingleMenuItemActivity" > 
    </activity> 
    <service android:name=".UpdateService"> 
     <intent-filter> 
      <action android:name="com.androidhive.jsonparsing.UpdateService"/> 
     </intent-filter> 
    </service> 
    <receiver android:name=".MyReceiver" 
     android:enabled="true" 
     android:exported="false"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED"/> 
     </intent-filter> 
    </receiver> 
</application> 

請問有什麼需要補充,使該服務完全啓動。

+0

,而不是這樣做的? –

回答

0

聲明你的接收機AndroidManifest.xml爲:

<receiver android:name=".Receiver" 
     android:enabled="true" 
     android:exported="false"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED"/> 
     </intent-filter> 
    </receiver> 

,因爲你有廣播接收器類爲Receiver.classMyReceiver

0

宣佈它在AndroidManifest.xml請檢查您的Android清單應該是這樣的:

<application 
android:icon="@drawable/ic_launcher" 
android:label="@string/app_name" > 
<activity 
    android:label="@string/app_name" 
    android:name=".AndroidJSONParsingActivity" > 
    <intent-filter > 
     <action android:name="android.intent.action.MAIN" /> 

     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 

<!-- Single List Item View --> 
<activity 
    android:label="Single Menu Item" 
    android:name=".SingleMenuItemActivity" > 
</activity> 
<service android:name=".UpdateService"> 
    <intent-filter> 
     <action android:name="com.androidhive.jsonparsing.UpdateService"/> 
    </intent-filter> 
</service> 
<receiver android:name=".Receiver" 
    android:enabled="true" 
    android:exported="false"> 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED"/> 
    </intent-filter> 
</receiver> 

問題在於您的接收器聲明。 請檢查android:name標籤,它應該有正確的類名稱。

0

而不是使用

Intent myIntent = new Intent(); 
myIntent.setAction("com.androidhive.jsonparsing.UpdateService"); 
context.startService(myIntent); 

嘗試在什麼是在logcat中所示的接收機

Intent myIntent = new Intent(context, UpdateService.class); 
context.startService(myIntent);