A typical usage for booting services when a phone starts is a message pull notification system. In the following, I have written a BoardcastReceiver to listen to the system boot event.
First, Create a BroadcastReciever:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class ActivateAtBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent();
i.setAction("com.pof.android.MessageService");
context.startService(i);
}
}
}
In AndroidManifest.xml,
Add the following permission inside the manifest tag:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Add the following to inside the application tag:
<service android:name=".MessageService"
android:process=":MessageService"
android:label="Message Notification">
<intent-filter>
<action android:name="com.pof.android.MessageService">
</action>
</intent-filter>
</service>
<receiver android:name=".ActivateAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
<category android:name="android.intent.category.HOME">
</category>
</intent-filter>
</receiver>
Note: MessageService is created by me. You will want to replace that with your own service.
No comments:
Post a Comment