service AIDL (仿照给力)

服务端:
1

// IAlipayInterface.aidl
package com.example.alipay;

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

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

            int call_safepay();
}

2

public class Alipay extends Service {
    private class MyBinder extends IAlipayInterface.Stub{

        @Override
        public int call_safepay() throws RemoteException {
            return safepay();
        }
    }
    public Alipay() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        //throw new UnsupportedOperationException("Not yet implemented");
        return new MyBinder();
    }
    public int safepay(){
        return 5;
    }
}

3

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Alipay">
        <service
            android:name=".Alipay"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.alipay"/>
            </intent-filter>
        </service>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

客户端
1.将服务端的aidl整个目录拷贝过来,特别注意,目录要和服务端一模一样
2、

private IAlipayInterface mIService;

    private MyConn mConn = null;

    private class MyConn implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            System.out.println("成功绑定到远程alipay服务上");
            mIService = IAlipayInterface.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent();
        intent.setAction("com.example.alipay");
        // 远程调用服务在安卓8.0系统及以上,也需要指定组件名,也就是显式调用
        intent.setComponent(new ComponentName("com.example.alipay", "com.example.alipay.Alipay"));
        mConn = new MyConn();
        bindService(intent, mConn, BIND_AUTO_CREATE);
    }

3、调用方法

 public void click(View view) {
        try {

            int i = mIService.call_safepay();
            Toast.makeText(MainActivity.this,i+"",Toast.LENGTH_SHORT).show();
        } catch (RemoteException e) {
            e.printStackTrace();
        }

    }

代码附件

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_27662081

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值