九 AIDL

来源:http://www.cnblogs.com/sunyz/archive/2016/05/01/5450629.html
-Advertisement-
Play Games

多進程 多線程 優點 安全 穩定 擴大記憶體空間 節約CPU時間 AIDL=Android Interface definition language 多進程 多線程 優點 安全 穩定 擴大記憶體空間 節約CPU時間 使用情況:做一個下載,不想讓你的下載拖垮你的應用;播放器;ADK; 目的 =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];
}
};
}

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 學習要點: 1.線性漸變 2.徑向漸變 主講教師:李炎恢 本章主要探討 HTML5 中 CSS3 背景漸變功能,主要有兩種漸變方式:線性漸變和徑向(放射性)漸變。 一.線性漸變 CSS3 提供了 linear-gradient 屬性實現背景顏色的漸變功能。在以前,這種效果必須採用圖片才能實現的。首先 ...
  • 學習要點: 1.文本陰影 2.文本裁剪 3.文本描邊 4.文本填充 主講教師:李炎恢 本章主要探討 HTML5 中 CSS3 中文本效果,其中也包含一些之前講過的 CSS3 文本屬性。 一.文本陰影 CSS3 提供了 text-shadow 文本陰影效果,這個屬性在之前講過,只是沒有涉及瀏覽器支持情 ...
  • 代碼: 代理方法: ...
  • <merge /> 標簽在你嵌套 Layout 時取消了 UI 層級中冗餘的 ViewGroup 。比如,如果你有一個 Layout 是一個豎直方向的 LinearLayout,其中包含兩個連續的 View 可以在別的 Layout 中重用,那麼你會做一個 LinearLayout 來包含這兩個 V ...
  • eclipse自動退出 今天打開eclipse的時候出現自動退出,也就是打不開了 我上網查了一下,把工作區間改了一下就好了 修改預設工作區間的如下:(不是預設的話直接修改就好) eclipse\configuration\.settings目錄下,有一個“org.eclipse.ui.ide.pre ...
  • 在 oncreateView 裡面 進行頁面初始化 view view.findViewById this 改成 getActivity() ...
  • UITableViewCell圖片視差效果 效果 源碼 https://github.com/YouXianMing/Animations 細節 OffsetImageCell.m OffsetCellViewController.m ...
  • 1. 類型安全和類型推測 1> 類型安全 Swift 是一個 _類型安全(type safe)_ 的語言。類型安全的語言可以讓你清楚地知道代碼要處理的值的類型。如果你的代碼需要一個`String`,你絕對不可能不小心傳進去一個`Int`。 由於 Swift 是類型安全的,所以它會在編譯你的代碼時進行 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...