Android TutorialsImplementing Simple Intent Service in Android

Implementing Simple Intent Service in Android

Hello Guys !! In this tutorial we are going to implement Intent Service. It is an easy demonstration of the concept and all we are doing in this example is making the process sleep for number of seconds as entered by the user.

  • Steps are as Follows:
  1. Create a new project , yor MainActivity.java and activity_main.xml file will be generated.
  2. Now take another java file for example name it as Sleeper.Java
  3. In Sleeper .Java extend the class with an IntentService.
  4. 1

  5. Intent Service requires a no_argument constructor whose name is same as the name of the class and pass it upto super.
  6. onHandleIntent is the handle that handles the intent which is declared to be protected :
  7. [code language=”java”]
    protected void onHandleIntent(Intent intent)
    {
    // TODO Auto-generated method stub

    seconds =intent.getExtras().getLong("seconds");
    long millis = seconds * 1000;
    try
    {
    Thread.sleep(millis);
    }
    catch(InterruptedException e)
    {
    e.printStackTrace();
    }
    }

    [/code]

  8. Then we write a try catch block.
  9. In try block ,we try to use sleep in a thread ,so Thread.sleep(millis);
  10. millis is the milli seconds that we have declared as :
    long millis = seconds * 1000;
  11. Add onDestroy method after the onHandleIntent method:
  12. 2

  13. onDestroy() method is used to destroy the thread and using a Toast.makeText we display the message.
  14. The code is as follows:
  15. [code language=”java”]public void onDestroy()
    {
    super.onDestroy();
    Toast.makeText(this,String.format("Slept %d seconds",seconds),Toast.LENGTH_SHORT).show();
    }

    [/code]

  16. Now in the Mainactivity.Java the onCreate() method is the same as it is.
  17. In the activity_main.xml file take a text field and a button .
  18. Now give the onClick property of the button a name for example : startService which will be using in the mainactivity.java file on the button click code.
  19. Now in the MainActivity.Java file write the code on the button click :
  20. [code language=”java”]public void startService(View v)

    {

    EditText sleepTime=(EditText)findViewById(R.id.editSeconds);

    Long secondsToSleep=Long.parseLong(sleepTime.getText().toString());

    Intent intent= new Intent(MainActivity.this,Sleeper.class);

    intent.putExtra("seconds", secondsToSleep);

    startService(intent);

    }

    [/code]

  21. We pass out the value of the text as long and get the text from the edit text convert it to string and pass to Long.
  22. Create an Intent and pass it from main activity to the corresponding class.
  23. Put the extras i.e seconds in secondsToSleep and start the Service.
  24. Also don’t forget to add service in the android Manifest file.
  25. [code language=”xml”]< service
    android:name=”.Sleeper”>
    </service>
    [/code]

  26. Now run the app and you will have the following output :
  27. 3

  28. Enter the number of seconds and click the Start Service button and after the respective number of seconds the application will pick up the Toast to display the message.”Slept for __5__ seconds”.
  29. Thus we have implemented Intent Service successfully .

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -