Development issue/problem:

I have a service related to an application context like this:

getApplicationContext().bindService(
new Intent(this, ServiceUI.class),
serviceConnection,
Context.BIND_AUTO_CREATE
) ;

protected nullity onDestroy() {
super.onDestroy();
getApplicationContext().unbindService(serviceConnection);
}

For some reason the context of the application is sometimes not correctly binding (I can’t fix this part), but in onDestroy() I do a non-binding service, which causes an error.

java.lang.IllegalArgumentException : Unregistered service: tools.cdevice.Devices$mainServiceConnection.

My question is this one: Is there a way to safely call the non-committal department or to see if it is already connected to the department before disbanding it?

Thank you in advance.

How can I solve this problem?

Solution 1:

Try this:

boolean isBound = false;
.
isBound = getApplicationContext().bindService( new Intent(getApplicationContext(), ServiceUI.class), serviceConnection, Context.BIND_AUTO_CREATE );
….
if (isBound)
getApplicationContext().unbindService(serviceConnection) ;

Pay attention:

You should use the same context for binding and non-binding. If you are using a bind service with getApplicationContext(), you must also use getApplicationContext.unbindService(…).

Solution 2:

Here is a good explanation and source code on how to work with related services. In your case, you must override the methods (onServiceConnected and onServiceDisconnected) of the ServiceConnection object. You can then easily check the mBound variable in your code.

Solution 3:

Doing exactly what Andrei Novikov suggested did not suit me.
I just replaced him:

getApplicationContext().unbindService(serviceConnection) ;

С :

unbindService(serviceConnection) ;

Solution 4:

I’ve discovered there are two problems. Multiple attempts to bind, but also multiple attempts to disconnect.

Solution:

the public class ServiceConnectionManager implements ServiceConnection {

closed loop context; closed loop service
;
closed logic attemptToBind = false;
closed logic link = false ;

public ServiceConnectionManager(Context, Class Service) {
this.context = context;
this.service = service;
}

public invalid bindToService() {
if (!tryToBind) {
tryToBind = true;
context.bindService(new Intent(context, service), this, Context.BIND_AUTO_CREATE);
}
}

@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
attemptToBind = false;
bound = true;
}

@Override
public void onServiceDisconnected(ComponentName componentName) {
related = false;
}

public invalid unbindFromService() {
attemptToBind = false;
if (bound) {
context.unbindService(this);
bound = false;
}
}

}

Solution No 5:

Why the mistake?

When trying to cancel the registration of an unregistered service

What are the most common examples?

  • Linking and disconnecting services in different contexts.
  • The unBind(mserviceConnection)-call is larger than bind(…).

The first point speaks for itself. Let’s look at the second source of the error. Debug your bind() and unbind() calls. If you see the calls in this order, your request will receive an illegal argebra.

Give here a description of the image

How can we avoid it?
There are two ways to link and disconnect the service in the activity. In Android documents it is recommended that

  • If you only want to communicate with the service when actions are visible, then

bindService() in onStart() and unbindService() in onStop().

Your activities {

@Overview
public null and void onStart(){
super.onStart() ;
bindService(intention, mConnection , Context.BIND_AUTO_CREATE) ;
}

@Override
public void onStop(){
super.onStop();
unbindService(mConnection);
}.

}

  • If you want to communicate with the service, even if the actions take place in the background, then

bindService() in onCreate() and unbindService() in onDestroy().

Your activities {

@Override
public void onCreate(Bindle sis){
super.onCreate(sis);
….
bindService(intention, mConnection , Context.BIND_AUTO_CREATE) ;
}

@Review
public null and void onDestroy() {
super.onDestroy();
unbindService(mConnection);
}

}

Solution No 6:

I don’t think this instruction is quite correct, as Surya Vijaya Majeed mentioned here. Memory leaks may occur when the corresponding service is restarted and not yet connected.

I think this approach is necessary:

mService ;

private final ServiceConnection mServiceConnection = new ServiceConnection()
{
boolean bound = false ;

@Override
public void onServiceDisconnected(Component Name)
{
mService = null ;
}

@general empty
onServiceConnected(Component name,IBinder service)
{
mService = ((MyService.ServiceBinder) service).getService() ;

if (!bound)
{
// perform any action – the service is bound for the first time
= true;
}
}
}

@Override
public void onDestroy()
{
if (mService !=null)
{
// do something with mService
}.

if (mServiceConnection.bound)
{
mServiceConnection.bound = false;
unbindService(mServiceConnection);
}
super.onDestroy();
}

public void someMethod()
{
if (mService != null)
{
// To check if the service is available, I need to test mService, not mServiceConnection.bound
}
}.

Solution No 7:

Use a variable to write if you bind it to a service, and decompose if the variable is true.

See the official example of Android :

http://androidxref.com/9.0.0_r3/xref/development/samples/ApiDemos/src/com/example/android/apis/app/RemoteService.java#376

Solution No 8:

I wasn’t sure of all the answers above, it seemed too complicated and none of them could have answered my question.

Tie each one once, and the service was definitely tied up at the time the call was made. I don’t want to merge anything, so I just made sure to use the same context for the bind() and unbind() calls, and that solved the problem forever! Do something like this:

any_context.getApplicationContext().bind(…) ;

another_context.getApplicationContext().unbind(…) ;

Good luck!

when to use bound service android,onserviceconnected not called,bound service example android github,querying the service in android,android aidl two way communication,bound service medium,coding for sqlite using android,method makes an activity visible to the user,example of a bound service,appupdateservice : linktodeath,coding with mitch bound services,how to stop services in android,bound service mvvm,bind foreground service android,coding with mitch blog,aidl android,android foreground service example,unbind and stop service android,unbind service android,stop bound service android,android service,start service vs bound service android,serviceconnection,serviceconnection android

You May Also Like

Logitech G533 Software, Gaming Headset, Update G HUB

Logitech G533 Software, Gaming Headset, Update G HUB. What sets the Logitech…

Download iSpoofer PokeGo++ Hack For iOS/Windows PC

iSpoofer PokeGo++ is an application that is very similar to an app…

LG Q8 (2018) Reset & Unlock When Forgot Password

Most of us use computers, smartphones and other digital devices on a…

Net Worth, Wife, Son, and More

<h4>tags with link:</h4><p><strong><iframe width=853 height=480 src=https://www.youtube.com/embed/qMNLZgApDGM frameborder=0 allow=accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope;…