AIDL簡單使用(經過它跨應用傳遞對象(數據))

服務端步驟:java


一、新建一個包 在包裏新建一個接口MyServiceAIDL(接口名本身取)android

 

代碼app

//接口前面不要有public 等 否則會報錯
interface MyServiceAIDL {
//這個接口內的方法能夠本身根據業務需求本身添加相應的方法
//這裏簡單建一個getData方法
//方法名前面不要有public 等否則會報錯
 String getData();
}

 


二、建無缺接口須要到找到你新建的接口類,工程保存路徑:右擊工程-->properties-->右邊有個Location位置
根據該位置找到本身剛剛新建的接口類,而後把後綴名改成.aidl 在回到本身工程刷新一下ide

 

 

三、在建一個類繼承Servicethis

 

代碼spa

 public class MyService extends Service {
 @Override
 public void onCreate() {
  // TODO Auto-generated method stub
  super.onCreate();
 }
 
 @Override
 public IBinder onBind(Intent intent) {
  // TODO Auto-generated method stub
  return new MyBindService();
 }
 
 @Override
 public boolean onUnbind(Intent intent) {
  // TODO Auto-generated method stub
  return super.onUnbind(intent);
 }
 
 @Override
 public void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();
 }
 
 //AIDL --> 要繼承Stub 而不是Binder
 //而後還要在配置清單裏添加服務(隱式意圖)
 class MyBindService extends Stub{
  @Override
  public String getData() throws RemoteException {
   // TODO Auto-generated method stub
   return "(跨進程)服務端返回來的消息";
  }
  
 }
}

 

 

四、在配置清單裏註冊建立的服務code

代碼xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.service.aidl"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <!-- android:exported="true"表示能夠跨進程訪問
        android:enabled="true"表示激活
        action 表示客戶端要訪問在這裏的服務的路徑action -->
        <service android:name=".MyService"
            android:enabled="true"
            android:exported="true">
            <intent-filter >
                <action android:name="com.examp.remote.service.MyService"/>
            </intent-filter>
        </service>
        
    </application>
</manifest>

 

 

 

 

 

========================================================對象

 

 

 

 


服務端寫好後寫客戶端繼承

 

客戶端步驟:


一、把服務端新建的接口連同包一塊兒拷貝到客戶端的源文件(src)下

 


二、MainActivity.java類


代碼

public class MainActivity extends Activity {
 private TextView text_info;
 private MyServiceAIDL myServiceAIDL;//聲明本身建的接口對象
 private ServiceConnection connection = new ServiceConnection() {
  @Override
  public void onServiceDisconnected(ComponentName name) {
   // TODO Auto-generated method stub
   
  }
  
  @Override
  public void onServiceConnected(ComponentName name, IBinder binder) {
   //經過Stub.asInterface方法把IBinder返回來的對象轉換而後賦值給MyServiceAIDL接口聲明的對象
   myServiceAIDL = Stub.asInterface(binder);
   
  }
 };
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  this.text_info = (TextView) this.findViewById(R.id.textview);
  
  bindMyService();//綁定服務
 }
 
 private void bindMyService() {
  Intent intent = new Intent();
  
  //經過隱式意圖來綁定服務
  intent.setAction("com.examp.remote.service.MyService");
  bindService(intent, connection, Context.BIND_AUTO_CREATE);
 }
 
 //按鈕的事件監聽
 public void bindRemoteService(View view){
  try {
   //這裏經過本身建立的接口對象來獲取服務端的數據
   text_info.setText(myServiceAIDL.getData());
  } catch (RemoteException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

 

 

效果圖示例:

 

 

 

相關文章
相關標籤/搜索