Reputation: 4179
I am working on an Application that requires to start a BackgroundService
on Android Boot
This is the code that I am using to start the BroadcastReceiver
on Android Boot
public class StartOnBootService extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try
{
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.package.myApplicationPackage.BackgroundService");
context.startService(serviceIntent);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
This is my BackgroundService.class
public class BackgroundService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
//code to execute when the service is first created
Toast.makeText(getBaseContext(), "BACKGROUND SERVICE STARTED", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
//code to execute when the service is shutting down
}
@Override
public void onStart(Intent intent, int startid) {
//code to execute when the service is starting up
}
}
This is the error log that I managed to snipe from the CatLog while my Android was Booting.
12-21 10:28:01.279: E/EmbeddedLogger(1710): App crashed! Process: com.package.myApplicationPackage
12-21 10:28:01.289: E/EmbeddedLogger(1710): App crashed! Package: com.package.myApplicationPackage v1 (1.0)
12-21 10:28:01.289: E/EmbeddedLogger(1710): Application Label: myApp Label
My AndroidManifest.xml
file
</service>
<service android:name=".BackgroundService">
<intent-filter>
<action android:name="com.package.myApplicationPackage.BackgroundService" />
</intent-filter>
</service>
<receiver
android:name=".receiver.StartOnBootService"
android:enabled="true"
android:exported="true"
android:label="StartOnBootService">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Upvotes: 2
Views: 1193
Reputation: 2897
Maybe it's a good practice to look at the manifest file before rushing over stackoverflow. I also had a similar problem, "crash after boot" and noticed that I had a wrong package name, as a result of a hasty copy paste action for one of my receivers.
<receiver android:name="com.somepackage.broadcast.ConnectionChangeReceiver" />
was indeed having a typo as
<receiver android:name="com.somepackage.broadcast.broadcast.ConnectionChangeReceiver" />
Correcting it obviously solved my problem.
Upvotes: 0
Reputation: 56925
You Broadcast receiver name and android manifest receiver name is totally differ.
StartOnBootService
or in manifest it is StartMyServiceAtBootReceiver
Upvotes: 3