Reputation: 6785
I trying to add a service to my android app but the service wont start at all. I added it to my android manifest so that it starts in its own process and created a broadcastreceiver which should start the service after the BOOT_COMPLETED.
Here the part of my manifest
<service android:process=":attachServiceBackground"
android:name=".AttachService"
android:icon="@drawable/camera"
android:label="@string/attachServiceName" />
<receiver android:name="AttachmentStartupReceiver"
android:process=":attachServiceBackground">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
my service ... just added logging
public class AttachService extends Service {
private static final String TAG = AttachService.class.getSimpleName();
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate service");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
}
And my BroadCastReceiver
public class AttachmentStartupReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent attachSvc = new Intent(context, AttachService.class);
context.startService(attachSvc);
}
}
I cannot see that the service starts after booting the device, there is no "oncreate service" in the logs and also I looked in the Settings->Applications->Running Services. Does someone know what I made wrong?
Thanks
Upvotes: 4
Views: 3080
Reputation: 127
You need to add the following permission to your manifest file
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
BOOT_COMPLETED doesn't send a broadcast by default.
Upvotes: 5