Service not Stop In Android Oreo - android

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.

Related

Start and stop service

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;
}
};

Issues when trying to bind to a service after unbinding

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");
}
}

Android Bound Service - How To Start?

I am stuck with getting my Android Service running and would really need some help.
I started off with Vogella's Tutorial on how to bind a Service and ended up trying out pretty much every approach which came to my mind and on the web to solve my problem.
"onStart()" of my main Activity gets called and no Exception (creating Intent, "bindService()") is thrown. But, also, no Service is started (apiService == null).
I know it shouldn't be hard to get it working, but sadly I am stuck with this for over two hours already. Any kind of help, pointers, etc. is really appreciated.
[EDIT]
Breakpoints set in the service class don't get hit. Also, Log.d() entries won't print in Logcat.
Service extending Android.Service and implementing my own Interface:
public class APIService extends Service implements APICall{
private final IBinder binder = new APIBinder();
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId){
super.onStartCommand(intent, flags, startId);
return Service.START_NOT_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return binder;
}
public class APIBinder extends Binder{
public APIService getService(){
return APIService.this;
}
}
//implementation of Interface...
Main Activity / binding of Service:
public class MainActivity extends Activity {
private APIService apiService = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onStart() {
super.onStart();
Intent serviceIntent = new Intent(this, APIService.class);
bindService(serviceIntent, apiServiceConnection, Context.BIND_AUTO_CREATE);
};
#Override
protected void onDestroy(){
super.onDestroy();
unbindService(apiServiceConnection);
}
private ServiceConnection apiServiceConnection = new ServiceConnection(){
public void onServiceConnected(ComponentName className, IBinder binder) {
APIBinder localBinder = (APIBinder)binder;
apiService = localBinder.getService();
}
public void onServiceDisconnected(ComponentName className) {
apiService = null;
}
};
//click handler, etc...
Manifest file entry:
<service android:name=".APIService"/>
You should add
startService(serviceIntent);
before
bindService(serviceIntent, apiServiceConnection, Context.BIND_AUTO_CREATE);

Not able to get binder object from Service

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;
}
.....
}

bind/unbind service example (android)

can you give me a simple example of an application with background service which uses bind/unbind methods to start and stop it? I was googling for it for a half-hour, but those examples use startService/stopService methods or are very difficult for me. thank you.
You can try using this code:
protected ServiceConnection mServerConn = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName name, IBinder binder) {
Log.d(LOG_TAG, "onServiceConnected");
}
#Override
public void onServiceDisconnected(ComponentName name) {
Log.d(LOG_TAG, "onServiceDisconnected");
}
}
public void start() {
// mContext is defined upper in code, I think it is not necessary to explain what is it
mContext.bindService(intent, mServerConn, Context.BIND_AUTO_CREATE);
mContext.startService(intent);
}
public void stop() {
mContext.stopService(new Intent(mContext, ServiceRemote.class));
mContext.unbindService(mServerConn);
}
Add these methods to your Activity:
private MyService myServiceBinder;
public ServiceConnection myConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder binder) {
myServiceBinder = ((MyService.MyBinder) binder).getService();
Log.d("ServiceConnection","connected");
showServiceData();
}
public void onServiceDisconnected(ComponentName className) {
Log.d("ServiceConnection","disconnected");
myService = null;
}
};
public Handler myHandler = new Handler() {
public void handleMessage(Message message) {
Bundle data = message.getData();
}
};
public void doBindService() {
Intent intent = null;
intent = new Intent(this, BTService.class);
// Create a new Messenger for the communication back
// From the Service to the Activity
Messenger messenger = new Messenger(myHandler);
intent.putExtra("MESSENGER", messenger);
bindService(intent, myConnection, Context.BIND_AUTO_CREATE);
}
And you can bind to service by ovverriding onResume(), and onPause() at your Activity class.
#Override
protected void onResume() {
Log.d("activity", "onResume");
if (myService == null) {
doBindService();
}
super.onResume();
}
#Override
protected void onPause() {
//FIXME put back
Log.d("activity", "onPause");
if (myService != null) {
unbindService(myConnection);
myService = null;
}
super.onPause();
}
Note, that when binding to a service only the onCreate() method is called in the service class.
In your Service class you need to define the myBinder method:
private final IBinder mBinder = new MyBinder();
private Messenger outMessenger;
#Override
public IBinder onBind(Intent arg0) {
Bundle extras = arg0.getExtras();
Log.d("service","onBind");
// Get messager from the Activity
if (extras != null) {
Log.d("service","onBind with extra");
outMessenger = (Messenger) extras.get("MESSENGER");
}
return mBinder;
}
public class MyBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
After you defined these methods you can reach the methods of your service at your Activity:
private void showServiceData() {
myServiceBinder.myMethod();
}
and finally you can start your service when some event occurs like _BOOT_COMPLETED_
public class MyReciever extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("android.intent.action.BOOT_COMPLETED")) {
Intent service = new Intent(context, myService.class);
context.startService(service);
}
}
}
note that when starting a service the onCreate() and onStartCommand() is called in service class
and you can stop your service when another event occurs by stopService()
note that your event listener should be registerd in your Android manifest file:
<receiver android:name="MyReciever" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
First of all, two things that we need to understand,
Client
It makes request to a specific server
bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"),
mServiceConn, Context.BIND_AUTO_CREATE);
here mServiceConn is instance of ServiceConnection class(inbuilt) it is actually interface
that we need to implement with two (1st for network connected and 2nd network not connected) method to monitor network connection state.
Server
It handles the request of the client and makes replica of its own which is private to client only who send request and this raplica of server runs on different thread.
Now at client side, how to access all the methods of server?
Server sends response with IBinder Object. So, IBinder object is our handler which accesses all the methods of Service by using (.) operator.
.
MyService myService;
public ServiceConnection myConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder binder) {
Log.d("ServiceConnection","connected");
myService = binder;
}
//binder comes from server to communicate with method's of
public void onServiceDisconnected(ComponentName className) {
Log.d("ServiceConnection","disconnected");
myService = null;
}
}
Now how to call method which lies in service
myservice.serviceMethod();
Here myService is object and serviceMethod is method in service.
and by this way communication is established between client and server.

Categories

Resources