How to create Adroid service?

General topics about the Android-Platform itself.
Coding issues please to the subforum right below.

How to create Adroid service?

Postby orbyone » Sat Jan 05, 2008 6:45 pm

Hi,

I would like to create an Android service, something that runs in the background all the time. I have seen an article in this forum talking about an Activity with BOOT_COMPLETED, is this the only way to do it? Ideally I would like the service to be hidden, i.e. not show up in an Activity Monitor, but I think I'm asking for too much ?

Thanks
Themos
orbyone
Freshman
Freshman
 
Posts: 7
Joined: Sat Jan 05, 2008 6:40 pm

Top

Postby plusminus » Sun Jan 06, 2008 1:12 pm

Hello orbyone,

of course you can start an Service instead of the Activity.
Don't know if you can run hidden Services and stuff... no bad thing I hope ;)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby orbyone » Sun Jan 06, 2008 4:01 pm

thanks for the reply plusminus (and great site by the way:)

could you be more specific? is all I need an XML with a service instead of an activity declared? could you type a sample XML perhaps?

Thanks!
orbyone
Freshman
Freshman
 
Posts: 7
Joined: Sat Jan 05, 2008 6:40 pm

Postby plusminus » Sun Jan 06, 2008 7:56 pm

Hello orbyone,

sth like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void onReceiveIntent(Context context, Intent intent) {
  2.  
  3.         Intent mainIntent = new Intent(context, KeepAlive.class);
  4.  
  5.         mainIntent.setLaunchFlags(Intent.NEW_TASK_LAUNCH);
  6.  
  7.         context.startService(mainIntent, null);
  8.  
  9. }
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby orbyone » Sun Jan 06, 2008 9:39 pm

yes, this looks fine. but will the service still run when the launching activity is terminated?

Themos
orbyone
Freshman
Freshman
 
Posts: 7
Joined: Sat Jan 05, 2008 6:40 pm

Postby andbasti » Mon Jan 28, 2008 4:16 pm

Yes, the service will still run when his activity is terminated.

I also like to start the hidden service only. The main problem for me is, that you see the empty activity for a moment on the display. :?

How can I start a background service on android without any launching activity or Intent Receiver (like android.intent.action.BOOT_COMPLETED)?
Or how make the activity invisible?
andbasti
Freshman
Freshman
 
Posts: 4
Joined: Mon Jan 28, 2008 3:24 pm

Top

Postby plusminus » Mon Jan 28, 2008 10:33 pm

Hello andbasti,

a IntentReceiver is not visible, isn't that what you want :?:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby andbasti » Tue Jan 29, 2008 10:40 am

Thanks for reply :)
Yes, I can use an IntentReceiver to start Services, but overall not really hidden in the background, because I have to start an visible Activity before.

IntentReceiver can be registered static in AndroidManifest.xml (through the <receiver> tag) or dynamic with Context.registerReceiver(). But in both solutions an Activity is needed and I don’t be able to start Activities full invisible (without a blank screen at startup).
andbasti
Freshman
Freshman
 
Posts: 4
Joined: Mon Jan 28, 2008 3:24 pm

Postby andbasti » Tue Jan 29, 2008 7:20 pm

I have it now! :D
Just use IntentReceiver and install the apk file with adb.exe.

Thanks
andbasti
Freshman
Freshman
 
Posts: 4
Joined: Mon Jan 28, 2008 3:24 pm

Top

Return to General

Who is online

Users browsing this forum: No registered users and 2 guests