Print
Check user's login status
Notice: This service is available in Bazaar v6.5+
In the following steps, you will learn how to get user's login status from Bazaar.
Create a package named com.farsitel.bazaar
and copy ILoginCheckService.aidl in it. After doing this your project structure should look like this:
Imagine you want to ask the user's login status inside an activity named LoginCheckActivity
from Bazaar. In order to establish the connection between your app and Bazaar's service, you need to create a new inner class (LoginCheckServiceConnection
in this example) by implementing Android ServiceConnection class inside your activity class (LoginCheckActivity
in this example).
LoginCheckServiceConnectioninner
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:
ILoginCheckService.Stub.asInterface((IBinder) boundService);
helper method. From here you will have a local service pointer to accessing data and methods.
Complete code of LoginCheckActivity
is as follows:
public class LoginCheckActivity extends Activity {
ILoginCheckService service;
LoginCheckServiceConnection connection;
private static final String TAG = "LoginCheck";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initService();
}
private void initService() {
Log.i(TAG, "initService()");
connection = new LoginCheckServiceConnection();
Intent i = new Intent(
"com.farsitel.bazaar.service.LoginCheckService.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();
}
public class LoginCheckServiceConnection implements ServiceConnection {
private static final String TAG = "LoginCheck";
public void onServiceConnected(ComponentName name, IBinder boundService) {
service = ILoginCheckService.Stub
.asInterface((IBinder) boundService);
try {
boolean isLoggedIn = service.isLoggedIn();
Toast.makeText(MainActivity.this, "isLoggedIn:" + isLoggedIn,
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");
}
}
}
The following inner class processes your request to bazaar and shows the response via a toast
public class LoginCheckServiceConnection implements ServiceConnection {
private static final String TAG = "LoginCheck";
public void onServiceConnected(ComponentName name, IBinder boundService) {
service = ILoginCheckService.Stub
.asInterface((IBinder) boundService);
try {
boolean isLoggedIn = service.isLoggedIn();
Toast.makeText(MainActivity.this, "isLoggedIn:" + isLoggedIn,
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 login 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 LoginCheckServiceConnection
and a new Intent
and binds them together to connect to bazaar
private void initService() {
Log.i(TAG, "initService()");
connection = new LoginCheckServiceConnection();
Intent i = new Intent("com.farsitel.bazaar.service.LoginCheckService.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 the user is logged in, below line in the code above:
boolean isLoggedIn = service.isLoggedIn();
will return true, otherwise it will return false.
initService() in LoginCheckActivity
is the method which is called from onCreate() method to established the connection with Bazaar.
Was this content helpful?
Write your comment here...
If you have any comment on this content or any idea to make it better, use this form to send us your comment.
Publish Date: 1400-03-28 21:21
Latest Update: 1400-04-10 19:55