AIDL 服务在 bindService() 之后未连接

问题描述 投票:0回答:2

我正在尝试使用 AIDL 开发 2 个应用程序(服务应用程序 + 客户端应用程序)的设置。我目前有 3 个模块的设置:

  • android-agent-framework(仅包含 AIDL 文件的 android 库模块)
  • android-agent(服务)
  • android-example-client(客户端)

android-agent 和 android-agent-framework 依赖于第一个来访问接口。

每当客户端调用 bindService() 时,它会返回 false,并且在 ServiceConnection 中,不会调用 onServiceConnected()。同样在服务实现中,不会调用 onBind()。日志中没有错误。

代码如下:

android 代理活动:

public class MyCompanyStartActivity extends Activity {

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i(MyCompanyStartActivity.class.toString(), "Create MyCompanyStartActivity");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ComponentName service = startService(new Intent(this, MyCompanyRequestService.class));
        Log.i("tag", service.getClassName() + "::" + service.getPackageName());
    }

}

android代理服务:

public class MyCompanyRequestService extends Service {

@Override
public IBinder onBind(Intent intent) {
    Log.i(MyCompanyRequestService.class.toString(), "Starting SmartRest Service");
    return mBinder;
}

private final IMyCompanyRequestService.Stub mBinder = new IMyCompanyRequestService.Stub() {

    @Override
    public void sendData(String xid, String authentication, String data) throws RemoteException{
        Log.i(MyCompanyRequestService.class.toString(), "sending data: " + data);
    }
};

}

android 代理清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mycompany.android.agent" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MyCompanyStartActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Services -->
        <service
            android:name="com.mycompany.android.agent.framework.MyCompanyRequestService"
            android:process=":remote"
            android:exported="true"
            android:enabled="true">
            <intent-filter>
                <action android:name="MyCompanyRequestService"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </service>

        <!-- Permissions -->

    </application>

</manifest>

android-example-client 活动:

public class ClientStarter extends Activity {

    protected IMyCompanyRequestService mycompanyRequestService = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i("tag","create client");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
    @Override
    protected void onStart() {
        super.onStart();
        if (mycompanyRequestService == null) {
            printServices();
            Intent it = new Intent("MyCompanyRequestService");
            it.setPackage("com.mycompany.android.agent.framework");
            Log.i("tag","before binding service: " + it.getAction() + "::" + it.getPackage());
            boolean serviceBinding = getApplicationContext().bindService(it, connection, Context.BIND_AUTO_CREATE);
            Log.i("tag", "service is bound: " + serviceBinding);
        }
        Handler handler = new Handler();
        handler.postDelayed(new Runner(), 10000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(connection);
    }

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i("service", "Service connected");
            mycompanyRequestService = IMyCompanyRequestService.Stub.asInterface(service);
            Toast.makeText(getApplicationContext(), "Service Connected", Toast.LENGTH_SHORT).show();
            Log.i("service", "Service connected");
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i("service", "Service disconnected");
            mycompanyRequestService = null;
            Toast.makeText(getApplicationContext(), "Service Disconnected", Toast.LENGTH_SHORT).show();
            Log.i("service", "Service disconnected");
        }
    };

    private void printServices() {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            Log.d("service", service.service.getClassName());
        }
    }

    private class Runner implements Runnable {

        @Override
        public void run() {
            Log.i("tag","starting");
            LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            Location loc;
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                Log.e(ClientStarter.class.toString(), "Error", e);
            }            while(true) {
                try {
                    if (mycompanyRequestService != null) {
                        loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        Log.i(ClientStarter.class.toString(), loc.getLatitude() + " - " + loc.getLongitude() + " - " + loc.getAltitude());
                        mycompanyRequestService.sendData("test", "auth", String.valueOf(loc.getLatitude()) + "," + String.valueOf(loc.getLongitude()) + "," + String.valueOf(loc.getAltitude()));
                    } else {
                        Log.i(ClientStarter.class.toString(), "service not yet available");
                    }
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    Log.e(ClientStarter.class.toString(), "Error", e);
                } catch (RemoteException e) {
                    Log.e(ClientStarter.class.toString(), "Error", e);
                }
            }
        }


    }

}

尝试绑定服务之前的 printServices() 调用实际上列出了服务,因此它正在运行。

日志没有任何错误,客户端最终在循环中运行,但服务仍然为空。

也许以前有人遇到过类似的问题。

android android-service aidl android-service-binding
2个回答
11
投票

在对所有文件进行另一轮检查后,我发现了我的错误。

我需要改变:

Intent it = new Intent("MyCompanyRequestService");
it.setPackage("com.mycompany.android.agent.framework");

至:

Intent it = new Intent("MyCompanyRequestService");
it.setPackage("com.mycompany.android.agent");

Intent的包需要匹配app的包,而不是service的包。


1
投票

你可能会遇到这个问题的另一个原因是——从 API 级别 30 开始——你需要声明你与之通信的应用程序,例如:

在客户的应用程序清单文件中:

<queries>
        <package android:name="com.your.server.app" />
</queries>
© www.soinside.com 2019 - 2024. All rights reserved.