return false BindService in api 30

982 Views Asked by At

when i run my code with compileSdkVersion 30 doesn't work but in 29 code working

Intent intent = new Intent();
intent.setPackage("com.androidlearn.securityman");

ServiceConnection serviceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        Log.e("","");
        Toast.makeText(context,"onServiceConnected",Toast.LENGTH_LONG).show();
        listener.onSuccess(IPaymentInterface.Stub.asInterface(service));
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        Toast.makeText(context,"onServiceDisconnected",Toast.LENGTH_LONG).show();
        Log.e("","");
        listener.onFailure("connection failed");

    }
};
context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);

}

In api 30 bindService return false and serviceConnection doesn't respond to listeners

How to solve this problem ?

1

There are 1 best solutions below

0
On

It might be related to the package visibility settings introduced in Android 11. When you have 2 apps: server with the service you are exposing and client that wants to bind that service - the client needs to declare in its Manifest that it can use the service package, like:

<manifest package="com.example.game">
    <queries>
        <package android:name="com.example.store" />
        <package android:name="com.example.services" />
    </queries>
    ...
</manifest>

Otherwise binding will return false, and in system-wide Logcat you will see something like:

I/AppFilter: Interaction: PackageSetting {<client.app.package/PID> -> <server.app.package/PID>} Blocked
W/ActivityManager: Unable to start service Intent { act=<action> pkg=<server.app.package>} U=0: not found

Because without this Manifest declaration the server app service is invisible for client app. Its still possible to start it from the adb if you want to check if the intent data is correct.

More info: https://developer.android.com/training/package-visibility/declaring