Check if the app is up-to-date

Following are steps reqired to check if your app is up-to-date (by means of getting its last versionCode from Cafebazaar). 

Create a package named com.farsitel.bazaar and copy IUpdateCheckService.aidl in it. After doing this your project structure will look like this:

Android Studio Screenshot

Now you should build your project and run according gradle task (named compileDebugAidl or something similar) to make Android Studio create needed files.

Imagine you want to ask the verisonCode of your app inside an activity named UpdateCheckActivity from Bazaar. In order to establish the connection between your app and Bazaar service, you need to create a new inner class (UpdateServiceConnection in this example) by implementing Android ServiceConnection class inside your activity class (UpdateCheckActivity in this example).

UpdateServiceConnectioninner class should implemet onServiceConnected() and onServiceDiconnected() methods. These methods are the callbacks, which will get the stub implementation of Bazaar service on connection. Then you just need to type cast them from Stubs to AIDL service implementation. To do so, you need to use:

IUpdateCheckService.Stub.asInterface((IBinder) boundService)

helper method. From here you will have a local service pointer to accessing data and methods.

Complete code of UpdateCheckActivity is as follows:

public class MainActivity extends Activity {

	IUpdateCheckService service;
	UpdateServiceConnection connection;
	private static final String TAG = "UpdateCheck";

	class UpdateServiceConnection implements ServiceConnection {
		public void onServiceConnected(ComponentName name, IBinder boundService) {
			service = IUpdateCheckService.Stub
					.asInterface((IBinder) boundService);
			try {
				long vCode = service.getVersionCode("your.app.packagename");
				Toast.makeText(MainActivity.this, "Version Code:" + vCode,
						Toast.LENGTH_LONG).show();
			} catch (Exception e) {
				e.printStackTrace();
			}
			Log.e(TAG, "onServiceConnected(): Connected");
		}

		public void onServiceDisconnected(ComponentName name) {
			service = null;
			Log.e(TAG, "onServiceDisconnected(): Disconnected");
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initService();
                ...
	}

	private void initService() {
		Log.i(TAG, "initService()");
		connection = new UpdateServiceConnection();
		Intent i = new Intent(
				"com.farsitel.bazaar.service.UpdateCheckService.BIND");
		i.setPackage("com.farsitel.bazaar");
		boolean ret = bindService(i, connection, Context.BIND_AUTO_CREATE);
		Log.e(TAG, "initService() bound value: " + ret);
	}

	/** This is our function to un-binds this activity from our service. */
	private void releaseService() {
		unbindService(connection);
		connection = null;
		Log.d(TAG, "releaseService(): unbound.");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		releaseService();
	}
}

The following inner class processes your request to bazaar and shows the response via a toast

class UpdateServiceConnection implements ServiceConnection {
	public void onServiceConnected(ComponentName name, IBinder boundService) {
		service = IUpdateCheckService.Stub
				.asInterface((IBinder) boundService);
		try {
			long vCode = service.getVersionCode("your.app.packagename");
			Toast.makeText(MainActivity.this, "Version Code:" + vCode,
					Toast.LENGTH_LONG).show();
		} catch (Exception e) {
			e.printStackTrace();
		}
			Log.e(TAG, "onServiceConnected(): Connected");
		}

		public void onServiceDisconnected(ComponentName name) {
			service = null;
			Log.e(TAG, "onServiceDisconnected(): Disconnected");
		}
	}
}

You should initialize this service via calling initService() when you want to check your apps update status (in this example, we called initService() in onCreate function of mainActivity class)

protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	initService();
	...
}

initService function, creates a new UpdateServiceConnection and a new Intent and binds them together to connect to bazaar

private void initService() {
	Log.i(TAG, "initService()");
	connection = new UpdateServiceConnection();
	Intent i = new Intent(
			"com.farsitel.bazaar.service.UpdateCheckService.BIND");
	i.setPackage("com.farsitel.bazaar");
	boolean ret = bindService(i, connection, Context.BIND_AUTO_CREATE);
	Log.e(TAG, "initService() bound value: " + ret);
}

releaseService function frees the resources that this service has used in order to free device's memory and resources

private void releaseService() {
	unbindService(connection);
	connection = null;
	Log.d(TAG, "releaseService(): unbound.");
}

You should call releaseService() when you are done with this service (in this example, we called it in onDestroy of mainActivity class)

protected void onDestroy() {
	super.onDestroy();
	releaseService();
	...
}

If there was an update for your app (The versionCode of your app on Bazaar was greater than the app installed on user's device), below line in above code will return its versionCode. Otherwise it will return -1. You should replace "your.app.packagename" with your app's package name.

long vCode = service.getVersionCode("your.app.packagename");

Depending on the version of your app the user is using and the versionCode of your app on Bazaar, you might take different decisions. For example you may notify the user to upgrade to the last version and if s/he agreed you can refer her/him to your app page on Bazaar. So s/he can upgrade to its last version. One last thing; checking updates for apps is being done from time to time and this API is working with the cached results.

Notice: This API can not be used to check updates of another apps, and when used in some app with package name a.b.c, it just can check if the app with a.b.c package name is updateable. Otherwise, it'll always return -1.

initService() in UpdateCheckActivity is the method which is called from onCreate() method to established the connection with Bazaar.