Basic Android background Service

Running sample program for background service

Step 1 :
Create sample service class

package com.javaorigin.android.sample.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {

String tag="TestService";
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();
Log.i(tag, "Service created...");
}

@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.i(tag, "Service started...");
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
}

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


Step 2 :
Create sample Activity class

package com.javaorigin.android.sample.service;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class SampleAction extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView view = new TextView(this);
view.setText("Service Test");
Intent i = new Intent();
i.setClassName( "com.javaorigin.android.sample.service",
"com.javaorigin.android.sample.service.MyService" );
bindService( i, null, Context.BIND_AUTO_CREATE);
this.startService(i);
setContentView(view);
}
}


Step 3:
Configure AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.javaorigin.android.sample.service" android:versionCode="1"
android:versionName="1.0">
<application icon="@drawable/icon" label="@string/app_name">
<service class=".MyService" name=".MyService">
<intent-filter>
<action android:value="com.javaorigin.android.sample.service.MY_SERVICE"
android:name=".MyService" />

</intent-filter>
</service>
<activity android:name=".SampleAction"
android:label="@string/app_name">
<intent-filter>
<action name="android.intent.action.MAIN">
<category name="android.intent.category.LAUNCHER">
</intent-filter>
</activity>

</application>
<uses-sdk minsdkversion="8">

</manifest>

