PocketMagic

PocketMagic

Where Technology meets magic


Android
45 Posts
BlackBerry
4 Posts
Electronics
68 Posts
Hardware
120 Posts
High Voltage
49 Posts
iPhone
4 Posts
Linux
2 Posts
Nuclear
20 Posts
Optics
11 Posts
Photography
7 Posts
Photoshop
3 Posts
Research
19 Posts
Reviews
18 Posts
Robotics
8 Posts
Security
7 Posts
Software
73 Posts
Symbian
2 Posts
Tubes
18 Posts
Windows
3 Posts
Windows Mobile
11 Posts

Top Articles!       See PocketMagic on Facebook


uRADMonitor - Online Radiation monitoring station | 14810 Views | Rate 70.52
uRADMonitor - Online Radiation monitoring station
Developing for Blackberry 10 | 58 Views | Rate 58
Developing for Blackberry 10
Bluetooth and iOS - Use Bluetooth in your iPhone apps | 17760 Views | Rate 57.85
Bluetooth and iOS - Use Bluetooth in your iPhone apps
NMEA GPS Library for AVR Microcontrollers | 4778 Views | Rate 57.57
NMEA GPS Library for AVR Microcontrollers
Building a robot – Part 2 | 4528 Views | Rate 44.83
Building a robot – Part 2
Programmatically Injecting Events on Android - Part 2 | 4865 Views | Rate 44.23
Programmatically Injecting Events on Android - Part 2
Simple Switched power Supplies | 15882 Views | Rate 41.47
Simple Switched power Supplies
Capacitor Discharge Microspot Welder / Cutter | 11167 Views | Rate 36.14
Capacitor Discharge Microspot Welder / Cutter

 
  

Auto Start Android Service on Boot


By Radu Motisan Posted on October 16th, 2011 , 1068 Views (Rate 1.84)



  




For some applications, you will need to have your service up and running when the device is started, without user intervention. Such applications mainly include monitors (telephony, bluetooth, messages, other events).

At least this feature is currently allowed by the exaggeratedly restrictive Android permissions policy.

Step 1: First you'll need to create a simple service, defined in Monitor.java:

  1.  
  2. public class Monitor extends Service {
  3.  
  4. private static final String LOG_TAG = "::Monitor";
  5.  
  6. @Override
  7. public void onCreate() {
  8. super.onCreate();
  9. Log.e(LOG_TAG, "Service created.");
  10. }
  11.  
  12. @Override
  13. public void onStart(Intent intent, int startId) {
  14. super.onStart(intent, startId);
  15. Log.e(LOG_TAG, "Service started.");
  16. }
  17. @Override
  18. public void onDestroy() {
  19. super.onDestroy();
  20. Log.e(LOG_TAG, "Service destroyed.");
  21. }
  22.  
  23. @Override
  24. public IBinder onBind(Intent intent) {
  25. Log.e(LOG_TAG, "Service bind.");
  26. return null;
  27. }
  28. }
  29.  

Step 2: Next we need to create a Broadcast receiver class, StartAtBootServiceReceiver.java:

  1.  
  2. public class StartAtBootServiceReceiver extends BroadcastReceiver
  3. {
  4. private static final String LOG_TAG = "::StartAtBootServiceReceiver";
  5. @Override
  6. public void onReceive(Context context, Intent intent)
  7. {
  8. Log.e(LOG_TAG, "onReceive:");
  9. if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
  10. Intent i = new Intent();
  11. i.setAction("test.package.Monitor");
  12. context.startService(i);
  13. }
  14. }
  15. }
  16.  

Step 3: Finally, your AndroidManifest.xml file must contain the following:

  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  4. package="test.package.Monitor"
  5. android:versionName="1.0"
  6. android:versionCode="100"
  7. android:installLocation="internalOnly">
  8. <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:anyDensity="true" />
  9.  
  10. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
  11.  
  12. <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="8"/>
  13.  
  14. <application android:icon="@drawable/icon" android:label="@string/app_name">
  15. <service android:name="test.package.Monitor">
  16. <intent-filter>
  17. <action android:name="test.package.Monitor">
  18. </action>
  19. </intent-filter>
  20. </service>
  21. <receiver android:name="test.package.StartAtBootServiceReceiver">
  22. <intent-filter>
  23. <action android:name="android.intent.action.BOOT_COMPLETED">
  24. </action>
  25. <category android:name="android.intent.category.HOME">
  26. </category>
  27. </intent-filter>
  28. </receiver>
  29. </application>
  30. </manifest>
  31.  

I need to highlight some of the most important aspects, key factors for possible errors in implementation:
1) The permission android.permission.RECEIVE_BOOT_COMPLETED must be provided (in the manifest xml)
2) The installation must be performed in internal storage, not on SDCARD! To enforce this use android:installLocation="internalOnly" in the manifest






  

More on PocketMagic:

Programmatically Injecting Events on Android - Part 1 | 9993 Views | Rate 25.89
Programmatically Injecting Events on Android - Part 1
Atmega8 and enc28J60 for ethernet support | 7125 Views | Rate 24.32
Atmega8 and enc28J60 for ethernet support
Victoreen CDV-717 Remote Gamma Survey dosimeter | 97 Views | Rate 24.25
Victoreen CDV-717 Remote Gamma Survey dosimeter
How to set the AVR Fusebits | 1650 Views | Rate 23.57
How to set the AVR Fusebits
CDV-742 Dosimeter and CDV-750 Dosimeter Charger | 94 Views | Rate 23.5
CDV-742 Dosimeter and CDV-750 Dosimeter Charger
ATMega128 and HD44780 LCD using 3 Wires with the 74HC164 | 2008 Views | Rate 22.82
ATMega128 and HD44780 LCD using 3 Wires with the 74HC164

Thank you for commenting. Your comment won't show until approved, which can take some time.

Please copy the string NcwAjG to the field below: