Android服务Services小结(二)

来源:互联网 发布:诸神黄昏升级数据 编辑:程序博客网 时间:2024/06/05 05:56

远程服务(cs 客户端与服务器),用于多个应用程序的通信等等。
//服务器
新建一个class继承Services,在mainfests中配置( android:exported=”true”),重写它的onBind()的方法。因为绑定服务需要返回值IBinder,就需要用到AIDL,
AIDL(Android interface define lauguage)Android接口定义语言,在项目新建一个aidl文件(需要重新编译),它会自动生成接口文件。
相当于业务逻辑层,里面放自己写的方法。然后在Services类里面new一个class类继承于该aidl生成接口文件(后面要点stub),
重写里面的方法。
后面再Activity里面的onREsume()方法里面写绑定服务的方法bindService(intent,serviceConnection, Service.BIND_AUTO_CREATE);
里面有三个参数,第一个参数:Intent(new一个Intent),intent = new Intent(this,QQLoginServices.class);
第二个参数:ServiceConnection,new一个ServiceConnection,实现接口类中的两个方法。onServiceConnected() onServiceDisconnected()。
第三个参数:Service.BIND_AUTO_CREATE(一般都是用这个)。
然后在onServiceConnected()方法里面得到接口的方法:QQLoginInface.Stub.asInterface(iBinder);调用它

//客户端
需要将服务器中AIDL自动生成的接口类以及包copy导客户端项目的Java包中(需要重新编译),开始在Activity中onREsume()方法绑定服务,
方法bindService(intent,serviceConnection, Service.BIND_AUTO_CREATE);
里面有三个参数,第一个参数:Intent(new一个Intent),intent = new Intent(this,QQLoginServices.class);
多进程间启动Services时,Android 5.0 之后,启动其他应用程序的服务,不允许使用隐式。
ComponentName 有两个参数,第一个是放置包名(你所需要的服务在哪个项目),第二个是放置服务类名(你所需要的服务在哪个项目的服务类名)

ComponentName componentName=new ComponentName
(“cn.veryedu.g0305_android18_services”,”cn.veryedu.g0305_android18_services.MyServices”);
intent.setComponent(componentName);

第二个参数:ServiceConnection,new一个ServiceConnection,实现接口类中的两个方法。onServiceConnected() onServiceDisconnected()。
第三个参数:Service.BIND_AUTO_CREATE(一般都是用这个)。
然后在onServiceConnected()方法里面得到接口的方法:QQLoginInface.Stub.asInterface(iBinder);调用它

传递数据(IBinder),通过调用方法取得服务器的数据(传递对象是需要序列化实现远程服务)。

下面上代码:服务器
Services

public class QQLoginServices extends Service {    //新建一个IBinder类继承aidl    class MyIBinder extends QQLoginInface.Stub{        @Override        public boolean Login(String num, String pwd) throws RemoteException {            if("10010".equals(num)&&"123456".equals(pwd)){                return true;            }            return false;        }    }    @Nullable    @Override    public IBinder onBind(Intent intent) {        Log.i("tt","onBind");        return new MyIBinder();    }}

//Activity(简易的登录)

public class MainActivity extends AppCompatActivity {    private EditText main_num;    private EditText main_pwd;    private Intent intent;    private QQLoginInface qqLoginInface;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        main_num = (EditText) findViewById(R.id.main_num);        main_pwd = (EditText) findViewById(R.id.main_pwd);        intent = new Intent();        ComponentName componentName=new ComponentName("com.example.yang_servicesqq","com.example.yang_servicesqq.QQLoginServices");        intent.setComponent(componentName);    }        ServiceConnection serviceConnection=new ServiceConnection() {      @Override      public void onServiceConnected(ComponentName componentName, IBinder iBinder) {          //连接成功          Log.i("tt","服务绑定成功了!");          //得到QQLoginInface.aidl          qqLoginInface = QQLoginInface.Stub.asInterface(iBinder);           }      @Override      public void onServiceDisconnected(ComponentName componentName) {      }  };    @Override    protected void onResume() {        super.onResume();        //绑定服务        bindService(intent,serviceConnection, Service.BIND_AUTO_CREATE);    }    //登录    public void login(View view) throws Exception {        String num=main_num.getText().toString();        String pwd=main_pwd.getText().toString();        boolean f=qqLoginInface.Login(num,pwd);        Toast.makeText(this,""+f,Toast.LENGTH_SHORT).show();    }}

//AIDL

package com.example.yang_servicesqq;interface QQLoginInface {   boolean Login(String num,String pwd);}//配置服务<service android:name=".QQLoginServices"            android:exported="true"            android:enabled="true">        </service>

//客户端Activity

public class MainActivity extends AppCompatActivity {    private EditText main_num;    private EditText main_pwd;    private Intent intent;    private QQLoginInface qqLoginInface;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        main_num = (EditText) findViewById(R.id.main_num);        main_pwd = (EditText) findViewById(R.id.main_pwd);        intent = new Intent();        ComponentName componentName=new ComponentName("com.example.yang_servicesqq","com.example.yang_servicesqq.QQLoginServices");        intent.setComponent(componentName);    }        ServiceConnection serviceConnection=new ServiceConnection() {     @Override      public void onServiceConnected(ComponentName componentName, IBinder iBinder) {          //连接成功          Log.i("tt","服务绑定成功了!");          //得到QQLoginInface.aidl          qqLoginInface = QQLoginInface.Stub.asInterface(iBinder);           }      @Override      public void onServiceDisconnected(ComponentName componentName) {      }  };    @Override    protected void onResume() {        super.onResume();        //绑定服务        bindService(intent,serviceConnection, Service.BIND_AUTO_CREATE);    }    //登录    public void login(View view) throws Exception {        String num=main_num.getText().toString();        String pwd=main_pwd.getText().toString();        boolean f=qqLoginInface.Login(num,pwd);        Toast.makeText(this,""+f,Toast.LENGTH_SHORT).show();    }}

嗯,就这样可以了,有疑问的请留言!

1 0
原创粉丝点击