日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Android跨进程通信二——AIDL

發布時間:2025/3/21 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android跨进程通信二——AIDL 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


AIDL全稱Android Interface Definition Language即安卓接口定義語言。主要用于多進程通信。比Messenger,它具有支持多線程優勢



注意事項:

  • 為了線程安全考慮,服務端可以使用多線程處理到來的請求

  • 客戶端發送給服務端的請求如果需要好幾毫秒處理時間,為了防止客戶端出現ANR(程序未響應)問題,則客戶端需要單獨開一個線程用于請求。

  • 不要將線程拋回給請求方


配置文件

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.androidClient" ><application android:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme" ><activity android:name="com.example.androidClient.MainActivity" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><service android:name=".RemoteService"android:enabled="true"android:exported="true"android:process=":remote"></service></application></manifest>

布局

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:paddingBottom="@dimen/activity_vertical_margin"tools:context=".MainActivity"android:orientation="vertical"><Button android:id="@+id/bind_service"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@color/colorPrimary"android:onClick="bind_service"android:text="綁定服務"/><Button android:id="@+id/unbind_service"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@color/colorPrimary"android:layout_marginTop="20dp"android:onClick="unbind_service"android:text="解除綁定"/><TextView android:layout_width="match_parent"android:layout_height="wrap_content"android:text="信息提示:"android:layout_marginTop="20dp"/><TextView android:id="@+id/message"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout>

定義AIDL文件

// IRemoteService.aidl package com.example.androidClient;// Declare any non-default types here with import statementsinterface IRemoteService {/*** Demonstrates some basic types that you can use as parameters* and return values in AIDL.*/void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,double aDouble, String aString);int getPid();int sum(int a, int b);}

服務端

package com.example.androidClient;import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.Process; import android.os.RemoteException;/*** 服務端的服務*/ public class RemoteService extends Service {@Overridepublic void onCreate() {super.onCreate();}@Overridepublic IBinder onBind(Intent intent) {return mBinder;}public IRemoteService.Stub mBinder = new IRemoteService.Stub() {@Overridepublic void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {}@Overridepublic int getPid() throws RemoteException {return Process.myPid();}@Overridepublic int sum(int a, int b) throws RemoteException {return a+b;}}; }

客戶端

package com.example.androidClient;import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.*; import android.os.Process; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; import android.widget.Toast;public class MainActivity extends AppCompatActivity {TextView message;IRemoteService mService;private ServiceConnection mServiceConnection = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {mService = IRemoteService.Stub.asInterface(service);Toast.makeText(MainActivity.this,"success",Toast.LENGTH_SHORT).show();try {message.setText("Caller Process ID: "+ Process.myPid()+"\n" +"Service Process ID: "+mService.getPid()+"\n"+"Result: "+mService.sum(2,3));} catch (RemoteException e) {e.printStackTrace();}}@Overridepublic void onServiceDisconnected(ComponentName name) {mService = null;Toast.makeText(MainActivity.this,"failure",Toast.LENGTH_SHORT).show();}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);message = (TextView) findViewById(R.id.message);}public void bind_service(View view){/*Intent intent = new Intent();intent.setAction("com.example.android.remote_service");intent.setPackage("com.example.android");*/Intent intent = new Intent(this,RemoteService.class);bindService(intent,mServiceConnection, Context.BIND_AUTO_CREATE);}public void unbind_service(View view){unbindService(mServiceConnection);} }

效果圖



內容來自:https://developer.android.com/guide/components/aidl.html

總結

以上是生活随笔為你收集整理的Android跨进程通信二——AIDL的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。