Android Studio 1-16 Service進程間通信
方法一
服務端
package com. example. messager_server;
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 androidx. annotation. NonNull;
public class MyService extends Service {
final Messenger messenger = new Messenger ( new Handler ( ) {
@Override
public void handleMessage ( @NonNull Message msg) {
super . handleMessage ( msg) ;
Bundle data = msg. getData ( ) ;
String string = data. getString ( "hello" , "king" ) ;
Toast. makeText ( MyService. this , "" + string, Toast. LENGTH_SHORT) . show ( ) ;
Messenger replyTo = msg. replyTo;
Message message = new Message ( ) ;
Bundle bundle = new Bundle ( ) ;
bundle. putString ( "person" , "諸葛亮" ) ;
message. setData ( bundle) ;
try {
replyTo. send ( message) ;
} catch ( RemoteException e) {
e. printStackTrace ( ) ;
}
}
} ) ;
public MyService ( ) {
}
@Override
public IBinder onBind ( Intent intent) {
return messenger. getBinder ( ) ;
}
}
< service
android: name= ".MyService"
android: enabled= "true"
android: exported= "true" >
< intent- filter>
< action android: name= "com.king" > < / action>
< / intent- filter>
< / service>
客戶端
package com. example. messager_client;
import androidx. annotation. NonNull;
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. Handler;
import android. os. IBinder;
import android. os. Message;
import android. os. Messenger;
import android. os. RemoteException;
import android. widget. Toast;
public class MainActivity extends AppCompatActivity {
private Messenger clientMessage = new Messenger ( new Handler ( ) {
@Override
public void handleMessage ( @NonNull Message msg) {
super . handleMessage ( msg) ;
Bundle data = msg. getData ( ) ;
String person = data. getString ( "person" ) ;
Toast. makeText ( MainActivity. this , "" + "來自服務器的消息是------" + person, Toast. LENGTH_SHORT) . show ( ) ;
}
} ) ;
private Messenger serverMessage;
private ServiceConnection connection = new ServiceConnection ( ) {
@Override
public void onServiceConnected ( ComponentName componentName, IBinder iBinder) {
serverMessage = new Messenger ( iBinder) ;
Message message = new Message ( ) ;
Bundle bundle = new Bundle ( ) ;
bundle. putString ( "hello" , "服務器你好 我是客戶端" ) ;
message. setData ( bundle) ;
message. replyTo = clientMessage;
try {
serverMessage. send ( message) ;
} catch ( RemoteException e) {
e. printStackTrace ( ) ;
}
}
@Override
public void onServiceDisconnected ( ComponentName componentName) {
}
} ;
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
setContentView ( R. layout. activity_main) ;
Intent intent = new Intent ( ) ;
intent. setAction ( "com.king" ) ;
intent. setPackage ( "com.example.messager_server" ) ;
bindService ( intent, connection, Service. BIND_AUTO_CREATE) ;
}
@Override
protected void onDestroy ( ) {
super . onDestroy ( ) ;
unbindService ( connection) ;
}
}
方法二
服務端
package com. example. aidl_server;
interface IMyAidlInterface {
int add ( int a, int b) ;
int multiply ( int a, int b) ;
}
package com. example. aidl_server;
import android. app. Service;
import android. content. Intent;
import android. os. IBinder;
import android. os. RemoteException;
public class MyService extends Service {
public MyService ( ) {
}
@Override
public IBinder onBind ( Intent intent) {
return iBinder;
}
IBinder iBinder = new IMyAidlInterface. Stub ( ) {
@Override
public int add ( int a, int b) throws RemoteException {
return a + b;
}
@Override
public int multiply ( int a, int b) throws RemoteException {
return a * b;
}
} ;
}
< service
android: name= ".MyService"
android: enabled= "true"
android: exported= "true" >
< intent- filter>
< action android: name= "com.hong.king" > < / action>
< / intent- filter>
< / service>
客戶端
package com. example. aidl_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. aidl_server. IMyAidlInterface;
public class MainActivity extends AppCompatActivity {
private IMyAidlInterface iMyAidlInterface;
private ServiceConnection connection;
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
setContentView ( R. layout. activity_main) ;
Intent intent = new Intent ( ) ;
intent. setAction ( "com.hong.king" ) ;
intent. setPackage ( "com.example.aidl_server" ) ;
connection = new ServiceConnection ( ) {
@Override
public void onServiceConnected ( ComponentName componentName, IBinder iBinder) {
iMyAidlInterface = IMyAidlInterface. Stub. asInterface ( iBinder) ;
try {
int num= iMyAidlInterface. add ( 4 , 6 ) ;
int multiply = iMyAidlInterface. multiply ( 3 , 7 ) ;
Toast. makeText ( MainActivity. this , "" + num, Toast. LENGTH_SHORT) . show ( ) ;
Toast. makeText ( MainActivity. this , "" + multiply, Toast. LENGTH_SHORT) . show ( ) ;
} catch ( RemoteException e) {
e. printStackTrace ( ) ;
}
}
@Override
public void onServiceDisconnected ( ComponentName componentName) {
}
} ;
bindService ( intent, connection, Service. BIND_AUTO_CREATE) ;
}
@Override
protected void onDestroy ( ) {
super . onDestroy ( ) ;
unbindService ( connection) ;
}
}