标签:nts statement nbsp 对象 aidl ibinder except asi 新建
aidl可以看做binder的一个辅助接口,aidl让binder通信更加高效.首先包名右键新建aidl文件,在aidl接口中定义一个helloworld方法,然后点击build->makeProject让接口同步,然后新建服务,在服务中实现aidl接口,并自动生成helloworld方法,在onbind方法中返回.Stup对象,.Stup是封装在接口中的必须实现.在配置文件中定义过滤action,action可以随便定义,但是远程绑定此服务的类中要使用相同的action.然后新建一个modle,把第一个modle的包含aidl接口的包整个复制到这个modle 在oncreate方法中绑定远程服务.
package com.january.spring; // Declare any non-default types here with import statements interface IMyAidlInterface { /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString); void sayHelloWorld(); }
上面定义的接口和sayhelloworld方法
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = findViewById(R.id.button); startService(new Intent(this, MyService.class)); } }
上面是第一个modle的主方法
public class MyService extends Service { IMyAidlInterface.Stub stub=new IMyAidlInterface.Stub() { @Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { } @Override public void sayHelloWorld() throws RemoteException { System.out.println("hello"); } }; public MyService() { } @Override public IBinder onBind(Intent intent) { return stub; } }
上面是第一个Modle的服务
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <service android:name=".MyService" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.january.spring" /> </intent-filter> </service> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
上面是第一个modle的配置文件
/////////////////
public class MainActivity extends AppCompatActivity { private Button bind_btn; private IMyAidlInterface iMyAidlInterface; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent("com.january.spring"); intent.setPackage("com.january.spring"); //Android5.0之后需要指定共享Service所在应用的应用包名,否则会抛异常 bindService(intent, connection, BIND_AUTO_CREATE); } private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder); try { iMyAidlInterface.sayHelloWorld(); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName componentName) { } }; }
上面是第二个modle的主类,绑定了服务Android5.0之后需要指定共享Service所在应用的应用包名,否则会抛异常 ,先运行第一个modle开启服务,然后运行第二个modle绑定,输出hello证明绑定成功
标签:nts statement nbsp 对象 aidl ibinder except asi 新建
原文地址:https://www.cnblogs.com/Ocean123123/p/10957164.html