除
package com. example. app3_server;
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) ;
}
}
减
package com. example. app_server;
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;
}
}
加
package com. example. app2. aidl;
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) {
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;
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;
}
}