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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人工智能 > ChatGpt >内容正文

ChatGpt

九 AIDL

發布時間:2024/8/26 ChatGpt 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 九 AIDL 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
進程 多線程
優點 安全 穩定 擴大內存空間 節約CPU時間
AIDL=Android Interface definition language 使用情況:做一個下載,不想讓你的下載拖垮你的應用;播放器;ADK; ? ----------------------------------------------------------------------------------------- 目的 =Aidl接口+service+activity =提供接口+輸入信息+接收信息 aidl作用 =提供接口 聲明 =另開進程 Service作用 =調用 AidlInterface.Stub+引用接口輸入信息+返回(onBind,stub) Activity作用 =綁定service+得到信息 步驟: 1、自動生成 (1)aidl文件夾 ? main-aidl (2)自動創建AIDL ? ? ? main-aidl-包-AIDL(右鍵創建) (3)自動生成相關代碼 ? ?app-build-generated-source-aidl-debug-點運行 會得到Interface 2、加入接口 ---------------------------------------------------------------------------- 3、輸入信息 ?service (1)創建aidl包 ? ?java-大包-aidl包 (2)創建AIDLService ?? ①繼承 ②綁定 ③ 調用?Interface ④調用接口,放入信息 ⑤聲明另開進程 -------------------------------------------------------------------------------- 4、接收信息 activity 得到信息 ? ? ? ---------------------------------------------------------------------------------------------------- ? interface IMyAidlInterface {
/**
* 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);

String getName(String nickName);
}
聲明進程 <service
android:name=".aidl.AIDLService"
android:process="com.aidl.test.service"
android:enabled="true"
android:exported="true">
</service> service import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;

import com.syz.lianxi.IMyAidlInterface;


public class AIDLService extends Service {

IMyAidlInterface.Stub mStub = new IMyAidlInterface.Stub(){

@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

}

@Override
public String getName(String nickName) throws RemoteException {
return nickName + "aidl_hahaha";
}
}
;


@Nullable
@Override
public IBinder onBind(Intent intent) {
return mStub;
}
}
activity import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.widget.Toast;

import com.syz.lianxi.IMyAidlInterface;
import com.syz.lianxi.R;


public class AIDLActivity extends Activity {

ServiceConnectionmServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mIMyAidlInterface =
IMyAidlInterface.Stub.asInterface(service);
}

@Override
public void onServiceDisconnected(ComponentName name) {

}
}
;

private IMyAidlInterface mIMyAidlInterface;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_aidl);

findViewById(R.id.button_aidl).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mIMyAidlInterface!=null){
try {
String name =
mIMyAidlInterface.getName("nick_know_maco");
Toast.makeText(AIDLActivity.this, name + "", Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
e.printStackTrace()
;
}
}
}
})
;

bindService(new Intent(this, AIDLService.class), mServiceConnection, Context.BIND_AUTO_CREATE);
}

}
------------------------------------------------------------------------------------------------------- 如何使用插件,自動生成接口? import android.os.Parcel;
import android.os.Parcelable;


public class Person implements Parcelable {

String mName;
int mAge;
String mAvatarUrl;

public String getName() {
return mName;
}

public void setName(String name) {
mName = name;
}

public int getAge() {
return mAge;
}

public void setAge(int age) {
mAge = age;
}

public String getAvatarUrl() {
return mAvatarUrl;
}

public void setAvatarUrl(String avatarUrl) {
mAvatarUrl = avatarUrl;
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.mName);
dest.writeInt(this.mAge);
dest.writeString(this.mAvatarUrl);
}

public Person() {
}

protected Person(Parcel in) {
this.mName = in.readString();
this.mAge = in.readInt();
this.mAvatarUrl = in.readString();
}

public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {
public Person createFromParcel(Parcel source) {
return new Person(source);
}

public Person[] newArray(int size) {
return new Person[size];
}
};
}

轉載于:https://www.cnblogs.com/sunyz/p/5450629.html

總結

以上是生活随笔為你收集整理的九 AIDL的全部內容,希望文章能夠幫你解決所遇到的問題。

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