service传值

1.add

server

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 add(int a, int b) throws RemoteException {
            return a+b;
        }
    };

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

client

package com.example.app_client;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.Toast;

import com.example.app_server.IMyAidlInterface;

public class MainActivity extends AppCompatActivity {

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

        ServiceConnection serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                IMyAidlInterface iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
                try {
                    int add = iMyAidlInterface.add(3, 4);
                    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.setAction("com.zrx.111");
        intent.setPackage("com.example.app_server");
        bindService(intent,serviceConnection,Service.BIND_AUTO_CREATE);

    }
}

sub

server

package com.example.sub_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 sub(int a, int b) throws RemoteException {
            return a - b;
        }
    };

    @Override
    public IBinder onBind(Intent intent) {

        return iBinder;
    }
}

client

package com.example.sub_client;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Toast;

import com.example.sub_server.IMyAidlInterface;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private IMyAidlInterface iMyAidlInterface;
    private int sub;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ServiceConnection serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                 iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
                try {
                    sub = iMyAidlInterface.sub(9, 3);

                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                Toast.makeText(MainActivity.this, sub + "", Toast.LENGTH_SHORT).show();
                Log.d(TAG, "onServiceConnected: "+sub+"");
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
        Intent intent = new Intent();
        intent.setAction("com.zrx.123");
        intent.setPackage("com.example.sub_server");
        bindService(intent,serviceConnection, Service.BIND_AUTO_CREATE);
    }
}

mult

srever

package com.example.mult_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 mult(int a, int b) throws RemoteException {
            return a*b;
        }
    };

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

client

package com.example.mult_client;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.Toast;

import com.example.mult_server.IMyAidlInterface;

public class MainActivity extends AppCompatActivity {

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

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

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
        Intent intent = new Intent();
        intent.setAction("com.zrx.111");
        intent.setPackage("com.example.mult_server");
        bindService(intent,serviceConnection, Service.BIND_AUTO_CREATE);
    }
}
发布了27 篇原创文章 · 获赞 0 · 访问量 626

猜你喜欢

转载自blog.csdn.net/zrx_z/article/details/102672948