练习

加减乘除

// IMyAidlInterface.aidl
package com.example.app3_server;

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

interface IMyAidlInterface {
    int divide(int a, int b);
}
package com.example.app3_client;

import android.content.ComponentName;
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.widget.Toast;

import com.example.app3_server.IMyAidlInterface;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ServiceConnection connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                IMyAidlInterface iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
                try {
                    int divide = iMyAidlInterface.divide(9, 3);
                    Toast.makeText(MainActivity.this, "结果:"+divide, Toast.LENGTH_SHORT).show();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };

        Intent intent = new Intent();
        intent.setPackage("com.example.app3_server");
        intent.setAction("pan.sds");

        bindService(intent,connection,BIND_AUTO_CREATE);
    }
}

// IMyAidlInterface.aidl
package com.example.app_server;

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

interface IMyAidlInterface {
    int less(int a, int b);
}

package com.example.app_server;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

public class MyService extends Service {
    public MyService() {
    }

    IBinder iBinder = new IMyAidlInterface.Stub() {
        @Override
        public int less(int a, int b) throws RemoteException {
            return a-b;
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        return iBinder;
    }
}

// IMyAidlInterface.aidl
package com.example.app2.aidl;

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

interface IMyAidlInterface {
    int add(int a,int b);
}
package com.example.apps;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.example.app2.aidl.IMyAidlInterface;

public class MainActivity extends AppCompatActivity {

    //接收服务端的消息
    private Messenger messenger = new Messenger(new Handler(){
        @Override
        public void handleMessage(Message msg) {
            Bundle bundle = msg.getData();
            String message = bundle.getString("server_message");
            Toast.makeText(MainActivity.this, "接收服务", Toast.LENGTH_SHORT).show();
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        ServiceConnection connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                IMyAidlInterface iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
                try {
                    int add = iMyAidlInterface.add(1, 2);
                    Toast.makeText(MainActivity.this, ""+add, Toast.LENGTH_SHORT).show();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };

        Intent intent = new Intent();
        intent.setPackage("com.example.app2");
        intent.setAction("com.pan.server");

        bindService(intent,connection, Service.BIND_AUTO_CREATE);
    }
}

package com.example.app2.service;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.widget.Toast;

import com.example.app2.aidl.IMyAidlInterface;

public class MyService extends Service {
    private static final String TAG = "MyService";

    Messenger server_messager = new Messenger(new Handler(){
        @Override
        public void handleMessage(Message msg) {

//            Bundle bundle = msg.getData();
//            String msg1 = bundle.getString("msg");
//            Toast.makeText(MyService.this, "服务器发送的"+msg1, Toast.LENGTH_SHORT).show();

            Messenger replyTo = msg.replyTo;
            Message message = Message.obtain();
            Bundle bundle1 = new Bundle();
            bundle1.putString("server_message","客户端你是不是傻啊");
            message.setData(bundle1);

            try {
                replyTo.send(message);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    });
    public MyService() {
    }

    IBinder binder = new IMyAidlInterface.Stub() {

        @Override
        public int add(int a, int b) throws RemoteException {
            return a+b;
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        return server_messager.getBinder();
    }
}

package com.example.app2_server;

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

interface IMyAidlInterface {
   int check(int a,int b);
}
package com.example.app2_client;

import android.app.Service;
import android.content.ComponentName;
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.widget.Toast;

import com.example.app2_server.IMyAidlInterface;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ServiceConnection connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                IMyAidlInterface iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
                try {
                    int check = iMyAidlInterface.check(3, 5);
                    Toast.makeText(MainActivity.this, ""+check, Toast.LENGTH_SHORT).show();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };

        Intent intent = new Intent();
        intent.setAction("pan.server2");
        intent.setPackage("com.example.app2_server");

        bindService(intent,connection, Service.BIND_AUTO_CREATE);
    }
}

package com.example.app2_server;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

public class MyService extends Service {
    public MyService() {
    }

    IBinder iBinder = new IMyAidlInterface.Stub() {
        @Override
        public int check(int a, int b) throws RemoteException {
            return a*b;
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        return iBinder;
    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值