DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > How to Create a Background Service in Android

How to Create a Background Service in Android

Not everything needs to run in a focussed application.

Nilanchala Panigrahy user avatar by
Nilanchala Panigrahy
·
Aug. 04, 15 · Mobile Zone · Tutorial
Like (1)
Save
Tweet
22.60K Views

Join the DZone community and get the full member experience.

Join For Free

To create a background service, first you need to add the service into your manifest file.  Then, create a class that extends service.  Finally, in your activity start the service.

1. First add the following service declaration in your application manifest file.

<service android:enabled="true" android:name=".MyService">
</service>

2. Create a new class MyService that extends Service class.

public class MyService extends Service {
  @Override
  public void onCreate() {
  }

  @Override
  public void onStart(Intent intent, int startId) {
    //do something
  }

  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }
}

3. To start the service and stop the service:


public class MyActivity extends Activity {
  @Override
  public void onCreate() {
    …
    startService(new Intent(this, MyService.class);
  }

  @Override
  public void onStop() {
    …
    stopService(new Intent(this, MyService.class));
  }
}
Android (robot)

Published at DZone with permission of Nilanchala Panigrahy, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Translate Value to Executives Using an Outcome-Driven Mindset
  • SQL Database Schema: Beginner’s Guide (With Examples)
  • 5 Myths of Kubernetes
  • Migrating Legacy Applications and Services to Low Code

Comments

Mobile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo