跨进程访问(AIDL服务)—Service(二)
在前面的一期里我复习的Content Provider,我们都知道Content Provider 是可以跨进程通信的,它为不同程序之间提供了统一分享数据的接口,有的是程序内置的ContentProvider,如果你自己的程序也需要想外分享数据的话可以给自己的程序自定义一个Content Provider 。话说了这么多,还是言归正传,在Service里面是怎样跨进程通信的呢?
这就要靠AIDL服务了。什么是AIDL服务?
AIDL 的英文全称是 Android interface Definition Language 很明显,这是android 的接口定义语言来公开服务的接口。
在java源目录下建一个后缀名为aidl的文件,只要文件编写成功就会在gen目录下生成一个java接口文件,而这个接口就是向外分享数据的接口。说到这里,想必大家都明白了。
下面我们就开始写代码了,首先我们来写个服务类
package com.example.aidl_01; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; public class MyService extends Service{ public class MyServiceImpl extends IMyService.Stub{ @Override public String getValue() throws RemoteException { // TODO Auto-generated method stub return "<android>深度解析"; } } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return new MyServiceImpl(); } }
里面还有一个内嵌类,只有实现了该类,就能调用实现的方法了
注册
<service android:name=".MyService">
<intent-filter >
<action android:name="com.example.aidl_01.IMyService"/>
</intent-filter>
</service>
这样服务端就写好了。接下来我们来写个客户端来访问Service
在此之前我们要先做个准备工作,就是把刚才生成的java接口文件连包一起复制到src目录下。
package com.example.aidlclient_01; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import com.example.aidl_01.IMyService; public class MainActivity extends Activity implements OnClickListener{ private IMyService myService = null; private Button button_01 = null; private Button button_02 = null; private TextView textView =null; private ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub获取服务 //我认为这是比较关键的一步,获取服务对象 myService = IMyService.Stub.asInterface(service); button_02.setEnabled(true); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button_01 = (Button)findViewById(R.id.button_01); button_02 = (Button)findViewById(R.id.button_02); textView = (TextView)findViewById(R.id.textView); button_01.setOnClickListener(this); button_02.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.button_01: System.out.println("button_01------>"+v.getId()); //然后绑定AIDL服务,传了一个客户端访问AIDL服务的ID bindService(new Intent("com.example.aidl_01.IMyService"), serviceConnection, Context.BIND_AUTO_CREATE); break; case R.id.button_02: try { textView.setText(myService.getValue()); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; default: break; } } }
先要绑定服务,才能得到服务对象。
这样就可以了。也是刚刚接触的知识点,不过理解起来还是比较容易的。
浙公网安备 33010602011771号