今天我们来看看Android的远程服务(及绑定)。
为什么要使用远程服务呢,主要是因为它可以帮助我们完成进程间通信(ipC),这是一个相当重要的一门技术,当然了,在实际的开发中,本地服务才是用的最多的。
下面我模拟了一个登录的远程服务,直接上代码,看我细细道来: activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.zking.android_service.MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et_main_number" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et_main_pwd" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录" android:onClick="login" /></LinearLayout>新建一个登录的服务类,继承Service QQLoginService.java
public class QQLoginService extends Service { //这个内部类类似于java项目中的业务逻辑类,用来处理数据并返回值 // class MyIBinder extends QQLoginInterfaceOut.Stub{ @Override public boolean login(String number, String pwd) throws RemoteException { if("10086".equals(number)&&"123456".equals(pwd)){ return true; } return false; } } //绑定 @Nullable @Override public IBinder onBind(Intent intent) { Log.i("test","onBind"); return new MyIBinder(); }}既然有了业务逻辑类了,那么肯定还需要一个服务接口了,不过,这个接口有点特别,它需要aidl(Android Interface definition language的缩写),一看就明白,它是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口。当新建一个aidl文件后,项目会自动生成一个Java接口
这里,我们右键JAVA文件下的包,新建一个aidl

自动生成的接口类所在文件 build>generated>source>aidl>debug>com.zking.android_service2>QQLoginInterfaceOut
// QQLoginInterfaceOut.aidlpackage com.zking.android_service2;statementsimport com.zking.android_service2.QQUser;interface QQLoginInterfaceOut { boolean login(String number,String pwd);}那么,现在我们需要的就是绑定服务了。绑定服务,需要在activity的生周期onresume中进行(binderService)。不过,还需要实例化一个接口 ServiceConnection 进行连接服务。
MainActivity.java
public class MainActivity extends AppCompatActivity { PRivate EditText et_main_number; private EditText et_main_pwd; private Intent intent; private QQLoginService.MyIBinder myIBinder; private QQLoginInterface qqLoginInterface; private QQLoginInterfaceOut qqLoginInterfaceOut; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_main_number = (EditText) findViewById(R.id.et_main_number); et_main_pwd = (EditText) findViewById(R.id.et_main_pwd); intent = new Intent(this,QQLoginService.class); } //服务的连接 ServiceConnection serviceConnection=new ServiceConnection() { //绑定成功 @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.i("test","绑定成功了"); qqLoginInterfaceOut = QQLoginInterfaceOut.Stub.asInterface(service); } //绑定失败 @Override public void onServiceDisconnected(ComponentName name) { Log.i("test","绑定失败了"); } }; @Override protected void onResume() { super.onResume(); //绑定服务 bindService(intent,serviceConnection, Service.BIND_AUTO_CREATE); } public void login(View view){ //获取用户名、密码 String number=et_main_number.getText().toString(); String pwd=et_main_pwd.getText().toString(); boolean f= false; try { f = qqLoginInterfaceOut.login(number,pwd); } catch (RemoteException e) { e.printStackTrace(); } Toast.makeText(this, " "+f, Toast.LENGTH_SHORT).show(); }}新闻热点
疑难解答