NOTE:

NOTE: Of late, I have been getting requests for very trivial problems that many of you are facing in your day-to-day work. This blog is not to solve your "project" problems - surely not a "Support" site.
I just love to share my knowledge in my spare time and would appreciate any questions or feedback on the articles and code I have shared. I also do appreciate thought-provoking questions that would lead me to write more articles and share.
But please do not put your day-to-day trivial problems here. Even if you do, you most probably would not get a response here.
Thanks

Search This Blog

x

Thursday, 24 September 2009

Remote Service | Android Developer Tutorial (Part 9)


Services typically are required to run for a long time and hence should run in their own thread. Such services can be invoked by any number of clients who want to connect to the service, invoke a few methods on the service and finally release the service, probably to serve more clients or close down.

Here, I would like to introduce you to the concept of connecting to a remote service and the kind of support provided by the android platform for the same.

We have earlier seen how local services can be created and used. The difference between the two mainly is that the local service runs in the same process as the application that started it and hence the life of the local service is dependent on the life of the said application while remote service can run in its own process. This causes a challenge of inter-process communication. If one process wants to communicate with another process, the object that is passed between the two needs to be marshaled.

For this purpose, Android provides the AIDL (Android Interface Definition Language) tool that handles the marshaling as well as the communication.

The service has to declare a service interface in an aidl file and the AIDL tool will automatically create a java interface corresponding to the aidl file. The AIDL tool also generates a stub class that provides an abstract implementation of the service interface methods. The actual service class will have to extend this stub class to provide the real implementation of the methods exposed through the interface.

The service clients will have to invoke the onBind() method on the service to be able to connect to the service. The onBind() method returns an object of the stub class to the client. Here are the code related code snippets:

The AIDL file:
package com.collabera.labs.sai;

interface IMyRemoteService {

      int getCounter();
}

Once you write this AIDL file (.aidl) in eclipse, it will automatically generate the Remote interface corresponding to this file. The remote interface will also provide a stub inner class which has to have an implementation provided by the RemoteService class. The stub class implementation within the service class is as given here:

private IMyRemoteService.Stub myRemoteServiceStub = new IMyRemoteService.Stub() {
            public int getCounter() throws RemoteException {
                  return counter;
            }
      };
The onBind() method in the service class:
      public IBinder onBind(Intent arg0) {
            Log.d(getClass().getSimpleName(), "onBind()");
            return myRemoteServiceStub;
      }

Now, let us quickly look at the meat of the service class before we move on to how the client connects to this service class. My RemoteService class is just incrementing a counter in a separate thread. This thread is created in the onStart() method as this gets certainly called whether the service is connected to by a call to startService(intent). Please read the lifecycle of a service if this needs more clarity. Here are the over-ridden onCreate(), onStart() and onDestroy() methods. Note that the resources are all released in the onDestroy() method.

      public void onCreate() {
            super.onCreate();
            Log.d(getClass().getSimpleName(),"onCreate()");
      }
      public void onStart(Intent intent, int startId) {
            super.onStart(intent, startId);
            serviceHandler = new Handler();
            serviceHandler.postDelayed(myTask, 1000L);
            Log.d(getClass().getSimpleName(), "onStart()");
      }
      public void onDestroy() {
            super.onDestroy();
            serviceHandler.removeCallbacks(myTask);
            serviceHandler = null;
            Log.d(getClass().getSimpleName(),"onDestroy()");
      }

A little explanation: In the onStart() method, I created a new Handler object that will spawn out a new task that implements the Runnable interface. This thread does the job of incrementing the counter. Here is the code for the Task class – an inner class of the RemoteService class.

class Task implements Runnable {
      public void run() {
            ++counter;
            serviceHandler.postDelayed(this,1000L);
            Log.i(getClass().getSimpleName(), "Incrementing counter in the run method");
      }
}

An object of this Task class is passed to the serviceHandler object as a message that needs to be executed after 1 second. The Task class implements the run() method in which we repeatedly post the same message to the serviceHandler. Thus, this becomes a repeated task till all the messages in the serviceHandler queue are deleted by calling the removeCallbacks() method on the serviceHandler in the destroy() method of the RemoteService class.

