Android中的服务(service)--AIDL远程服务

这几天仔细研究了下service服务:
AIDL的作用
在Android平台,每个应用程序都是一个单独的JVM,都运行在自己的进程空间里, 通常,一个进程不允许访问另一个进程的内存空间(一个应用不能访问另一个应用)。当用户(程序开发人员)想在一个App中访问另一个App的进程空间的时候,就需要进程间通信。在Android中,远程服务为我们提供了实现进程间通信的方式,其中,AIDL是应用程序开发人员常的一种方式。
那么我们首先
(1)在工程的src下,新建立一个文本文件,将要实现的函数放在这个文件中,后缀为.aidl。
(2)刷新工程后,就会发现在gen包下,有一个同名的java文件,这是aidl工具自动生成的,里面,就有我们要实现的函数。
老规矩废话少说,直接上列子
通过AIDL远程调用Service

1.MainActivity.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:orientation="vertical"
   >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="微信登录"
        android:textColor="#ff0000"
        android:textSize="30sp"
        android:gravity="center"
        />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="2279356953"
        android:textSize="30sp"
        android:id="@+id/main_number"

        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="12345678"
        android:id="@+id/main_pwd"
        android:textSize="30sp"
        />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登录"
            android:layout_marginRight="10dp"
            android:onClick="login"
            android:textSize="30sp"
            />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="取消"
            android:textSize="30sp"
            />
    </LinearLayout>
</LinearLayout>

2.MyLoginService

package com.zking.suzhen_android_service_qq;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

import java.util.Map;

/**
 * Created by suzhen on 2017/2/12.
 */

public class MyLoginService extends Service {

    class MyBind extends MyLoginInterfaceOut.Stub{

        @Override
        public boolean Login(String name, String pwd) throws RemoteException {
            if("admin".equals(name)&&"123456".equals(pwd)){
                return true;
            }
            return false;
        }

        @Override
        public User login2(Map map) throws RemoteException {
            String name=map.get("name").toString();
            String pwd=map.get("pwd").toString();
            if("admin".equals(name)&&"12345678".equals(pwd)){
                User user=new User("黑马",name,pwd);
                return user;
            }
            return null;
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.i("text","onBind");
        return new MyBind();
    }
}
3.User类

package com.zking.suzhen_android_service_qq;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * Created by suzhen on 2017/2/13.
 */

public class User implements Parcelable{
    private String uname;
    private String number;
    private String pwd;

    public User() {
    }

    public User(String uname, String number, String pwd) {
        this.uname = uname;
        this.number = number;
        this.pwd = pwd;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(uname);
        dest.writeString(number);
        dest.writeString(pwd);

    }
    public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>(){

        @Override
        public User createFromParcel(Parcel source) {
            User user=new User();
            user.setUname(source.readString());
            user.setNumber(source.readString());
            user.setPwd(source.readString());
            return user;
        }

        @Override
        public User[] newArray(int size) {
            return new User[size];
        }
    };
}
4.MyLoginInterface

package com.zking.suzhen_android_service_qq;

import java.util.Map;

/**
 * Created by suzhen on 2017/2/12.
 */

public interface MyLoginInterface {
    public boolean Login(String name,String pwd);
    public User login2(Map<String,Object> map);
}

5.User.aidl

// UserOut.aidl
package com.zking.suzhen_android_service_qq;
import com.zking.suzhen_android_service_qq.User;
parcelable User;

6.MyLoginInterfaceOut.aidl

// MyLoginInterfaceOut.aidl
package com.zking.suzhen_android_service_qq;
import com.zking.suzhen_android_service_qq.User;
interface MyLoginInterfaceOut {
    boolean Login(String name,String pwd);

    User login2(in Map map);
}

7.AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.zking.suzhen_android_service_qq">

    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".MyLonginService"
            android:exported="true"></service>
    </application>

</manifest>

列子全部代码就在这里了,各位老铁!稳

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值