I'm going to start a service when device boot completed. I write my code following this:
Manifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name="com.test.MBootBroadcastReceiver" android:enabled="true" android:exported="false" >
<intent-filter android
riority="1">
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service android:name="com.test.TestService" android:enabled="true" android:exported="true" />
MBootBroadcastReceiver.java
public class MBootBroadcastReceiver extends BroadcastReceiver {
@override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "BOOT_COMPLETED:" + intent.getAction(), Toast.LENGTH_LONG).show();
Intent i = new Intent(context, TestService.class);
context.startService(i);
}
}
TestService.java
public class TestService extends Service {
@nullable
@override
public IBinder onBind(Intent intent) {
return null;
}
@override
public int onStartCommand(Intent intent, int flags, int startId) {
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.app_name))
.setContentText("Test Service Running")
.setSmallIcon(R.mipmap.ic_launcher)
.setWhen(System.currentTimeMillis())
.setPriority(Notification.PRIORITY_MIN)
.setOngoing(true)
.build();
notification.flags = Notification.FLAG_ONGOING_EVENT;
startForeground(startId, notification);
return super.onStartCommand(intent, flags, startId);
}
}
It works good when I run adb shell am broadcast -a android.intent.action.BOOT_COMPLETED it in my command. But when I restart my android phone, it not works.
What's wrong with my service?
Thanks in advice.
Manifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name="com.test.MBootBroadcastReceiver" android:enabled="true" android:exported="false" >
<intent-filter android

<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service android:name="com.test.TestService" android:enabled="true" android:exported="true" />
MBootBroadcastReceiver.java
public class MBootBroadcastReceiver extends BroadcastReceiver {
@override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "BOOT_COMPLETED:" + intent.getAction(), Toast.LENGTH_LONG).show();
Intent i = new Intent(context, TestService.class);
context.startService(i);
}
}
TestService.java
public class TestService extends Service {
@nullable
@override
public IBinder onBind(Intent intent) {
return null;
}
@override
public int onStartCommand(Intent intent, int flags, int startId) {
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.app_name))
.setContentText("Test Service Running")
.setSmallIcon(R.mipmap.ic_launcher)
.setWhen(System.currentTimeMillis())
.setPriority(Notification.PRIORITY_MIN)
.setOngoing(true)
.build();
notification.flags = Notification.FLAG_ONGOING_EVENT;
startForeground(startId, notification);
return super.onStartCommand(intent, flags, startId);
}
}
It works good when I run adb shell am broadcast -a android.intent.action.BOOT_COMPLETED it in my command. But when I restart my android phone, it not works.
What's wrong with my service?
Thanks in advice.