package com.example.aidlclient;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.aidltest.IMyAidlInterface;
import java.security.Provider;
public class MainActivity extends AppCompatActivity {
private TextView tv;
private Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=findViewById(R.id.tv);
Intent intent = new Intent();
intent.setPackage("com.example.aidltest");
intent.setAction("com.example.aidltest.MyService");
bindService(intent, connection, Service.BIND_AUTO_CREATE);
}
private IMyAidlInterface aidlService;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
tv.setText("50");
new Thread( new Runnable( )
{
@Override
public void run( ) {
aidlService = (IMyAidlInterface) IMyAidlInterface.Stub.asInterface(service);
try {
int sum = aidlService.add(10, 50);//对10和50相加
Log.d("onServiceConnected", sum + "");
tv.setText(sum);
} catch (RemoteException e) {
e.printStackTrace();
}
}
} ).start( );
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
tv.setText("连接断开");
}
};
}
服务端aidltest架构
android:exported="true">
<intent-filter>
<action android:name="com.example.aidltest.MyService"></action>
</intent-filter>
</service>
客户端aidlclient架构
是不是service没有在清单文件中注册呀
、这是在服务端注册的service:
包名 package="com.example.aidltest"
<service
android:name="com.example.aidltest.MyService"
android:exported="true">
<intent-filter>
<action android:name="com.example.aidltest.MyService"></action>
</intent-filter>
</service>