Note that the onDestroy() method thus stops this thread and set the serviceHandler to null. This completes the implementation of the RemoteService class. The complete code is downloadable here.
Now coming to the client class - Here, for simplicity sake, I have put the start, stop, bind, release and invoke methods all in the same client. While in reality, one client may start and another can bind to the already started service.

There are 5 buttons one each for start, stop, bind, release and invoke actions. A client needs to bind to a service before it can invoke any method on the service.
Here are the start and the bind methods.

private void startService(){
     if (started) {
       Toast.makeText(RemoteServiceClient.this, "Service already started", Toast.LENGTH_SHORT).show();
     } else {
       Intent i = new Intent();
       i.setClassName("com.collabera.labs.sai", "com.collabera.labs.sai.RemoteService");
       startService(i);
       started = true;
       updateServiceStatus();
       Log.d( getClass().getSimpleName(), "startService()" );
      }
                 
  }

An explicit intent is created and the service is started with the Context.startService(i) method.
Rest of the code is to update some status on the UI. There is nothing specific to a remote service invocation here. It is on the bindService() method that we see the difference from a local service.

private void bindService() {
     if(conn == null) {
        conn = new RemoteServiceConnection();
        Intent i = new Intent();
        i.setClassName("com.collabera.labs.sai", "com.collabera.labs.sai.RemoteService");
        bindService(i, conn, Context.BIND_AUTO_CREATE);
        updateServiceStatus();
        Log.d( getClass().getSimpleName(), "bindService()" );
     } else {
       Toast.makeText(RemoteServiceClient.this, "Cannot bind - service already bound", Toast.LENGTH_SHORT).show();
     }
}

Here we get a connection to the remote service through the RemoteServiceConnection class which implements ServiceConnection Interface. The connection object is required by the bindService() method – an intent, connection object and the type of binding are to be specified. So, how do we create a connection to the RemoteService? Here is the implementation:

class RemoteServiceConnection implements ServiceConnection {
      public void onServiceConnected(ComponentName className,
      IBinder boundService ) {
remoteService = IMyRemoteService.Stub.asInterface((IBinder)boundService);
            Log.d( getClass().getSimpleName(), "onServiceConnected()" );
      }

      public void onServiceDisconnected(ComponentName className) {
            remoteService = null;
            updateServiceStatus();
            Log.d( getClass().getSimpleName(), "onServiceDisconnected" );
      }
};

The Context.BIND_AUTO_CREATE ensures that a service is created if one did not exist although the onstart() will be called only on explicit start of the service.

Once the client is bound to the service and the service has already started, we can invoke any of the methods that are exposed by the service. Here we have only one method and that is getCounter(). In this example, the invocation is done by clicking the invoke button. That would update the counter text that is below the button. 

Let us see the invoke method:

private void invokeService() {
     if(conn == null) {
        Toast.makeText(RemoteServiceClient.this, "Cannot invoke - service not bound", Toast.LENGTH_SHORT).show();
     } else {
        try {
            int counter = remoteService.getCounter();
            TextView t = (TextView)findViewById(R.id.notApplicable);
            t.setText( "Counter value: "+Integer.toString( counter ) );
            Log.d( getClass().getSimpleName(), "invokeService()" );
        } catch (RemoteException re) {
            Log.e( getClass().getSimpleName(), "RemoteException" );
        }
     }
}    

Once we use the service methods, we can release the service. This is done as follows (by clicking the release button):

private void releaseService() {
      if(conn != null) {
            unbindService(conn);
            conn = null;
            updateServiceStatus();
            Log.d( getClass().getSimpleName(), "releaseService()" );
      } else {
            Toast.makeText(RemoteServiceClient.this, "Cannot unbind - service not bound", Toast.LENGTH_SHORT).show();
      }
}

Finally we can stop the service by clicking the stop button. After this point no client can invoke this service.

