Hi All,

Currently I am trying to access the Service which is implemented in
one application and accessed by another application. But it is not
happening.

Below I explain the code for reference, Please check it out.
Consider the implemented Service application as "Server" and accessing
the Service application as "Client".

IRemoteService.aidl:(This class is append in both the Server and
Client application)
interface IRemoteService {
        int getCounter();
}

Server Application:
RemoteService:(This is Service implemented class)
public class RemoteService extends Service {

        private Handler serviceHandler;
        private int counter;
        private Task myTask = new Task();


        @Override
        public IBinder onBind(Intent arg0) {
                Log.d(getClass().getSimpleName(), "onBind()");
                return remoteServiceStub;
        }

        private IRemoteService.Stub remoteServiceStub =
                new IRemoteService.Stub() {
                        public int getCounter() throws RemoteException {
                                return counter;
                }};

        @Override
        public void onCreate() {
                super.onCreate();
                Log.d(getClass().getSimpleName(),"onCreate()");
        }

        @Override
        public void onDestroy() {
                super.onDestroy();
                serviceHandler.removeCallbacks(myTask);
                serviceHandler = null;
                Log.d(getClass().getSimpleName(),"onDestroy()");
        }

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

        class Task implements Runnable {
                public void run() {
                        ++counter;
                        serviceHandler.postDelayed(this,1000L);
                        Log.i(getClass().getSimpleName(),
                                        "Incrementing counter in the run 
method");
                }
        }
}
Manifest file for Server Application:
<service android:name="RemoteService" android:exported="true"
android:enabled="true">
</service>

Client Application:
RemoteServiceClientDemo : (This class accessing the method in the
interface)
public class RemoteServiceClientDemo extends Activity {

        private IRemoteService remoteService;
        private boolean started = false;
        private RemoteServiceConnection conn = null;

        /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button start = (Button)findViewById(R.id.startButton);
        Button stop = (Button)findViewById(R.id.stopButton);
        Button bind = (Button)findViewById(R.id.bindButton);
        Button release = (Button)findViewById(R.id.releaseButton);
        Button invoke = (Button)findViewById(R.id.invokeButton);

        start.setOnClickListener(new OnClickListener() {
                public void onClick(View v){
                        startService();
                }
        });

        stop.setOnClickListener(new OnClickListener() {
                public void onClick(View v){
                        stopService();
                }
        });

        bind.setOnClickListener(new OnClickListener() {
                public void onClick(View v){
                        bindService();
                }
        });

        release.setOnClickListener(new OnClickListener() {
                public void onClick(View v){
                        releaseService();
                }
        });

        invoke.setOnClickListener(new OnClickListener() {
                public void onClick(View v){
                        invokeService();
                }
        });
    }

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

    private void stopService() {
        if (!started) {
                Toast.makeText(RemoteServiceClientDemo.this,
                "Service not yet started", Toast.LENGTH_SHORT).show();
        } else {
                Intent i = new Intent();
                i.setClassName("com.demo",
                        "com.demo.RemoteService");
                stopService(i);
                started = false;
                updateServiceStatus();
                Log.d( getClass().getSimpleName(), "stopService()" );
        }
    }

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

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

    private void invokeService() {
        if(conn == null) {
                Toast.makeText(RemoteServiceClientDemo.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" );
                }
        }
    }

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

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

      private void updateServiceStatus() {
          String bindStatus = conn == null ? "unbound" : "bound";
          String startStatus = started ? "started" : "not started";
          String statusText = "Service status: "+
                                                        bindStatus+ ","+ 
startStatus;
          TextView t = (TextView)findViewById( R.id.serviceStatus);
          t.setText( statusText );
        }

      protected void onDestroy() {
          super.onDestroy();
          releaseService();
          Log.d( getClass().getSimpleName(), "onDestroy()" );
      }
}

Manifest file for Client application:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android";
      package="com.demo"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/
app_name">
                <activity android:name=".RemoteServiceClientDemo"
                  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>
        <uses-sdk android:minSdkVersion="3" />
</manifest>

Thanks
Hari

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to