GTU Android Program 9 Android Service Example

      Comments Off on GTU Android Program 9 Android Service Example

GTU Android Program 9 Android Service Example Create an background application that will open activity on specific time.

GTU Android Program 9

Android Service is the same as Activity but it don’t have interface. Some task need to be repeated many time and frequently in this kind of situation we need to use the Android Service. Android service is the best way to execute on background when its required. For example you setup alarm of morning and in morning alarm will automatically starts, that’s magic of Android Service.

Step 1 : Create New Android Project

Step 2 : Add New Java File (Which we are going to create Service, Remember only java File Not Activity)

Step 3 : Write down code in Service File

Step 4 : Register Your service into AndroidManifest.xml sothat we can call service.

Step 5 : Add 2 button in your main activity to start/stop service.

Check out the complete code of the Android Service Example.

Question related with android service.

  • Differentiate Activity and Service
  • Explain android service.
  • write down code snippet of android service.

 

Location : res\layout

FileName: activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="gtu.android.prog09.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp"
        android:text="Service Example"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="37dp"
        android:text="Start" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="16dp"
        android:text="Stop" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="68dp"
        android:text="Developed By: Adarsh Patel"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

 

Location : src\package\

FileName : MainActivity.java

package gtu.android.prog09;

/*
* GTU Android Program 9 Android Service Example Create an background application that will open activity on specific time.
*
*/

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

	Button b1,b2;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		b1 = (Button) findViewById(R.id.button1);
		b2 = (Button) findViewById(R.id.button2);
		
		//==============================================
		// Button Event to Start Service
		//==============================================
		b1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
	
				Intent i = new Intent(getApplicationContext(),ExampleService.class);
				startService(i);
			}
		});
		
		//==============================================
		// Button Event to Stop Service
		//==============================================
		b2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
	
				Intent i = new Intent(getApplicationContext(),ExampleService.class);
				stopService(i);
			}
		});
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

 

Continue Reading…