Last exercise "Start a service to send Notification" show how to start a service by user clicking in a activity. In this exercise, a BroadcastReceiver will be implemented to receive Intent of "android.intent.action.BOOT_COMPLETED" action after power-up, no interactive with user.
"android.intent.action.BOOT_COMPLETED" is Broadcast Action, is broadcast once, after the system has finished booting. It can be used to perform application-specific initialization. You must hold the RECEIVE_BOOT_COMPLETED permission in order to receive this broadcast.
Create a new class AutoStartNotifyReceiver.java, extends BroadcastReceiver. Simple do the same thing as that in Start button OnClickListener in last exercise "Start a service to send Notification", start a service.
In order to receive Intent of "android.intent.action.BOOT_COMPLETED", modify AndroidManifest.xml to add for AutoStartNotifyReceiver, also grant permission of "android.permission.RECEIVE_BOOT_COMPLETED".
Keep using the other files implemented in last exercise "Start a service to send Notification".
Download the files.
"android.intent.action.BOOT_COMPLETED" is Broadcast Action, is broadcast once, after the system has finished booting. It can be used to perform application-specific initialization. You must hold the RECEIVE_BOOT_COMPLETED permission in order to receive this broadcast.
Create a new class AutoStartNotifyReceiver.java, extends BroadcastReceiver. Simple do the same thing as that in Start button OnClickListener in last exercise "Start a service to send Notification", start a service.
package com.exercise.AndroidNotifyService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AutoStartNotifyReceiver extends BroadcastReceiver {
private final String BOOT_COMPLETED_ACTION = "android.intent.action.BOOT_COMPLETED";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals(BOOT_COMPLETED_ACTION)){
Intent myIntent = new Intent(context, com.exercise.AndroidNotifyService.NotifyService.class);
context.startService(myIntent);
}
}
}
In order to receive Intent of "android.intent.action.BOOT_COMPLETED", modify AndroidManifest.xml to add
package="com.exercise.AndroidNotifyService"
android:versionCode="1"
android:versionName="1.0">
android:label="@string/app_name">
Keep using the other files implemented in last exercise "Start a service to send Notification".