Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8782345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:32:30+00:00 2026-06-13T20:32:30+00:00

I try to create remote service for communicating between client and server. the main

  • 0

I try to create remote service for communicating between client and server.
the main idea is starting the service with my main activity
when the service will start, it will get the server address and port to open a socket.

I want it to be remote service so other applications will be able to use the same service.
the service will keep the connection alive by sending and receiving data to\from the server.
it will have the methods to read \ write Int and String.
in other words, implement the socket in and out methods…

the problem I’m facing for now, is understanding how the remote service works in android.
i started with creating a small service, that only have one method for returning an int.
here is some code:

ConnectionInterface.aidl:

    interface ConnectionInterface{
      int returnInt();
    }

ConnectionRemoteService.java:

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.Toast;

public class ConnectionRemoteService extends Service {
    int testInt;

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();
}



@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
}

@Override
public IBinder onBind(Intent intent) {
    return myRemoteServiceStub;
}   

private ConnectionInterface.Stub myRemoteServiceStub = new ConnectionInterface.Stub() {
    public int returnInt() throws RemoteException {
        return 0;
    }
};

}

and part of “onCreate” on my main activity:

final ServiceConnection conn = new ServiceConnection() {
        public void onServiceConnected(ComponentName name, IBinder service) {
            ConnectionInterface myRemoteService = ConnectionInterface.Stub.asInterface(service);
        }
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    final Intent intent = new Intent(this, ConnectionRemoteService.class);

later on i have an 2 OnClickListeners that binds and unbind the service:

bindService(intent, conn, Context.BIND_AUTO_CREATE);
unbindService(conn);

the one part that i missing here, is how do i use the methods in the service?
for now i have only 1 method that returns an int value.
how do i call it?
and how do i use other methods that gets values to the service?

thanks,
Lioz.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:32:31+00:00Added an answer on June 13, 2026 at 8:32 pm

    When you successfully bind to the service, onServiceConnected() is called with the service binder that you then use to communicate with the service. At the moment you are just putting that in a local variable myRemoteService. What you need to do is to store that in a member variable in your main activity. So define it like this in your main activity:

    private ConnectionInterface myRemoteService;
    

    and then, in onServiceConnected() do:

    myRemoteService = ConnectionInterface.Stub.asInterface(service);
    

    Later, when you want to use the service’s method(s), do something like this:

    // Access service if we have a connection
    if (myRemoteService != null) {
        try {
            // call service to get my integer and log it
            int foo = myRemoteService.returnInt();
            Log.i("MyApp", "Service returned " + foo);
        } catch (RemoteException e) {
            // Do something here with the RemoteException if you want to
        }
    }
    

    Make sure that you set myRemoteService to null when you have no connection to the service. You can do that in onServiceDisconnected().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a small remote task manager application [server/client] on LAN using WCF service
Anyone have any idea how to get the SACL's on a remote service using
I want to try create something like Zend's Server Pagecache. What I want to
I try to create a thread in QT, can declare, create and start it,
I am unable to get dependency injection to work for my remote service and
I'm trying to create a client for a WCF service that I have hosted
I was writing a simple AIDL based android remote service & a client to
I would like to implement WCF service with server and client certificates. When I
I try to access a virtual disk service (VDS) on a remote machine. After
I´m trying to create a client in C# to a web service which (I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.