View Full Version : How to create a service?
mast3rpyr0
10-02-2008, 08:34 PM
Anyone know? im not 100% clear on what Intents are for either and the documentation doesnt help me.
swaney29
10-03-2008, 12:09 PM
exactly wat do u mean service.. lik apps.. phone service can u be a little more detailed
regdent
10-03-2008, 02:16 PM
i think he means he wants to create an app which runs as a service in the background, for example windows has a print spooler service.
I did notice in the gravity Ringer application he has options for activating and deactivating the service so maybe some use to you if you can contact the programmer or get the source, link below.
http://code.google.com/p/snowservices/wiki/GravityRinger
mazimi
10-03-2008, 02:21 PM
http://code.google.com/android/reference/android/app/Service.html
mazimi
10-03-2008, 02:28 PM
Here are 2 tutorials as well:
http://developerlife.com/tutorials/?p=356
http://mylifewithandroid.blogspot.com/2008/02/double-life-of-service.html
prashantadesara
08-28-2010, 03:14 AM
Hello Friends,
Myself Prashant.
Me giving u WebService Example for Android.
Part - 1:
In this part I giving small demo for webservice running in Android device.
From this link you can find this example. Where I give Comment for webservice demo.
Please all of you check it.
http://lukencode.com/2010/04/27/calling-web-services-in-android-using-httpclient/#comment-179
Part - 2: Webservice running in Android with database connectivity
Now I am running database connectivity and return value from database and showing in Android device.
Here i m using Microsoft SQL connectivity. You can use your way.
For that u have to download this file
SQL connectivity jar file for webservice:
http://www.4shared.com/file/IxoPooiE/sqljdbc.html
Ksoap Library File for android project.
http://www.4shared.com/file/uOhx5aoj/ksoap2-android-assembly-24-jar.html
This is good example for you all.
First We create Dynamic Project usign Eclipse
Add this package and class.
Package Name: org.webservice.pa
Class Name : GetDatabaseRecords.java
and also create once class for bean.(Setter-Getter Property).
Class Name : BookData.java
BookData.java
package org.webservice.pa;
public class BookData {
public String bookCode;
public String bookName;
public String authorName;
public String getBookCode() {
return bookCode;
}
public void setBookCode(String bookCode) {
this.bookCode = bookCode;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthorName() {
return authorName;
}
public void setAuthorName(String authorName) {
this.authorName = authorName;
}
}
GetDatabaseRecords.java
package org.webservice.pa;
public class GetDatabaseRecords{
public BookData[] getBookDetails{
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
ArrayList<BookData> bookDataList = new ArrayList<BookData>();
BookData bookData = null;
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//Driver Name
con = DriverManager.getConnection("connectionUrl");//connection URL
stmt = con.createStatement();
rs = stmt.executeQuery("select * from BookDetails");//Table Name
while (rs.next())
{
bookData= new BookData();
bookData.setBookCode(rs.getString("bookCode"));
bookData.setBookName(rs.getString("bookName"));
bookData.setAuthorName(rs.getString("authorName"));
bookDataList.add(bookData);
}
}catch (Exception e)
{
e.printStackTrace();
}
finally {
try {
rs.close();
} catch(Exception e) {}
try {
stmn.close();
} catch(Exception e) {}
try {
con.close();
} catch(Exception e) {}
}
return (BookData[])bookDataList.toArray(new BookData[bookDataList.size()]);
}
}
Now Right click on GetDatabaseRecords.java file and select "WebService" option.
Now further step is same as posted my Part 1:
Click finish.
and test your service it will return BookData object.
Here our Dynamic WebProject task is completed.
Now you want to create Android Project for calling above WebService.
Now u add "ksoap" library File in android project:
Download from here:
http://www.4shared.com/file/uOhx5aoj/ksoap2-android-assembly-24-jar.html
public class MainActivity extends Activity {
private String METHOD_NAME = "getBookDetails"; // our webservice method name
private String NAMESPACE = "http://pa.webservice.org"; // Here package name in webservice with reverse order.
private String SOAP_ACTION = NAMESPACE + METHOD_NAME; // NAMESPACE + method name
private static final String URL = "http://192.168.1.1:8080/WebServiceDemo/services/GetDatabaseRecords?wsdl"; // you must use ipaddress here, don't use Hostname or localhost
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.txtsearch);
try
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION,envelope);
SoapObect so = (SoapObject)envelope.bodyIn;
System.out.println("Counter: "+so.getPropertyCount());
for(int i =0; i < so.getPropertyCount(); i++){
SoapObject soap = (SoapObject) so.getProperty(i);
System.out.println("BookCode :: "+ soap.getProperty("bookCode"));
System.out.println("BookName :: "+ soap.getProperty("bookName"));
System.out.println("AuthorName :: "+ soap.getProperty("authorName"));
}
tv.setText(so.toString());
} catch (Exception E) {
E.printStackTrace();
tv.setText("ERROR:" + E.getClass().getName() + ": " + E.getMessage());
}
}
}
Now you can run your project and it's work fine
:)
And if you have any doubt just tell me.
Thanks & Regards,
Prashant Adesara.
rohit.nagamalla
09-20-2010, 08:38 AM
Hi,
I created the web service correctly and its working fine and then created an Android application also as said above but when i ran the android app on my emulator its says permission denied. I followed the link given in Part - 1.
Can u please help me??
Regards,
Rohit
Scythe
09-20-2010, 09:08 AM
Hi,
I created the web service correctly and its working fine and then created an Android application also as said above but when i ran the android app on my emulator its says permission denied. I followed the link given in Part - 1.
Can u please help me??
Regards,
Rohit
What does your AndroidManifest.xml look like?
rohit.nagamalla
09-20-2010, 09:16 AM
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.blx.com"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<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>
</application>
</manifest>
This is how my manifest file looks like....
Scythe
09-20-2010, 10:20 AM
Yeah... you need to define your service, similar to:
<service android:name=".myservice.MyService"/>
Follow a tutorial like this one: http://developerlife.com/tutorials/?p=356
You also need to add permission for your service to access the internet.
rohit.nagamalla
09-20-2010, 10:29 AM
Hey thanks.. it worked...:o
gaara01
04-06-2011, 12:17 PM
i am new in android and i wanna create a restful service for coding an image interacting with a database for obtaining the name of an image ,in which part can i start and by what can i replace Ksoap2 in the example for having a restful web service.
Thanks
karamvan
05-11-2011, 12:59 AM
Hi Prashant,
I am just starting Android, i want to know how we could pass a java bean to ksoap2. My intention here is to insert a record into database. Can you please provide some samples?
thanks,
hi, extremely simple example of using service can be found there: http://maephv.blogspot.com/2011/08/android-notification-in-your.html
Powered by vBulletin® Version 4.1.7 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.