I've created a simple class which extends the Service class to understand how services work. But I've got a problem. I want my service to run without interruption while I'm binding and unbinding. I start the service inside activity, then I bind the activity to it, unbind and bind again. But the things after the unbinding does not go well. I don't get the output of onBind(Intent intent) and onUnbind(Intent intent) or onRebind(Intent intent) methods in the service.
public class MyService extends Service {
private final IBinder binder = new LocalBinder();
public class LocalBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
#Override
public void onCreate() {
MyActivity.log("service created");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
MyActivity.log("service started");
return 0;
}
#Override
public void onDestroy() {
MyActivity.log("service destroyed");
}
#Override
public IBinder onBind(Intent intent) {
MyActivity.log("binded");
return binder;
}
#Override
public boolean onUnbind(Intent intent) {
MyActivity.log("unbinded");
return super.onUnbind(intent);
}
#Override
public void onRebind(Intent intent) {
MyActivity.log("rebinded");
}
}
Related
I have a code like this, which run, when click the button
if (isMyServiceRunning(PlayerService.class)) {
stopService(mIntent);
unbindService(serviceConnection);
} else {
startService(mIntent);
bindService(mIntent, serviceConnection, Context.BIND_AUTO_CREATE);
}
When I click button and my service do not running, service start to work( all good here)
When I click button and my service running, service stop and destroy( all good here)
but when I click, to service start to work, service do not working
My Service
public class PlayerService extends Service {
private final IBinder binder = new LocalBinder();
#Nullable
#Override
public IBinder onBind(Intent intent) {
return binder;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
doAnything();
return START_STICKY;
}
public class LocalBinder extends Binder {
PlayerService getService() {
return PlayerService.this;
}
}
}
Service Connection
private ServiceConnection serviceConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className, IBinder service) {
PlayerService.LocalBinder binder = (PlayerService.LocalBinder) service;
myService = binder.getService();
Log.d("TEST", "MainActivity onServiceConnected");
bound = true;
}
#Override
public void onServiceDisconnected(ComponentName arg0) {
Log.d("TEST", "MainActivity onServiceDisconnected");
bound = false;
}
};
I have implemented Service in Application but need to stop service after killing App. But I got an issue in Android O. There is one Method onTaskRemoved which in not call in Orio. I am also trying to JobScheduler But getting the same issue. ANy suggestion or solution will be appreciated. :)
Start Service:
ServiceConnection mConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className,
IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
}
#Override
public void onServiceDisconnected(ComponentName arg0) {
}
};
Intent intentService = new Intent(this, ServiceClass.class);
bindService(intentService, mConnection, Context.BIND_AUTO_CREATE);
Service Class:
public class ServiceClass extends Service {
private final IBinder mBinder = new LocalBinder();
#Nullable
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
#Override
public void onTaskRemoved(Intent rootIntent) {
stopSelf();
super.onTaskRemoved(rootIntent);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}
public class LocalBinder extends Binder {
public ServiceClass getService() {
// Return this instance of LocalService so clients can call public methods
return ServiceClass.this;
}
}
}
AndroidManfiest.xml
<service android:name="Service"
android:stopWithTask="false"/>
I Hope this will work for you
android:stopWithTask="false"
Add this attribute in your service tag in manifest file.
I'm kind of confused with bound services. I read Can anybody explain what is difference between unbound and bound service in android this post and based on that I'm trying out a sample. I created a Service and binding it to My activity as shown below
public class MyService extends Service {
private static final String TAG = "MyService";
MyBinder mMyBinder = new MyBinder();
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand: ");
// start long running operation
return super.onStartCommand(intent, flags, startId);
}
#Override
public boolean onUnbind(Intent intent) {
Log.d(TAG, "onUnbind: ");
return super.onUnbind(intent);
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind: ");
return mMyBinder;
}
public class MyBinder extends Binder {
public MyService getService(){
return MyService.this;
}
}
}
Activity is
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
MyService.MyBinder mMyBinder;
private Connect mConn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, MyService.class);
mConn = new Connect();
bindService(intent, mConn, BIND_AUTO_CREATE);
}
public class Connect implements ServiceConnection {
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "onServiceConnected: ");
}
#Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "onServiceDisconnected: ");
}
}
}
The service 'onStartCommand' is not called when I call bindService(intent, mConn, BIND_AUTO_CREATE);
should I call startService after binding? binding will not start the service?
If I already have a service which is started using startService and running , and I'm bindind the same service in some other activity, what happens if activity goes out of scope?
MainActivity starts my TestService in onCreate and bind in onStart method. AsyncThread is started in TestService onStartCommand. bind and unbind methods are called in correct sequences. All things work perfectly, absolutely no issue :).
Issue starts from here: If MainActivity is terminated then running Async thread is also stopped w/o any interrupt exception but TestService is still running which I can check at Running Application Setting.
Please help me to find out why thread stops working.
PS: I cross checked with Thread/Handler but same result.
MainActivity and Service code are here:
public class MainActivity extends Activity implements IServiceInterface {
boolean mBound = false;
TestService mTestService = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent serviceIntent = new Intent(this, TestService.class);
startService(serviceIntent);
}
#Override
protected void onStart() {
super.onStart();
// Bind to LocalService
NSLogger.i("service binding....");
Intent intent = new Intent(this, TestService.class);
bindService(intent, mBindConnection, Context.BIND_AUTO_CREATE);
}
#Override
protected void onStop() {
super.onStop();
if (mBound) {
NSLogger.i("service unbinding....");
unbindService(mBindConnection);
mBound = false;
}
}
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mBindConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className,
IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
NSLogger.i("UI is connected to service");
LocalBinder binder = (LocalBinder) service;
mTestService = binder.getTestService();
mBound = true;
}
#Override
public void onServiceDisconnected(ComponentName arg0) {
NSLogger.i("UI is disconnected from service");
mBound = false;
mTestService = null;
}
};
}
public class TestService extends Service implements Runnable, Handler.Callback{
// Binder given to inproc clients
private final IBinder mBinder = new LocalBinder();
#Override
public IBinder onBind(Intent intent) {
mUIIsBound = true;
return mBinder;
}
public boolean onUnbind (Intent intent) {
mUIIsBound = false;
NSLogger.i("unbind service");
return true;
}
public class LocalBinder extends Binder {
TestService getTestService() {
return TestService.this;
}
}
public void onDestroy() {
//NEVER CALLED.
}
public int onStartCommand(Intent intent2, int flags, int startId) {
NSLogger.i("TestService start!");
new DownloadConfiguration().execute();
return START_STICKY;
}
private class DownloadConfiguration extends AsyncTask<Void, Integer, Boolean> {
#Override
protected Boolean doInBackground(Void... params) {
try {
NSLogger.i("before sleep");
Thread.sleep(5000);
NSLogger.i("After sleep");
} catch (InterruptedException e) {
e.printStackTrace();
}
return true;
}
}
I am receiving a boot completed intent in my BootReceiver class and start a service when I receive that intent.
#Override
public void onReceive(Context arg0, Intent arg1) {
Intent myIntent = new Intent(arg0, BootService.class);
arg0.startService(myIntent);
}
The service is started normally and now, I want to use the binder object within the service . Here is the service code.
public class BootService extends Service implements IBinder{
private IBinder binder;
public class LocalBinder extends Binder {
IBinder getService() {
return BootService.this;
}
}
#Override
public void onCreate() {
super.onCreate();
Log.d("BootService", "onCreate()");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("BootService", "onStartCommand()");
binder = new LocalBinder().getService();
//This doesn't seem to work
//I wana use this binder object here
return START_STICKY;
}
.....
}
I am not sure if this is the correct way to get a binder.
Any help is much appreciated!!
In ur Acivity
#Override
protected void onStart() {
super.onStart();
Intent intent = new Intent(getApplicationContext(), MyService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
#Override
protected void onStop() {
super.onStop();
unbindService(mConnection);
}
private ServiceConnection mConnection = new ServiceConnection() {
#Override
public void onServiceDisconnected(ComponentName name) {
Toast.makeText(getApplicationContext(), "Service disconnected", 1000).show();
mBindr = false;
}
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
Toast.makeText(getApplicationContext(), "Service connected", 1000).show();
LocalBinder mLocalBinder = (LocalBinder) service;
myService = mLocalBinder.getSrvice();
mBindr = true;
}
};
Update your code
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class BootService extends Service {
private IBinder binder = new LocalBinder();
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return binder;
}
public class LocalBinder extends Binder {
BootService getService() {
return BootService.this;
}
}
#Override
public void onCreate() {
super.onCreate();
Log.d("BootService", "onCreate()");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("BootService", "onStartCommand()");
//binder = new LocalBinder().getService();
//This doesn't seem to work
//I wana use this binder object here
return START_STICKY;
}
.....
}