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>
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.
ReplyDeleteThanks in advance!!!
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;
ReplyDelete@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
thnks....!!!
ReplyDeletenice tutorial...!!!
plz post, how we pass the values from service to activity...???
thanks...
firzan
Hi,
ReplyDeleteExcellent blog post. thanks for sharing with us.
This comment has been removed by the author.
ReplyDeletethnks...
ReplyDeleteHighlight the part of manifest file modded.. So that its easy to understand the addition of service in xml file
ReplyDelete
Deleterequired part
service class=".MyService" android:name=".MyService"
Deletejust replace the "name" attribute with "android:name"
ReplyDeleteThat's it :)
I want to get key press events inside the service.. how can i do it..
ReplyDelete