AIDL客户端未绑定到远程服务

时间:2019-04-13 02:04:08

标签: java android aidl

我正在尝试与远程服务一起运行简单的乘法。我有AIDL服务器文件声明和定义方法。在AIDL中,我已在服务器的程序包名称下复制了与服务器相同的AIDL文件。 我已经为服务器服务的意图过滤器提供了操作。 仍然我的AIDL客户端代码未连接到服务。

AIDLServer:

清单

<service
            android:name=".CalService"
            android:enabled="true"
            android:exported="true"
            android:process=":remote">
            <intent-filter>
                <action android:name="mutliply"/>
            </intent-filter>
        </service>

ICalService.aidl

interface ICalService {
        String getMessage(String name);
        int getResult(int val1, int val2);
}

CalService.java

public class CalService extends Service {
    public CalService() {
    }

    private final ICalService.Stub binder = new ICalService.Stub() {
        @Override
        public String getMessage(String name) throws RemoteException {
            return "Hello " + name + ". The result is: ";
        }

        @Override
        public int getResult(int val1, int val2) throws RemoteException {
            return val1 * val2;
        }
    };

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }
}

AIDLCLient:

MainActivity.java

@Override
    protected void onStart() {
        super.onStart();
        editName = (EditText) findViewById(R.id.editName);
        editVal1 = (EditText) findViewById(R.id.editVal1);
        editVal2 = (EditText) findViewById(R.id.editVal2);
        resultView = (TextView) findViewById(R.id.resultView);

        if(calService == null) {
            Log.v("CALSERVICE", "cal service null");
            Intent it = new Intent("multiply");
            it.setPackage("com.example.aidlserver");
            if(getBaseContext().getApplicationContext().bindService(
                    it, connection, Context.BIND_AUTO_CREATE
            ) == true){
                Log.v("Bind", "Bind service Succeeded");
            } else {
                Log.v("Bind", "Bind service failed");
            }
        } else {
            Log.v("Cal", "Cal Service not null");
        }
    }

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

    public void mutiply(View v) {
        switch (v.getId()) {
            case R.id.btnCal:
                int num1 = Integer.parseInt(editVal1.getText().toString());
                int num2 = Integer.parseInt(editVal2.getText().toString());
                try {
                    int result = calService.getResult(num1, num2);
                    String msg = calService.getMessage(editName.getText().toString());
                    resultView.setText(msg + result);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
        }
    }


    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d("onServiceConnected", "Connected");
            calService = ICalService.Stub.asInterface(service);
            Toast.makeText(getApplicationContext(), "Service Connected",
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d("onServiceDisconnected", "Disconnected");
            calService = null;
            Toast.makeText(getApplicationContext(), "Service Disconnected",
                    Toast.LENGTH_SHORT).show();
        }
    };

1 个答案:

答案 0 :(得分:1)

代码显示其在bindService调用时使用的隐式意图。

Intent it = new Intent("multiply");
it.setPackage("com.example.aidlserver");

如果您的API级别高于21,则必须以明确的意图更新代码。请使用setClassName()API更新代码,以明确意图进行绑定服务调用。

    Intent it = new Intent("multiply");            
    it.setClassName("com.example.aidlserver","com.example.aidlserver.CalService");
    if(getBaseContext().getApplicationContext().bindService(it, connection, Context.BIND_AUTO_CREATE) == true){
        Log.v("Bind", "Bind service Succeeded");
    } else {
        Log.v("Bind", "Bind service failed");
    }

请注意以下几点:

  

警告:为确保您的应用程序安全,请始终使用显式   启动服务时的意图,请勿声明用于   您的服务。使用隐式意图启动服务是   安全隐患,因为您无法确定该服务   响应该意图,用户看不到哪个服务启动。   从Android 5.0(API级别21)开始,系统会抛出一个   如果您以隐式意图调用bindService(),则异常。   参考:https://developer.android.com/guide/components/services

也请检查一下

“要接收隐式意图,必须在意图过滤器中包括CATEGORY_DEFAULT类别”

<category android:name="android.intent.category.DEFAULT"/>

https://developer.android.com/guide/components/intents-filters#Receiving