In this tutorial we are going to implement a Simple Service which we have to start and stop.
- In this implementation of Service which will calculate the average of the series of number’s that we will enter in the text field.
- Steps are as Follows:
- Create a new project , your MainActivity.java and activity_main.xml files will be created.
- In the activity_main.xml file take a Large Text from the Palette.
- Also take the Plain Text from the Text Fields and two buttons in the activity_Main.xml file.
- Set the button property as : android:text=”Stop Service”.
- And for another button set as : android:text=”Average”.
- So now the text of the buttons are set as Stop Service and Average .
- Now set the text in the text property of the TextView.
- For example : android:text=”Enter Numbers ,Space Seperated”.
- So Now your activity_main file’s Graphical Layout will look like this :
- Now go to your MainActivity.java file .
- In this declare the following in the MainACtivity class as :
- Private EditText numbersText ;
- Private static Intent averagingIntent ;
- Now in the onCreate( ) method insert the following code :
numbersText = (EditText) findViewById(R.id.editNumbers);
- So the MainActivity.java file will look for the EditText by finding it by its id.
- So the MainActivity.java file will look like this :
- Now write the following code for Average button in the MainActivity.java file as shown below :
- Now startService is the name given to the onClick property of the Average button.
- Let’s see the explanation of the code line by line :
- startService gets a String array of numbers from the numbersText,getting its text ( ) ,converting it to string( ) and then splitting it on the space character.
- Here we take the string array and convert it to the integer array by doing new int and the [strNumbers.length].
- We take a for loop to set the integer array to the numbers entered by the user .
- Now set up the intent by using the calling class and the target class and startService with the intent.
- Then stop the service.
- So now you will have the following code :
- Now take another java class for example: TestService.java
- Class TestService must be extended with Service .
- In this write the oncreate( ) method which must be called through super.
- The next method important here is the onStartCommand this is where the intent enters into the service.
- Here we get the numeric array an integer array by getting those extras with that number array.
- Then the avg is the calculated average that is calculated by the “ find average logic” .
- Toast is used to display the message of the calculated average.
- Then return the start Id.
- Also call the onDestroy( ) method to stop the service .
- Write down the logic for the calculation of average.
- So your window will look as shown below :
- Now in the android manifest file list the service as stated
- Now run your app.
- You will get the following display :
- Now enter the numbers and click on the Average button :
- Thus a Tost message will display the calculated average :
- Now click on the Stop Service button to stop the service
- Thus we have implemented a Normal Service in Android
[code language=”java”]public void startService(View v)
String strNumbers = numbersText .getText().toString().split(" ");
int numbers = new int[strNumbers.length];
averagingIntent = new Intent(MainActivity.this,TestService.class);
[code language=”java”]public void onCreate()
[code language=”java”]public int onstartCommand(Intent intent,int flags, int startId)
int numArray = intent.getExtras().getIntArray("numbers");
float avg = findAverage(numArray);
Toast.makeText(getApplicationContext(), String.format("Average : %f", avg), Toast.LENGTH_SHORT).show();
[code language=”java”]float findAverage(intnums)
sum += nums[i];