Android Studio中的跨进程访问(aidl)

  1. 服务端
    创建一个MyService继承Service并在Manifests.xml中注册,因为服务器不需要界面,我将Activity删除了,记得同时将Manifests.xml中的Activity注册删除,右键创建AIDL文件,创建完成后将和java在同一个路径下
    Android Studio中的跨进程访问(aidl)_第1张图片
    IService.aidl
// IService.aidl
package com.example.yuxin.service_aidl;

// Declare any non-default types here with import statements

interface IService {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */

      String getName(int num);
}

创建完成可能不会马上编译成java文件,你可以选中项目,点击Make Project这里写图片描述

MyService.java

package com.example.yuxin.service_aidl;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
/**
 * Created by yuxin on 2016/7/1 0001.
 */
public class MyService extends Service {
    private String[] name=new String[]{"琼恩","珊莎","猎狗"};
    private  MyBinder myBinder=new MyBinder();

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return myBinder;
    }
    private class MyBinder extends IService.Stub{

        @Override
        public String getName(int num) throws RemoteException {
            return name[num];
        }
    }
}

Manifests.xml


<manifest package="com.example.yuxin.service_aidl"
          xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <service android:name=".MyService">
            <intent-filter >
            <action android:name="com.jju.aidlService">action>
        intent-filter>
        service>
    application>
manifest>
  1. 客户端
    编写一个界面,这里不写了,同上面的步骤创建一个AIDL文件夹,将服务端adli文件夹中的package和package下面的文件复制到客户端的aidl文件夹中,注意是复制!包名和类名都不能修改,再次点击Make Project让其将aidl编译成java文件,在Activity中便可调用Stub类
    MainActivity.java
package com.example.yuxin.service_aidl;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    private EditText et_name;
    private EditText et_num;
    private IService iService;
    private  MyServiceConnetion conn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_name = (EditText) findViewById(R.id.et_name);
        et_num = (EditText) findViewById(R.id.et_num);
        Intent service=new Intent();
         conn=new MyServiceConnetion();
        service.setAction("com.jju.aidlService");
        bindService(service,conn, Context.BIND_AUTO_CREATE);


    }
    private class MyServiceConnetion implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            iService = IService.Stub.asInterface(service);

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            conn=null;

        }
    }
    public void quary(View v) throws RemoteException {
       int num= Integer.parseInt(et_num.getText().toString());
        String name = iService.getName(num);
        et_name.setText(name);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(conn);
    }
}

目录结构:
Android Studio中的跨进程访问(aidl)_第2张图片
先运行服务端,然后运行客服端,运行结果:
Android Studio中的跨进程访问(aidl)_第3张图片

我的博客网站:http://huyuxin.top/欢迎大家访问!评论!

你可能感兴趣的:(Android)