private void stopService() {
      if (!started) {
            Toast.makeText(RemoteServiceClient.this, "Service not yet started", Toast.LENGTH_SHORT).show();
      } else {
            Intent i = new Intent();
            i.setClassName("com.collabera.labs.sai", "com.collabera.labs.sai.RemoteService");
            stopService(i);
            started = false;
            updateServiceStatus();
            Log.d( getClass().getSimpleName(), "stopService()" );
      }
}
These are the basics of working with a remote service on Android platform. All the best!


Addendum (updated on 6 Jan 2011) – based on many questions related to Remote services. If the client accessing the remote service is in a separate package or application, it has to include the .aidl file along with the package structure as in the Service provider.
I have written a sample remote client in a completely different application, while the remote service is the same one provided above. You may download the client app here.


NOTE on Code for download. The complete Server and client code is downloadable here. While the client alone in a separate app is here. If this client code has to work, you have to download the previous server code as well, and then use the client to start, stop, bind etc.

85 comments:

  1. Hi SaiGeeta ,

    Thanks a lot for your tutorial.I have one query like,

    I created a remote service .lets take like ftpserver ,it will run irresecptive of any application.Now i want to use that service in another application itself ,please let me know how we can achieve this ?

    The remoter service example given gives a picture of how service is created and used in the same applicaation (ofcourse with a diffreent thread for the service ) ,but i want to use that service in the other application.

    ReplyDelete
  2. Hi,
    you have to follow the same steps mentioned in this tutorial irrespective of whether the remote service runs in the same or a separate application.
    1. To create teh FTP service as a remote service, you must create the AIDL file declaring the methods that can be invoked by clients.
    2. You have to extend the stub that is auto generated in your remote service.
    3. Apart from that you have to implement the life cycle methods of the service.
    4. Then, the service can run independently
    5. From the client program, you must create a connection object to the remote service
    6. Invoke the services declared in the remote interface by binding to the service.
    Basically what I am trying to say is that you must follow the same steps but can separate out the remote service program into one application and the client program into another application and it will work.

    ReplyDelete
  3. Hi SaiGeetha

    I need one more help like ,i am trying to implement TCP/IP sockets in Android ,how to achive the same in Android ,Can you please provide the code if you have done .Also request you to add more on port forwarding stuff if you have .

    Thanks in Advance

    ReplyDelete
  4. Dear Sai Geetha,

    I have separate service and client application, but I got an error message while call on service function, the message: Binder invocation to an incorrect interface. Maybe you have a some clue for me?

    Thanks

    ReplyDelete
  5. Hi Slashwaves,

    I think you have probably created 2 interfaces in the two packages. They become different interfaces once the packages have changed. Hence you are getting the error. Ensure you use the same package interface in both the applications. It should work.

    ReplyDelete
  6. Hi
    Not sure if you are familiar with "Not Call Log" application but I was wondering how it is done ... basically the application listens to outgoing call event and upon ending, it switches to HOME screen or any other that you choose ... 2 questions:
    1. Would you use service to listen for those events or ContentObserver?
    2. How do you switch between applications manually?

    Thanks in advance
    Amer

    ReplyDelete
  7. Hi SaiGeeta,
    I tried this example it works fine but when I tried it out in my example the service is not getting called. It say "unable to find start service intent NameOfTheService" not found error. Can you please tell why is this happening. I have made an entry in the manifest file. But still not working.
    Thanks

    ReplyDelete
  8. Can you post the manifest file please?

    ReplyDelete
  9. Hi mad_max,
    if you download my source code - link available in the article above, you will get the manifest file too. Let me know if you have any problem

    ReplyDelete
  10. Hi thanks for your tutorial,
    and i have one question.
    you said, To use remote Service between two Apps, i should make aidl file`s package same.
    but i don`t get it.
    Because, say i have two Apps which are in "com.clnt" , "com.srv",
    so each aidl file is in each package, then how can i make them to be in same package?

    so please let me know
    1. there should be aidl file in each App to use remote on different Apps?
    2. if that so, how can i do that?

    Thanks.

    ReplyDelete
  11. Hi Sai Geetha,

    I'm Abdul Muyeed here. The blog is too good. I have one doubt. We have 2 remote services running and one activity to control them. The problem is both the remote services are running with the same PID. However the activity is running in different PID. I want both the services to run on different PID. How to achieve this?

    Thanks and Regards,
    Abdul Muyeed M

    ReplyDelete
  12. Hi KeniF,Thanks for pointing out,...True. onStart() is not called from a bindService() method. It is called only from startService(). Will correct in my post.
    Will also make necessary changes in the code.
    Thanks.

    ReplyDelete
  13. If you wan to start a service in a separate process (which would ensure a separate PID), you need to explicitly set the process name to a different name for one of the services. The other service will run in the default process.
    How to set the process name?
    In the Android.manifest xml, where you define your service, you can have a element as shown below:

    <service android:enabled=["true" | "false"]
    android:exported[="true" | "false"]
    android:icon="drawable resource"
    android:label="string resource"
    android:name="string"
    android:permission="string"
    android:process="string" >
    . . .
    </service>

    Here you set a process name - any string and that will ensure that the service runs in a separate process.
    However a warning: creating multiple processes must be avoided as it is very resource intensive and hence should be done only if it is absolutely necessary.
    Hope this helps

    ReplyDelete
  14. Hi Geeta,


    Really itz great that your are contributing to open source.

    Could you please clarify the following:

    In your example you haven't created the service as remote process which can be specified in the manifest file with process attribute.Your service processname is same as application process name ie default process.As your service runs in the same process as application process (this can be viewed in the DDMS in eclipse envment)it's nothing but a local process.But you use RPC.I have also gone through API demos example
    there the remote service is invoked as separate process and so RPC is acceptable.Could you please clarify

    Tilak

    ReplyDelete
  15. Hi,
    Nice tutorial.
    I am working on Background Service.I have one problem.I created one activity and one service.
    I invoked the service using startService(intent) method.I want to know when the service is finishing its task.while the background service finishes its task,i have to invoke a method in the activity.

    How can i do that?....please help me....

    ReplyDelete
  16. Hi,
    Nice tutorial.
    I am working on Background Service.I have one problem.I created one activity and one service.
    I invoked the service using startService(intent) method.I want to know when the service is finishing its task.while the background service finishes its task,i have to invoke a method in the activity.

    How can i do that?....please help me....

    ReplyDelete
  17. Hi,

    I am unable to get codes from your blog ,when i click on link to get source code it doesn't work,please update it.

    Thanks

    ReplyDelete
  18. Hello Geeta,

    I have a problem. In the way you are getting the connection object of the service, I tried getting connection objects for two services one after another in sequence. But for some reason, i only get the connection object for one. The other is always null. Any comments / suggestions?

    Anyways, great work at your blog. I am a regular visitor to your blog.

    Thanks & Regards
    SM

    ReplyDelete
    Replies
    1. Hai geeta please help me to how to transefering files from pc to andoid phone and vice versa. please send the source code also

      @rambabu

      Delete
  19. hi!

    how do i bind a remote service i don't have any sources for (i.e. it is installed on my device)?

    i've written a remote service other applications should be able to query (via broadcast intents).

    is this any different than binding a remote service for which i do have the sources in my eclipse project?

    ReplyDelete
  20. first of all thank you for this amazing tutorial.

    I am trying to develop an application and don't know where to start.

    the application will do following:
    - It will play music 5 known times a day (like alarm but times differ day by day),
    - It will have a widget to show how many minutes left to next music to play,
    - It will have an activity to altar alarms, add new alarms etc.

    The question is how my design should be? Is Remote Service necessary? What will update widget for every minute? I want my alarms run even though activity is killed.

    Thanks

    ReplyDelete
  21. Hi
    Can you please update the link, I can't download source codes.
    Thanks

    ReplyDelete
  22. Hi SaiGeetha,

    The tutorial example works great.
    I created a example having the service in different application (UI less) and the RemoteServiceClient.java in another application. It does not recognise the IMyService and throws me error in compilation.

    Please help me

    ReplyDelete
  23. Hi

    Its a very nice article but i have the following scenario...
    I have developed an android service. Any android app can use it's API's to get some kind of news updates from this service. I want to distribute this service so that any android app on the phone can use this service. My questions here are : 1: When some android application try to use its API on the phone and suppose that service is not available on the phone then what will happen ? 2: How will android application developer will make sure that the service is available on the phone ? 3: Does application developer has to bundle service with his application ? If yes then wont be there multiple instances of same service on phone if multiple application contains same service on the phone ?

    Thx
    Dalvin

    ReplyDelete
  24. Hi SaiGeetha,

    So which is the client and which is the server as both are named RemoteService and RemoteServiceClient? Thanks

    ReplyDelete
  25. Hi,
    i have a question, i have a service which reads the lines from a file and continuously feed it to an activity.This all is a continuous process no breaks in between.Service will keep on reading lines from file and activity keep on working on those points....
    Please help me out how to implement this...

    ReplyDelete
  26. excellent tutorial.

    not only it is working right away and is up to date, but it is well explained.

    Good job!

    greetings from Poland,
    mike

    ReplyDelete
  27. its very informative tutorial, i have query How can we connect service from different project to activity in different project
    help me how can i achieve this
    i will appreciate

    ReplyDelete
  28. please help me out how can i communicate activity from one project to service in another project by using .aidl
    but u tell me is there some house keeping is involve in manifest file


    i will appreciate

    ReplyDelete
  29. hi sai geetha i have doubt regarding with notification service.how to get notifications from 3rd party applications server to android app.Does this posible in android 2.1.For 2.2 google introduces c2dm service.

    ReplyDelete
  30. I meam to ask how can i implement pushnotifications service in android

    ReplyDelete
  31. Sai Geeta,

    I downloaded and ran your project, but when I press the "Invoke" button, the following error occurs in LogCat:

    Unable to start service Intent { cmp=com.collabera.labs.sai/.RemoteService }: not found

    The app subsequently force closes on a NullPointerException due to the service being null. This is the exact same problem I'm experiencing, and I had downloaded your project in the hope that it could show me where I was going wrong. Do you have any idea why it is unable to find the service?

    ReplyDelete
  32. Hi Robert,

    You need to invoke the remote service after you start and bind it. So the order of actions needs to be: start -> bind -> invoke -> release -> stop.

    If you go through the tutorial, I hope you will understand why the order is so.

    ReplyDelete
  33. HI Geetha,
    thanks for this tutorial. Its really helpful for me. I downloaded project and ran it. I am getting the same problem what Robert is getting Even though I have follow these steps start -> bind -> invoke -> release -> stop.
    I tried to removed all the previous project from the phone and ran only this project then also it didn't work.
    Please let me know what I am missing here?

    ReplyDelete
  34. Thanks a lot for the very useful tutorial!

    ReplyDelete
  35. Hi Sai,
    I want to compile an aidl file in android source(mydroid) and want to generate an System.img.Because the aidl file is needed by all the users of our group.Instead of writing in every application,I want to make a Centralize one.
    Where can place this aidl file to compile and generate System.img?

    ReplyDelete
  36. HI Geetha,
    thanks for this tutorial. I am getting the same problem what Robert and Hame is getting Even though I have follow these steps start -> bind -> invoke -> release -> stop.
    Please let me know what I am missing here?

    ReplyDelete
  37. Sai:

    Thank you for providing this information, it was very helpful.

    To those having the same problem as Robert, Hame and Sab (service not found), make sure to use the correct download link. It is provided in the paragraph that begins with the sentence "Note that the onDestroy() method thus...".

    ...perhaps Sai is also sly, and testing to see how careful we read ;)

    ReplyDelete
  38. Hi I see three of you saying that the code is not working. I hope you are downloading the right source code. there are two priovided here. One consisting of the service and the service client in one package and the other consisting only of the client that can remotely (in a separate project0 invoke the service. For this the first one should have been downloaded and started and only then the second one will work.

    ReplyDelete
  39. i am facing issues for setting alarm through android code.
    I gone through most of the documents on net about alarmmanager and how to use it but nothing worked.

    please find the code am using ,is it the right way for setting alarm .
    for eg: i am setting it for 8 AM morning .but its not working i am strugling on it for 3 hrs.
    i am testing it on emulator(emulator time 7.50am).
    the alarm is not starting at 8 am its just satrting at the apllication lauch time itself without waiting 10mnts
    :(
    Calendar calendar = new GregorianCalendar(); or // Calendar calendar = new Calendar();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY,8);
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

    Thanks you

    ReplyDelete
  40. Hello Geetha.. i found your tutorial very helpfull... thank u... am new to this android programming... could u please send me one small project in android which on clicking the download button downloads the entered data in the form, into the database..

    ReplyDelete
  41. This comment has been removed by the author.

    ReplyDelete
  42. Hi Geeta,
    I am following these steps--start -> bind -> invoke still I am getting problem in invoking,Please help..
    Thanks..

    ReplyDelete
  43. Hi,

    Great tutorial, but would like to know and discuss why and when to service over asynctask for Restful Client on android.

    Regards,
    Amol

    ReplyDelete
  44. Thanks a lottt Geetha!! You are really an Angle for me!! I got exactly what I wanted!! Thankz a tonn again...
    Mayuri Ruparel

    ReplyDelete
  45. nice tutorial for me who newbie in here,...
    thanks

    ReplyDelete
  46. Hi Sai Geetha
    I am a newbie and trying to find the way to separate client and server. I am stumble across the net and found your program. It is real nice to have someone discuss and give out the sample code. I did try use your program. However, here I got confused. I am able to bind to the service even I did not start service on another machine which I ran your server program. So, I am not sure what I did not understand. I am trying to send message from one machine to another using AIDL. Please kindly respond to this message. I am struck on this for more than a week now. Very frustrated. Please help - will be very much appreciated.

    Saow

    ReplyDelete
  47. Nice tutorials, I really appreciate them.

    I've been studying Android development for a while, haven't produced nothing commercial yet, hope to do it some day...

    I think that you, being a professional, participated on some projects that weren't about building stand-alone apps... meaning that in some way, the app used the Cloud (sending a file by HTTP, getting some data from a server, etc.)

    Hope you consider this domain enough "thought-provoking" to create a tutorial, I know that I and many newbies, would definitely appreciate one.

    ReplyDelete
  48. Thank you for providing the code to download. It made it a lot easier to understand and truly learn! Thanks for the great tutorials. Keep it up!

    ReplyDelete
  49. Did you copy code from this Blog : http://www.anddev.org/remote_service_tutorial-t8127.html

    ReplyDelete
    Replies
    1. i think not because he is refering to this page !

      Delete
  50. In the implementation of your service class you have missed declaring the exported property in the manifest file which causes startService SecurityException.

    I stumpled upon http://groups.google.com/group/android-developers/browse_thread/thread/a05cf92a35f1e180

    and found that RemoteService manifest file should have



    i.e set the exported property to true atleast if my client and server are running in 2 different packages.

    ReplyDelete
  51. Somehow
    service android:name=".RemoteService" android:exported="true">
    got deleted from mypost

    ReplyDelete
  52. "If you wan to start a service in a separate process (which would ensure a separate PID), you need to explicitly set the process name to a different name for one of the services. The other service will run in the default process."

    I'm trying to run it between two apps using your ExternalClient and your RemoteService, but it fails. I need to configure some android.permission to do this, or it fails because some reference problem?

    I have tried to configure process name, but it fails too.

    ReplyDelete
  53. can i put clickable buttton on status bar? how?

    ReplyDelete
  54. Nice blog, just started with android services and the google dev documentation really isnt much fun, these are nice example of how to use them in code.

    Thanks!

    ReplyDelete
  55. By read your post I got good idea about public access specifier.By using this public access specifier the data can be accessed by outside the class.This is one of the valuable coding.Android app developers

    ReplyDelete
  56. In one word, awsome tutorial. I love your works.

    ReplyDelete
  57. I noticed one problem (when i did my own background service) during the "onStart()" method, you initialise the handler and start a postdelayed run... The problem with this is that onStart is called every time something does a startService().

    The point being that if you have serveral components trying to get to your service and they all do a startServices, you'll end up with multiple call-backs, so instead of run() being called every 5 seconds, it'll get called once every 5 seconds for each startService call.

    ReplyDelete
  58. hello... many thanks for this article...
    i need to create webservice, that is calling method from other platforms(from .net)...
    do u have any example about it simply to add two numbers...
    otherwise do u know any link related to it...
    thanks in advance...

    ReplyDelete
  59. Hi,

    Blinking issue found while playing two video in single activity.

    Here is my code:

    VideoView video1=(VideoView)findViewById(R.id.videoView1);
    video1.setVideoPath("/sdcard/sample1_10fps.mp4");
    VideoView video2=(VideoView)findViewById(R.id.videoView2);
    video2.setVideoPath("/sdcard/sample3_10fps.mp4");
    video1.start();
    video2.start();

    Thanks,
    Kamal

    ReplyDelete
  60. hi sai geetha,
    will u explain what is the Parcelable interface...and where will we use that interface........

    ReplyDelete
  61. Hi sai Geetha,

    I have download rar of above tutorial.but can u tell me how to create AIDL file.

    ReplyDelete
  62. Hello madam,
    Android is new for me.I want to create simple web service and calling or accessing that service through android application what i do? can u provide any source code and steps with explanation to building such application.plz tell me best way to doing such application.plz Help me.Hope You will think about it .plz rpy as early as possible. Thank You in advanced.

    ReplyDelete
  63. This article is very informative and useful for all of us. The article has been written very well. I appreciate your work. Thanks for posting.
    dissertation writing assistance

    ReplyDelete
  64. hi madam sai geetha ur blog is very help full to me

    ReplyDelete
  65. Your post is awesome. You have explained it very well. I am willing to read posts like this one. Thanks for posting.
    dissertation writing software

    ReplyDelete
  66. hello sai geeta i've downloaded the code for both server and client, client app has some problems of package naming in i.setclass() methode plz correct it. the only problem remaining is that i get null pointer exception when i press invoke button. i tried by buttons start bind and then invoke but get null pointer exception instead of counter value

    ReplyDelete
  67. Hi Geetha,
    I have read your post Number of times.
    I have two application. I want to Inter Process communication between two application Using services, I have achieve this task using AIDL, but I want to do using services. pleas me know, how should I achieve this.
    please also add me in Linkedin. emran.hamza85@gmail.com

    ReplyDelete
  68. Hi Mam,
    your post is easy to understand thanks Alot 4 being posting.

    ReplyDelete
  69. int counter = remoteService.getCounter();

    Getting NullPointerException on that line

    ReplyDelete
  70. Hello Geetha,

    I must appreciate the simplicity and ease towards understanding the code. Keep up the good work.

    Thank you.

    ReplyDelete
  71. Hi Geetha,
    well explained service bounding using aidl, it helped me a lot in understanding.
    I also tried using your same example where as client being in a different application. I am able to start or stop the service, but, have below issues,
    1. Not able to bind to the server, from logs i see this,

    09-11 11:05:22.528: I/dalvikvm(1175): Could not find method com.example.aidlexample.IMyRemoteService$Stub.asInterface, referenced from method com.example.aidlclient.AidlClient$RemoteServiceConnection.onServiceConnected
    09-11 11:05:22.528: W/dalvikvm(1175): VFY: unable to resolve static method 3058: Lcom/example/aidlexample/IMyRemoteService$Stub;.asInterface (Landroid/os/IBinder;)Lcom/example/aidlexample/IMyRemoteService;
    09-11 11:05:22.537: D/dalvikvm(1175): VFY: replacing opcode 0x71 at 0x0002
    09-11 11:05:22.537: W/dalvikvm(1175): VFY: unable to find class referenced in signature (Lcom/example/aidlexample/IMyRemoteService;)
    09-11 11:05:22.547: W/ActivityManager(159): Unable to start service Intent { cmp=com.example.aidlexample/.RemoteService }: not found
    2. when try to invoke the service, i get a force close with below logs,

    09-11 11:06:03.998: E/AndroidRuntime(1175): FATAL EXCEPTION: main
    09-11 11:06:03.998: E/AndroidRuntime(1175): java.lang.NoSuchMethodError: com.example.aidlexample.IMyRemoteService.getCounter
    09-11 11:06:03.998: E/AndroidRuntime(1175): at com.example.aidlclient.AidlClient.invokeService(AidlClient.java:149)
    09-11 11:06:03.998: E/AndroidRuntime(1175): at com.example.aidlclient.AidlClient.access$4(AidlClient.java:144)
    09-11 11:06:03.998: E/AndroidRuntime(1175): at com.example.aidlclient.AidlClient$1.onClick(AidlClient.java:68)
    09-11 11:06:03.998: E/AndroidRuntime(1175): at android.view.View.performClick(View.java:4084)
    09-11 11:06:03.998: E/AndroidRuntime(1175): at android.view.View$PerformClick.run(View.java:16966)
    09-11 11:06:03.998: E/AndroidRuntime(1175): at android.os.Handler.handleCallback(Handler.java:615)
    09-11 11:06:03.998: E/AndroidRuntime(1175): at android.os.Handler.dispatchMessage(Handler.java:92)
    09-11 11:06:03.998: E/AndroidRuntime(1175): at android.os.Looper.loop(Looper.java:137)
    09-11 11:06:03.998: E/AndroidRuntime(1175): at android.app.ActivityThread.main(ActivityThread.java:4745)
    09-11 11:06:03.998: E/AndroidRuntime(1175): at java.lang.reflect.Method.invokeNative(Native Method)
    09-11 11:06:03.998: E/AndroidRuntime(1175): at java.lang.reflect.Method.invoke(Method.java:511)
    09-11 11:06:03.998: E/AndroidRuntime(1175): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    09-11 11:06:03.998: E/AndroidRuntime(1175): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    09-11 11:06:03.998: E/AndroidRuntime(1175): at dalvik.system.NativeStart.main(Native Method)

    If you can help in identifying the issue it would help in making my understanding even good about aidl,
    -Thanks & regards,
    Manju

    ReplyDelete
  72. This comment has been removed by the author.

    ReplyDelete
  73. You are an inspiration to Indian women :)

    ReplyDelete
  74. Hi Sai Geetha.

    Thanks for your topic.

    I'm new in Android and have a problem, but my English is very very terrible.
    My problem about: I usually receive sms from number "0123456789" day by day, it's a obtrusion. I want to create a small first app in Android but i do it myself. I read about this topic in:

    http://stackoverflow.com/questions/4637821/how-to-analyze-incoming-sms-on-android

    https://github.com/jewel/unnagroid

    It gave me so much time but the result is nothing. I hope so You can help me to create a SERVICE for it.

    Waiting your answer day by day!
    Email: dangtuanson_vn@yahoo.com

    ReplyDelete
  75. HI Geeta,

    I want to start the background service from the AppWidget which is on Home screen .I try for the same but not able to implement it.Can you help ?

    ReplyDelete
  76. hai geetha....
    im new to android programming....
    hw cn i update the datas in mobiledatabase(sqlite) to a wesite???

    ReplyDelete
  77. Hai geetha! i implement your services with notifications example! my question is this .

    you said that local services's life is dependent on the activity that started it. but when i start the service from my activity and then close my activity but the service was still running. now why is that? if i am mistaking please help me!

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  78. Hi geetha,

    Nice tutorial, I was just wondering what is the updateServiceStatus method about?

    ReplyDelete
  79. Thanks a lot for providing such a nice information. It will be really useful for android developers. The way you expressed your code stuns good. My friend who is an Android developer has the great passion in developing the android application. I will take forward this blog to my friend, so that he will get benefited by reading your blog.

    ReplyDelete
  80. Is it possible to create aidl in 1 app and access it from other app.
    can u share example.
    i tried by making 1 app[service] as lib and by adding it to my other app but still security exception is there.

    ReplyDelete