I want create an application which contains only a service (no activity). This service must start on boot. My problem is that it seems the boot receiver don't seems call if there aren't activity. I have test with the following example. I have the different files :
MyReceiver.java :
package com.test.teststartserviceatboot;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive( Context ctx, Intent i ) {
Log.v( "MyReceiver", "onReceive : ");
Intent intent = new Intent( ctx, MonService.class );
ctx.startService(intent);
}
}
MyService.java :
package com.test.teststartserviceatboot;
import android.app.Service;
public class MyService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v( "MyService","onStartCommand" );
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind( Intent arg0 ) {
Log.v( "MyService","onBind" );
return null;
}
}
MainActivity.java:
package com.test.teststartserviceatboot;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I only modify the AndroidManifest on my several test.
Test 1 (with Activity)
<uses-sdk android:minSdkVersion="17" android:targetSdkVersion="17"/> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".MyService" android:exported="false" android:label="MyService" > </service> <receiver android:name=".MyReceiver" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> </application>
-> after reboot, the service is running.I see log :
AtivityManager Start proc com.test.teststartserviceatboot for broadcast com.test.teststartserviceatboot/.MyReceiver: pid=1808 uid=10156 gids={50156}
MyReceiver onReceive
MyService onStartCommand
Test 2 (without Activity)
<uses-sdk android:minSdkVersion="17" android:targetSdkVersion="17"/> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <service android:name=".MyService" android:exported="false" android:label="MyService" > </service> <receiver android:name=".MyReceiver" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> </application>
->The service don't running
- Test 3
I use the same appllication on test 1 (with activity). This time I kill the app before to restart the tablet (Parameter->Apps->TestServiceAtBoot->force stop). -> After reboot, service don't running
Is it necessary to have an activity for brodcast receiver works ? And why ?
Thank your for your lightening.