问题
I'm trying start a service at the time of booting the device. I have searched many all over and followed the steps and it is still not working out. Once the mobile reboots it shows in the beginning that "application has stopped unexpectedly". I know it is from the receiver file, but couldn't figure out where particularly the problem is.
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>
Is there anything I need to add so that the service starts perfectly.
回答1:
declare your receiver in AndroidManifest.xml
as:
<receiver android:name=".Receiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
because you have BroadcastReceiver class as Receiver.class
but declaring it in AndroidManifest.xml with MyReceiver
回答2:
Please check you android manifest it should be like this :
<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>
The problem is in declaration of your receiver. Please check the android:name tag, it should have the right class name.
回答3:
Instead of using
Intent myIntent = new Intent();
myIntent.setAction("com.androidhive.jsonparsing.UpdateService");
context.startService(myIntent);
Try doing this instead in the receiver
Intent myIntent = new Intent(context, UpdateService.class);
context.startService(myIntent);
来源:https://stackoverflow.com/questions/14334449/service-not-starting-at-reboot