I simply want to call methods of a local service from my activity. How can I do that ?
You can call startService(intent) and bindService(mIntent, mConnection, BIND_AUTO_CREATE) in any order. Binding and Starting a service are two independent things.
This example demonstrates how do I communicate between Activity and Service in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
You need to create a BroadcastReceiver in your activity (be sure to register it in onResume() and unregister it in onPause() ) and notify it via a broadcast, providing an Intent .
Here is an example that might help
Server.java:
package com.example.bindservice.binder; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; public class Server extends Service { IBinder mBinder = new LocalBinder(); @Override public IBinder onBind(Intent intent) { return mBinder; } public class LocalBinder extends Binder { public Server getServerInstance() { return Server.this; } } public String getTime() { SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return mDateFormat.format(new Date()); } }
Client.java
package com.example.bindservice.binder; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.example.bindservice.binder.Server.LocalBinder; public class Client extends Activity { boolean mBounded; Server mServer; TextView text; Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); text = (TextView)findViewById(R.id.text); button = (Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { text.setText(mServer.getTime()); } }); } @Override protected void onStart() { super.onStart(); Intent mIntent = new Intent(this, Server.class); bindService(mIntent, mConnection, BIND_AUTO_CREATE); }; ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { Toast.makeText(Client.this, "Service is disconnected", 1000).show(); mBounded = false; mServer = null; } @Override public void onServiceConnected(ComponentName name, IBinder service) { Toast.makeText(Client.this, "Service is connected", 1000).show(); mBounded = true; LocalBinder mLocalBinder = (LocalBinder)service; mServer = mLocalBinder.getServerInstance(); } }; @Override protected void onStop() { super.onStop(); if(mBounded) { unbindService(mConnection); mBounded = false; } }; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With