GTU Android Program 9 Android Service Example Part 2

      Comments Off on GTU Android Program 9 Android Service Example Part 2

Go Back to GTU Android Program 9 Android Service Example Part 1

Hope you have understood the concept of the android service till now. Let discuss the remaining code and understand the rest of the part. Android service is very useful when you would like to do the background task as its done by the whatsapp and all messanger apps.

 

Location : src\package\

FileName : ExampleService.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.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class ExampleService extends Service {

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

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		//return super.onStartCommand(intent, flags, startId);
		Toast.makeText(this, "Service Started...", Toast.LENGTH_SHORT).show();
		return startId;
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		Toast.makeText(this, "Service Stopped.", Toast.LENGTH_SHORT).show();
	}
	
}

Location : \

FileName : AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="gtu.android.prog09"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="15" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <service android:name=".ExampleService"></service>

    </application>

</manifest>

Android Service Example Output 1

GTU Android Program 9

Android Service Example Output 2

GTU Android Program 9

Android Service Example Output 3

GTU Android Program 9

Download Complete Project Android Service Example

Download