Reboot Broadcast Reciever

  • Antworten:0
  • OffenNicht stickyNicht beantwortet

24.07.2010 17:31:06

Bitte beachtet das die Methode startService entsprechend noch geändert werden sollte. Ebenfall ist hier nur angedeutet das der User evtl auswählen können sollte
ob euer Service bei einem Reboot gestartet werden soll. Um das Snippet einfach zu halten habe Ich das entfernt!

1/**
2 * Broadcast Reciever which gets called when Boot is complete. When called it
3 * starts the Background Service when configured.
4 *
5 * @author mac
6 *
7 */
8public final class BootCompletedBroadcastReceiver extends BroadcastReceiver
9{
10
11 private final static String LOG_TAG = "BootCompletedReceiver";
12
13 /*
14 * (non-Javadoc)
15 *
16 * @see android.content.BroadcastReceiver#onReceive(android.content.Context,
17 * android.content.Intent)
18 */
19 @Override
20 public void onReceive(final Context _context, final Intent _intent)
21 {
22 if (Logging.isEnabled)
23 {
24 Log.d(LOG_TAG, "BootCompletedBroadcastReceiver#onRecieve(...) called.");
25 }
26
27 if (Intent.ACTION_BOOT_COMPLETED.equals(_intent.getAction()))
28 {
29 if (Logging.isEnabled)
30 {
31 Log.i(LOG_TAG, "Boot completed.");
32 }
33 startService(_context);
34 }
35 }
36
37 /**
38 * Starts the Service
39 *
40 * @param _context
41 */
42 private static void startService(final Context _context)
43 {
44 final boolean isLaunchOnBoot = true;
45
46 if (isLaunchOnBoot)
47 {
48 final Intent startServiceIntent = new Intent();
49 startServiceIntent.setAction(#your action there#);
50 startServiceIntent.putExtra("add some extra which can notify the service that its lauched via an reboot");
51 _context.startService(startServiceIntent);
52 }
53 }
54}

Windmate HD, See you @ IO 14