How to start a backgroud service on System/Emulator startup?

Put your problem here if it does not fit any of the other categories.

How to start a backgroud service on System/Emulator startup?

Postby Rader » Mon Mar 10, 2008 8:09 am

Launch Activity on System/Emulator Startup( http://www.anddev.org/viewtopic.php?t=428 ), is a very good post to me!


I make a small change to the sample Plusminus has delivered in the post, since I want to start a background service instead of an activity on System/Emulator startup.

So I replace the LaunchOnStartup activity by a service “NotifyingService”, and change the manifest to this one:


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3.  
  4.  
  5. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  6.  
  7.  
  8.  
  9.    package="cienet.cl">
  10.  
  11.  
  12.  
  13.     <application android:icon="@drawable/icon">
  14.  
  15.  
  16.  
  17.         <receiver android:name=".MyStartupIntentReceiver">
  18.  
  19.  
  20.  
  21.             <intent-filter>
  22.  
  23.  
  24.  
  25.                  <action android:name="android.intent.action.BOOT_COMPLETED"/>
  26.  
  27.  
  28.  
  29.                  <category android:name="android.intent.category.HOME"/>
  30.  
  31.  
  32.  
  33.             </intent-filter>
  34.  
  35.  
  36.  
  37.         </receiver>
  38.  
  39.  
  40.  
  41.        
  42.  
  43.  
  44.  
  45.     <service android:name="NotifyingService">
  46.  
  47.  
  48.  
  49. </service></application>
  50.  
  51.  
  52.  
  53. </manifest>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4




Then in the onCreate method of MyStartupIntentReceiver, I replace the startActivity by startService:



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     public void onReceiveIntent(Context context, Intent intent) {      
  2.  
  3.  
  4.  
  5.         /* Create intent which will finally start the Main-Activity. */
  6.  
  7.  
  8.  
  9.         Intent myStarterIntent = new Intent(context, NotifyingService.class);
  10.  
  11.  
  12.  
  13.         /* Set the Launch-Flag to the Intent. */
  14.  
  15.  
  16.  
  17.         myStarterIntent.setLaunchFlags(Intent.NEW_TASK_LAUNCH);
  18.  
  19.  
  20.  
  21.         /* Send the Intent to the OS. */
  22.  
  23.  
  24.  
  25. //        context.startActivity(myStarterIntent);
  26.  
  27.  
  28.  
  29.         context.startService(myStarterIntent, null);
  30.  
  31.  
  32.  
  33.     }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4




But, I get an error msg said there is no activity. So, can’t I start a service without an activity?


Best wishes,

Rader
Best regards,

Rader
Rader
Freshman
Freshman
 
Posts: 6
Joined: Thu Mar 06, 2008 8:10 am
Location: China

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 9 guests