48 comments :

  1. Hi tutorial for new-bie but i want to add some new features to service class - for example , getting the latitude and longitude and sending it to the server. How can i do that and where should i write the code.

    Thanks in advance!!!

    ReplyDelete
    Replies
    1. im getting connection is null error!

      Delete
    2. Great Article android based projects

      Java Training in Chennai

      Project Center in Chennai

      Java Training in Chennai

      projects for cse

      The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

      Delete
  2. Getting any geo information you will need to use Android's LocationManager and Location classes. Google how to use them; you will get all that you need and more;

    @author on your manifest has one tiny error, the attribute name should be android:name instead of just name;

    Otherwise this has been of great help! appreciated atom

    ReplyDelete
  3. thnks....!!!
    nice tutorial...!!!
    plz post, how we pass the values from service to activity...???

    thanks...

    firzan

    ReplyDelete
  4. Hi,

    Excellent blog post. thanks for sharing with us.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Highlight the part of manifest file modded.. So that its easy to understand the addition of service in xml file

    ReplyDelete
  7. just replace the "name" attribute with "android:name"
    That's it :)

    ReplyDelete
  8. I want to get key press events inside the service.. how can i do it..

    ReplyDelete
  9. i want to call a method of service in other in other application...
    How can i do this????

    ReplyDelete
  10. Will this service keep running even if the App is not running?

    ReplyDelete
  11. How to show Toast message for every 10 sec using this service??

    ReplyDelete
  12. i want background service in android which never be stopped.

    ReplyDelete
  13. What is com.sec.android.daemonapp

    ReplyDelete
  14. I simply want to say I’m very new to blogs and actually loved you’re blog site. Almost certainly I’m going to bookmark your blog post . You absolutely come with great well written articles. Thanks a lot for sharing your blog.
    Best Android Training in Velachery | android development course fees in chennai

    ReplyDelete
  15. This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.Android Training in chennai | Best Android Training in chennai

    ReplyDelete
  16. I am expecting more interesting topics from you. And this was nice content and definitely it will be useful for my friends. Android Training in Ahmedabad
    Android Live Project Training in Ahmedabad

    ReplyDelete
  17. This information you provided in the blog that is really unique I love it!! Thanks for sharing such a great blog. Keep posting..
    Android training in Noida
    Android training institute in Noida
    Android course in Noida

    ReplyDelete

  18. Thank you for sharing such great information very useful to us.
    Android Training in Noida

    ReplyDelete
  19. Well written and interesting thoughts. Its amazing how sometimes we get inspiration from the most unexpected of quarters !
    school app in chennai

    ReplyDelete
  20. Nice Bog! Thank you for sharing the valuable information.
    mobile app development in usa

    ReplyDelete
  21. Thank you for sharing such great information very useful to us.
    Android Course in Noida

    ReplyDelete
  22. Please continue this great work and I look forward to more of your awesome blog posts

    Corporate training in Machine learning

    ReplyDelete
  23. I really appreciate this post and I like this very much. I am waiting for new post here and Please keep it up in future.. Website Development Sydney,App Developers Sydney

    ReplyDelete



  24. INSTEAD OF GETTING A LOAN,, I GOT SOMETHING NEW
    Get $10,050 USD every week, for six months!

    See how it works
    Do you know you can hack into any ATM machine with a hacked ATM card??
    Make up you mind before applying, straight deal...
    Order for a blank ATM card now and get millions within a week!: contact us
    via email address:: besthackersworld58@gmail.com or whats-app +1(323)-723-2568

    We have specially programmed ATM cards that can be use to hack ATM
    machines, the ATM cards can be used to withdraw at the ATM or swipe, at
    stores and POS. We sell this cards to all our customers and interested
    buyers worldwide, the card has a daily withdrawal limit of $2,500 on ATM
    and up to $50,000 spending limit in stores depending on the kind of card
    you order for:: and also if you are in need of any other cyber hack
    services, we are here for you anytime any day.
    Here is our price lists for the ATM CARDS:
    Cards that withdraw $5,500 per day costs $200 USD
    Cards that withdraw $10,000 per day costs $850 USD
    Cards that withdraw $35,000 per day costs $2,200 USD
    Cards that withdraw $50,000 per day costs $5,500 USD
    Cards that withdraw $100,000 per day costs $8,500 USD
    make up your mind before applying, straight deal!!!

    The price include shipping fees and charges, order now: contact us via
    email address::besthackersworld58@gmail.com or whats-app +1(323)-723-2568

    ReplyDelete
  25. I really enjoy reading of your article. I wanted to inform you that you have people like me who appreciate your work.

    techwithgeeks
    talesbuzz
    whizzherald

    ReplyDelete
  26. Do you need to hack into any, databaseserver spy on Facebook,Emails, Whatsapp, Viber, Snapchat, Instagram and many more.
    I urge you to get in touch with the best people for the job, i have confirm the service when i need to spy on my spouse phone. They are good at Phone Cloning and Bitcoin/binary minning and any other hack job.
    Thanks guys for the team work HACKINTECHNOLOGYATGMAILDOTCOM
    +12132951376(WHATSAPP)

    ReplyDelete
  27. Hi
    It is great and so amazing post and I am enjoying to read your blog. I am very grateful for the effort put on by you, to guide us, Thank a lot for this informative post ,keep posting such type of wonderful post. Keep it up. We will also offer QuickBooks Customer Service Number Contact us 1-855-756-1077 for instant help.

    ReplyDelete
  28. Outstanding article, many articles I come across these days do not really provide anything that attracts others as yours, but believe me the way you interact is literally awesome I do respect that so much. I will instantly grab your rss feed to stay informed of any updates you make and as well take the advantage to share some vital information regarding the
    UNDETECTED FAKE BANK STATEMENTS FOR ONLINE VERIFICATION which many are not yet informed of. This is the UNDETECTED FAKE BANK STATEMENTS many Americans uses to get loans of all types, PPP loan, Auto loan, Mortgage loan etc approved within 24 to 48hours no matter your credit score. Not over demanding, I will also take the advantage to ask for your permission to join our 179.3k members TELEGRAM GROUP
    .As to share with us your ideas or any latest update on your blog.
    Thanks I am Scott from Globex, we are expecting you on our platform.

    ReplyDelete
  29. i m using Quickbooks software since long time but when i faced any technical problem then i called
    QuickBooks Customer Service who resolve my issue quickly

    ReplyDelete
  30. Simply we can say that Background Services is a service that runs in the background of an Android device. It can be used to refresh content, handle sensors, and more. It is a fundamental part of Android's architecture
    Learn Android Programming for Basic android background services

    ReplyDelete