Android studio 使用aidl实现远程服务

AIDL

定义

Android Interface definition language的缩写,它是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口

在androidstudio简单使用

服务端

  1. 新建服务
public class TestService extends Service {
    private static final String TAG = "TestService";
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.e(TAG, "onBind: " );
        return new TextBinder();
    }

    class TextBinder extends  IMyAidlInterface.Stub{

        @Override
        public void pay(int money) throws RemoteException {
            TestService.this.pay(money);
        }
    }
    public void pay(int money){
        Log.e(TAG, "pay: "+money+"sueecss" );
    }
}


  1. 新建aidl文件

app右键 new->aidl
// IMyAidlInterface.aidl
package cn.zsp.aidldemo_01;

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

interface IMyAidlInterface {
    void pay(int money);
}

Build ->mark project (生成对应的java文件)

客户端

  1. 复制服务端的aidl (包括包要完全一致)
  2. 客户端代码
package cn.zsp.aidldemo_02;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
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;
import android.widget.Toast;

import java.util.List;

import cn.zsp.aidldemo_01.IMyAidlInterface;

public class MainActivity extends AppCompatActivity {
    private EditText etMoney;
    private IMyAidlInterface mMyAidlInterface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etMoney= (EditText) findViewById(R.id.etMoney);
        Intent intent=new Intent();
        intent.setAction("cn.zsp.testservice");
        Intent intent1=new Intent(getExplicitIntent(this,intent));
        bindService(intent1, new ServiceConnection() {


            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                mMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
                Toast.makeText(MainActivity.this, "服务连接成功", Toast.LENGTH_SHORT).show();
                //服务链接成功才能进行后续操作 否者会报空指针异常  服务连接需要时间

            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                Toast.makeText(MainActivity.this, "服务连接失败", Toast.LENGTH_SHORT).show();
            }
        },BIND_AUTO_CREATE);
    }

    public void pay(View view){
        try {
            String money=etMoney.getText().toString().trim();
            mMyAidlInterface.pay(Integer.parseInt(money));
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * Android 5.0 之后 服务必须显示启动 所以需要把隐式改成显示启动
     * @param context
     * @param implicitIntent
     * @return
     */
    public static Intent getExplicitIntent(Context context, Intent implicitIntent) {
        // Retrieve all services that can match the given intent
        PackageManager pm = context.getPackageManager();
        List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);
        // Make sure only one match was found
        if (resolveInfo == null || resolveInfo.size() != 1) {
            return null;
        }
        // Get component info and create ComponentName
        ResolveInfo serviceInfo = resolveInfo.get(0);
        String packageName = serviceInfo.serviceInfo.packageName;
        String className = serviceInfo.serviceInfo.name;
        ComponentName component = new ComponentName(packageName, className);
        // Create a new intent. Use the old one for extras and such reuse
        Intent explicitIntent = new Intent(implicitIntent);
        // Set the component to be explicit
        explicitIntent.setComponent(component);
        return explicitIntent;
    }
}

代码

链接:http://pan.baidu.com/s/1nvdCKFv 密码:es58

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值