banner



How To Wait For A Service To Bind Andriod Studio

Android Local Leap Service Case with Binder and ServiceConnection

July 17, 2015

On this page we will provide android local bound service case with Folder and ServiceConnection. Bound service is bound with other application and responds to the clients. Service class has onBind() method which is overridden and that returns IBinder. For local bound service where service use volition exist private and are in same application nosotros can create an inner class in our service course with public methods that can be accessed by clients. To bind and unbind service from primary thread of activity we call bindService() and unbindService() respectively. Hither we will provide a consummate example. In our service we volition create a method to get current engagement that will be called by client. Our service should be configured in AndroidManifest.xml within awarding tag.

<service android:name=".MyLocalService"/>            

Bound Service

A bound service is a service which allows other applications to demark and interact with it. This is the implementation of Service where we take to override onBind() that volition return IBinder.

public IBinder onBind(Intent intent) {      return binder; }            

To demark and unbind service we use the post-obit methods.

bindService(Intent service, ServiceConnection conn, int flags): Binds the service. We demand to pass intent which is instantiated using our service class. Pass the ServiceConnection case created for service. flag is context value which can exist as follows

BIND_ABOVE_CLIENT, BIND_ADJUST_WITH_ACTIVITY, BIND_ALLOW_OOM_MANAGEMENT, BIND_AUTO_CREATE, BIND_DEBUG_UNBIND, BIND_IMPORTANT, BIND_NOT_FOREGROUND, BIND_WAIVE_PRIORITY          

unbindService(ServiceConnection conn): Unbinds the bound service from other application for the given connection.

We telephone call the higher up methods from the main thread of our activeness.

Binder

android.bone.Folder implements android.bone.IBinder. If our client and service are in same application, we tin implement our ain Folder. To employ information technology we can create public inner course which will extend Binder inside our service and finally return the instance of this inner class past onBind() method. Extending Folder works if our service is individual to our application that is known as local bounden.

ServiceConnection

android.content.ServiceConnection is an interface which is used to monitor the country of service. Nosotros need to override post-obit methods.
onServiceConnected(ComponentName name, IBinder service) : This is called when service is connected to the application.
onServiceDisconnected(ComponentName name) : This is chosen when service is disconnected.

Complete Example

MainActivity.java

package com.concretepage; import java.util.Appointment; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.bone.Packet; import android.os.IBinder; import android.view.View; import android.widget.Toast; import com.concretepage.MyLocalService.LocalBinder; public form MainActivity extends Activity {     MyLocalService localService;     private boolean isBound = fake;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);     }     @Override     protected void onStart() {         super.onStart();         Intent intent = new Intent(this, MyLocalService.class);         bindService(intent, connection, Context.BIND_AUTO_CREATE);     }     @Override     protected void onStop() {         super.onStop();         if (isBound) {         	unbindService(connection);         	isBound = fake;         }     }     public void dispalyDate(View 5) {     	if (isBound) { 	    	Date date = localService.getCurrentDate(); 	        Toast.makeText(this, String.valueOf(date), Toast.LENGTH_SHORT).show();     	}     }     individual ServiceConnection connexion = new ServiceConnection() {         @Override         public void onServiceConnected(ComponentName className, IBinder service) { 	        LocalBinder folder = (LocalBinder) service; 	        localService = binder.getService(); 	        isBound = true;         }         @Override         public void onServiceDisconnected(ComponentName arg0) {         	isBound = simulated;         }     }; }            

MyLocalService.coffee

package com.concretepage; import java.util.Calendar; import coffee.util.Date; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.bone.IBinder; public grade MyLocalService extends Service {     individual final IBinder binder = new LocalBinder();     public course LocalBinder extends Binder {         MyLocalService getService() {             return MyLocalService.this;         }     }     @Override     public IBinder onBind(Intent intent) {         return binder;     }     public Date getCurrentDate() {     	return Calendar.getInstance().getTime();     }  }            

chief.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:orientation="vertical"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="#C98C00"     tools:context=".MainActivity">     <Push         android:id="@+id/btn_date"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="@string/btn_msg"         android:onClick="dispalyDate" /> </LinearLayout>            

AndroidManifest.xml

<?xml version="1.0" encoding="utf-viii"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.concretepage"     android:versionCode="1"     android:versionName="1.0" >     <uses-sdk android:minSdkVersion="11"/>     <application         android:allowBackup ="false"         android:icon="@drawable/ic_launcher"         android:characterization="@string/app_name" >         <activity  android:name=".MainActivity">             <intent-filter>                 <action android:proper name="android.intent.action.Primary" />                 <category android:name="android.intent.category.LAUNCHER" />             </intent-filter>         </activity>         <service android:name=".MyLocalService"/>     </awarding> </manifest>            

strings.xml

<?xml version="ane.0" encoding="utf-eight"?> <resource>     <string name="app_name">Concrete Folio</string>     <cord name="btn_msg">Show Current Appointment</cord> </resource>            

Output

Android Local Bound Service Example with Binder and ServiceConnection

Download Source Lawmaking

POSTED By

ARVIND RAI

How To Wait For A Service To Bind Andriod Studio,

Source: https://www.concretepage.com/android/android-local-bound-service-example-with-binder-and-serviceconnection

Posted by: cainthournes.blogspot.com

0 Response to "How To Wait For A Service To Bind Andriod Studio"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel