Android Interface definition language用于多个进程之间的通信
启动其他应用里面的service在Android5.0以后不能通过隐式Intent启动,只能通过显式Intent启动。
通过setComponent(包名,类名)显式启动其他应用Service。
绑定其他应用的service并进行通信:
用AIDL通信时,需要保证AIDL文件的包名一样,然后复制AIDL文件至anotherapp。
在service中实现aidl中的接口,并在service运行时写一个输出线程。
public class AppService
extends Service {
public AppService() {
}
@Override
public IBinder onBind(Intent intent) {
return new IerviceRemoteBinder.Stub(){
@Override
public void basicTypes( int anInt , long aLong , boolean aBoolean , float aFloat , double aDouble , String aString) throws RemoteException {
}
@Override
public void setData(String data) throws RemoteException {
AppService. this. data = data ;
}
} ;
}
@Override
public void onCreate() {
super.onCreate() ;
System. out.println( "service started") ;
new Thread(){
@Override
public void run() {
super.run() ;
running = true;
while ( running){
System. out.println( data) ;
try {
Thread. sleep( 1000) ;
} catch (InterruptedException e) {
e.printStackTrace() ;
}
}
}
}.start() ;
}
@Override
public int onStartCommand(Intent intent , int flags , int startId) {
return super.onStartCommand(intent , flags , startId) ;
}
@Override
public void onDestroy() {
super.onDestroy() ;
System. out.println( "service destory") ;
running = false;
}
private String data = " 初始值 " ;
private Boolean running = false;
}
public AppService() {
}
@Override
public IBinder onBind(Intent intent) {
return new IerviceRemoteBinder.Stub(){
@Override
public void basicTypes( int anInt , long aLong , boolean aBoolean , float aFloat , double aDouble , String aString) throws RemoteException {
}
@Override
public void setData(String data) throws RemoteException {
AppService. this. data = data ;
}
} ;
}
@Override
public void onCreate() {
super.onCreate() ;
System. out.println( "service started") ;
new Thread(){
@Override
public void run() {
super.run() ;
running = true;
while ( running){
System. out.println( data) ;
try {
Thread. sleep( 1000) ;
} catch (InterruptedException e) {
e.printStackTrace() ;
}
}
}
}.start() ;
}
@Override
public int onStartCommand(Intent intent , int flags , int startId) {
return super.onStartCommand(intent , flags , startId) ;
}
@Override
public void onDestroy() {
super.onDestroy() ;
System. out.println( "service destory") ;
running = false;
}
private String data = " 初始值 " ;
private Boolean running = false;
}
在另一app中输入一个值并传入service中进行修改:
public class MainActivity
extends ActionBarActivity
implements View.OnClickListener
, ServiceConnection {
EditText input ;
private Intent serviceIntent ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState) ;
setContentView(R.layout. activity_main) ;
serviceIntent = new Intent() ;
serviceIntent.setComponent( new ComponentName( "com.example.wangxiang.remoteservicecomunication" , "com.example.wangxiang.remoteservicecomunication.AppService")) ;
findViewById(R.id. button).setOnClickListener( this) ;
findViewById(R.id. button2).setOnClickListener( this) ;
findViewById(R.id. button3).setOnClickListener( this) ;
findViewById(R.id. button4).setOnClickListener( this) ;
findViewById(R.id. button5).setOnClickListener( this) ;
input = (EditText) findViewById(R.id. etInput) ;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id. button:
startService( serviceIntent) ; //通过全路径启动另一个app里面的service
break;
case R.id. button2:
stopService( serviceIntent) ; //通过全路径停止另一个app里面的service
break;
case R.id. button3:
bindService( serviceIntent ,this, Context. BIND_AUTO_CREATE) ;
break;
case R.id. button4:
unbindService( this) ;
binder = null;
break;
case R.id. button5:
if( binder != null){
try {
binder.setData( input.getText().toString()) ;
} catch (RemoteException e) {
e.printStackTrace() ;
}
}
break;
}
}
@Override
public void onServiceConnected(ComponentName name , IBinder service) {
System. out.println( "bind service") ;
System. out.println(service) ;
binder = IerviceRemoteBinder.Stub. asInterface(service) ;
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
private IerviceRemoteBinder binder = null;
}
EditText input ;
private Intent serviceIntent ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState) ;
setContentView(R.layout. activity_main) ;
serviceIntent = new Intent() ;
serviceIntent.setComponent( new ComponentName( "com.example.wangxiang.remoteservicecomunication" , "com.example.wangxiang.remoteservicecomunication.AppService")) ;
findViewById(R.id. button).setOnClickListener( this) ;
findViewById(R.id. button2).setOnClickListener( this) ;
findViewById(R.id. button3).setOnClickListener( this) ;
findViewById(R.id. button4).setOnClickListener( this) ;
findViewById(R.id. button5).setOnClickListener( this) ;
input = (EditText) findViewById(R.id. etInput) ;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id. button:
startService( serviceIntent) ; //通过全路径启动另一个app里面的service
break;
case R.id. button2:
stopService( serviceIntent) ; //通过全路径停止另一个app里面的service
break;
case R.id. button3:
bindService( serviceIntent ,this, Context. BIND_AUTO_CREATE) ;
break;
case R.id. button4:
unbindService( this) ;
binder = null;
break;
case R.id. button5:
if( binder != null){
try {
binder.setData( input.getText().toString()) ;
} catch (RemoteException e) {
e.printStackTrace() ;
}
}
break;
}
}
@Override
public void onServiceConnected(ComponentName name , IBinder service) {
System. out.println( "bind service") ;
System. out.println(service) ;
binder = IerviceRemoteBinder.Stub. asInterface(service) ;
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
private IerviceRemoteBinder binder = null;
}
AndroidManifest中的service配置:
<service
android :name= ".AppService"
android :enabled= "true"
android :exported= "true" >
<intent-filter>
<action android :name= "android.intent.action.AIDLService" />
<category android :name= "android.intent.category.DEFAULT" />
</intent-filter>
</service>
android :name= ".AppService"
android :enabled= "true"
android :exported= "true" >
<intent-filter>
<action android :name= "android.intent.action.AIDLService" />
<category android :name= "android.intent.category.DEFAULT" />
</intent-filter>
</service>