android之服务service远程服务

1. 引言: 
   (1)AIDL的作用

      在Android平台,每个应用程序都是一个单独的JVM,都运行在自己的进程空间里, 通常,一个进程不允许访问另一个进程的内存空间(一个应用不能访问另一个应用)。当用户(程序开发人员)想在一个App中访问另一个App的进程空间的时候,就需要进程间通信。在Android中,远程服务为我们提供了实现进程间通信的方式,其中,AIDL是应用程序开发人员常的一种方式。
    

    AIDL (Android Interface Definition Language) 是一种IDL 语言,用于生成可以在android设备上两个进程之间进行进程间通信(interprocess communication, IPC)的代码。如果在一个进程中(例如Activity)要调用另一个进程中(例如Service)对象的操作,就可以使用AIDL生成可序列化的参 数。换句比较浅显的话来说,就是我这个App应用的activity,需要调用其他App应用的Service.当然同一App应用的activity 与service也可以在不同进程间,这可以设置Service配置中,Android:process=":remote"。


2.知识图谱




例子:实现QQ的登录功能

xml代码如下:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  5.     xmlns:tools="http://schemas.android.com/tools"  
  6.     android:orientation="vertical"  
  7.     android:layout_width="match_parent"  
  8.     android:layout_height="match_parent"  
  9.     tools:context="com.example.cookie.android0715services2.MainActivity">  
  10.   
  11.     <TextView  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:text="MyQQ"  
  15.         android:textSize="20sp"/>  
  16.   
  17.    <EditText  
  18.        android:layout_width="match_parent"  
  19.        android:layout_height="wrap_content"  
  20.        android:id="@+id/et_main_uname"  
  21.        android:hint="请输入用户名"/>  
  22.   
  23.     <EditText  
  24.         android:layout_width="match_parent"  
  25.         android:layout_height="wrap_content"  
  26.         android:id="@+id/et_main_upass"  
  27.         android:hint="请输入密码"/>  
  28.   
  29.     <Button  
  30.         android:layout_width="match_parent"  
  31.         android:layout_height="wrap_content"  
  32.         android:onClick="login"  
  33.         android:text="登录"/>  
  34.   
  35. </LinearLayout>  

java代码如下:

MainActivity.Java

[html]  view plain  copy
  1. package com.example.cookie.android0715services2;  
  2.   
  3. import android.app.Service;  
  4. import android.content.ComponentName;  
  5. import android.content.Intent;  
  6. import android.content.ServiceConnection;  
  7. import android.os.IBinder;  
  8. import android.os.RemoteException;  
  9. import android.support.v7.app.AppCompatActivity;  
  10. import android.os.Bundle;  
  11. import android.util.Log;  
  12. import android.view.View;  
  13. import android.widget.EditText;  
  14. import android.widget.Toast;  
  15.   
  16. public class MainActivity extends AppCompatActivity {  
  17.   
  18.     private EditText et_main_uname;  
  19.     private EditText et_main_upass;  
  20.     private Intent intent;  
  21.    // private QQLoginService.MyIBinder myIBinder;  
  22.    private QQLogin qqLogi;  
  23.     private QQLoginInterface qqLoginInterface;  
  24.   
  25.     @Override  
  26.     protected void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.activity_main);  
  29.         et_main_uname = (EditText) findViewById(R.id.et_main_uname);  
  30.         et_main_upass = (EditText) findViewById(R.id.et_main_upass);  
  31.         intent = new Intent(this,QQLoginService.class);  
  32.   
  33.     }  
  34.   
  35.     ServiceConnection serviceConnection=new ServiceConnection() {  
  36.           
  37.         @Override  
  38.         public void onServiceConnected(ComponentName name, IBinder service) {  
  39.             Log.i("test","绑定成功");  
  40.            // myIBinder = (QQLoginService.MyIBinder) service;  
  41.             //qqLogi = (QQLogin) service;  
  42.             qqLoginInterface = QQLoginInterface.Stub.asInterface(service);  
  43.   
  44.         }  
  45.   
  46.         @Override  
  47.         public void onServiceDisconnected(ComponentName name) {  
  48.             Log.i("test","绑定失败");  
  49.         }  
  50.     };  
  51.   
  52.     @Override  
  53.     protected void onResume() {  
  54.         super.onResume();  
  55.         //绑定服务(Service.BIND_AUTO_CREATE绑定的时候自动创建)  
  56.         bindService(intent,serviceConnection, Service.BIND_AUTO_CREATE);  
  57.     }  
  58.   
  59.     public void login(View view) throws RemoteException {  
  60.         String uname=et_main_uname.getText().toString();  
  61.         String upass=et_main_upass.getText().toString();  
  62.        boolean flagqqLoginInterface.login(uname,upass);  
  63.         if (flag){  
  64.             Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();  
  65.         }else{  
  66.             Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();  
  67.         }  
  68.   
  69.     }  
  70. }  

QQService.java

