Monday, June 3, 2013

Creating an Android library with external service and calling it from client application

1. Service needs to be declared with action filter e.g:
    <service android:name="com.example.RemoteService"
        android:process=":remote"
        android:exported="true">
              <intent-filter><action android:name="com.example.RemoteService"></action></intent-filter>
        </service>

2. Starting the service from the client app:
    Intent i = new Intent("com.example.RemoteService");
PackageManager packageManager = getPackageManager();
List<ResolveInfo> services = packageManager.queryIntentServices(i, 0);
Log.i(Prefs.TAG, "services.size(): " +  services.size());


if (services.size() > 0) {
   ResolveInfo service = services.get(0);
   i.setClassName(service.serviceInfo.packageName, service.serviceInfo.name);
   i.setAction("com.example.RemoteService");
           ComponentName cn = startService(i);
   Log.d(Prefs.TAG, "started: " + cn.getClassName());
}

No comments:

Post a Comment