[html]  view plain  copy
  1. package com.example.cookie.android0715services2;  
  2.   
  3. import android.app.Service;  
  4. import android.content.Intent;  
  5. import android.os.Binder;  
  6. import android.os.IBinder;  
  7. import android.support.annotation.Nullable;  
  8. import android.util.Log;  
  9. import android.widget.Toast;  
  10.   
  11. /**  
  12.  * Created by Administrator on 2017/7/15 0015.  
  13.  */  
  14.   
  15. public class QQLoginService extends Service {  
  16.   
  17.     //把它想象成以前我们的dao层登录业务逻辑类  
  18.     class MyIBinder extends QQLoginInterface.Stub{  
  19.         public boolean login(String uname,String upass){  
  20.             if ("10000".equals(uname) &&"123456".equals(upass)){  
  21.                return true;  
  22.             }else{  
  23.                 return false;  
  24.             }  
  25.         }  
  26.   
  27.     }  
  28.     @Nullable  
  29.     @Override  
  30.     public IBinder onBind(Intent intent) {  
  31.         Log.i("test","onBind");  
  32.         return new MyIBinder();  
  33.     }  
  34. }  


在java文件下new一个aidl文件

[html]  view plain  copy
  1. package com.example.cookie.android0715services2;  
  2.   
  3. /**  
  4.  * Created by Administrator on 2017/7/15 0015.  
  5.  */  
  6.   
  7. public interface QQLogin  {  
  8.     public boolean login(String uname,String upass);  
  9. }  

之后再重新编译这个项目

Build -------> Make Project


做一个微信的登录需要用到QQ的登录服务

xml代码如下:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  5.     xmlns:tools="http://schemas.android.com/tools"  
  6.     android:orientation="vertical"  
  7.     android:layout_width="match_parent"  
  8.     android:layout_height="match_parent"  
  9.     tools:context="com.example.cookie.android0715wechat.MainActivity">  
  10.   
  11.     <TextView  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:text="MyWeChat"  
  15.         android:textSize="20sp"/>  
  16.   
  17.     <EditText  
  18.         android:layout_width="match_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:id="@+id/et_main_uname"  
  21.         android:hint="请输入用户名"/>  
  22.   
  23.     <EditText  
  24.         android:layout_width="match_parent"  
  25.         android:layout_height="wrap_content"  
  26.         android:id="@+id/et_main_upass"  
  27.         android:hint="请输入密码"/>  
  28.   
  29.     <Button  
  30.         android:layout_width="match_parent"  
  31.         android:layout_height="wrap_content"  
  32.         android:onClick="login"  
  33.         android:text="登录"/>  
  34.   
  35. </LinearLayout>  


在java下新建一个文件夹,就是以你要启动的那个包名作为它的包名

跳转到project--->找到刚写的QQ登录项目----->generated----->source----->aidl---->debug

copy里面的文件复制到你在微信下新建的包下面


MainActivity.java代码如下

[html]  view plain  copy
  1. package com.example.cookie.android0715wechat;  
  2.   
  3. import android.app.Service;  
  4. import android.content.ComponentName;  
  5. import android.content.Intent;  
  6. import android.content.ServiceConnection;  
  7. import android.os.IBinder;  
  8. import android.os.RemoteException;  
  9. import android.support.v7.app.AppCompatActivity;  
  10. import android.os.Bundle;  
  11. import android.view.View;  
  12. import android.widget.EditText;  
  13. import android.widget.Toast;  
  14.   
  15. import com.example.cookie.android0715services2.QQLoginInterface;  
  16.   
  17. public class MainActivity extends AppCompatActivity {  
  18.   
  19.     private EditText et_main_uname;  
  20.     private EditText et_main_upass;  
  21.     private Intent intent;  
  22.     private QQLoginInterface qqLoginInterface;  
  23.   
  24.     @Override  
  25.     protected void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.activity_main);  
  28.         et_main_uname = (EditText) findViewById(R.id.et_main_uname);  
  29.         et_main_upass = (EditText) findViewById(R.id.et_main_upass);  
  30.         intent = new Intent();  
  31.         //componentName中的第一个双引号写的是你要启动的那个项目的包名,包名.服务类  
  32.         ComponentName componentName=new ComponentName("com.example.cookie.android0715services2","com.example.cookie.android0715services2.QQLoginService");  
  33.         intent.setComponent(componentName);  
  34.     }  
  35.   
  36.     ServiceConnection serviceConnection=new ServiceConnection() {  
  37.   
  38.         @Override  
  39.         public void onServiceConnected(ComponentName name, IBinder service) {  
  40.             //把你的自动生成的aidl的类copy到微信里面  
  41.             qqLoginInterface = QQLoginInterface.Stub.asInterface(service);  
  42.         }  
  43.         @Override  
  44.         public void onServiceDisconnected(ComponentName name) {  
  45.         }  
  46.     };  
  47.   
  48.     @Override  
  49.     protected void onResume() {  
  50.         super.onResume();  
  51.         //绑定服务  
  52.         bindService(intent,serviceConnection, Service.BIND_AUTO_CREATE);  
  53.     }  
  54.   
  55.     public void login(View view)  {  
  56.         String uname=et_main_uname.getText().toString();  
  57.         String upass=et_main_upass.getText().toString();  
  58.         boolean flagfalse;  
  59.         try {  
  60.             flag = qqLoginInterface.login(uname,upass);  
  61.         } catch (RemoteException e) {  
  62.             e.printStackTrace();  
  63.         }  
  64.         if (flag){  
  65.             Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();  
  66.         }else{  
  67.             Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();  
  68.         }  
  69.   
  70.   
  71.     }  
  72. }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值