重點擼代碼:異步
一、跨設備啓動FA、跨設備遷移、回遷async
(1)權限分佈式
ohos.permission.DISTRIBUTED_DEVICE_STATE_CHANGE:用於容許監聽分佈式組網內的設備狀態變化。 ohos.permission.GET_DISTRIBUTED_DEVICE_INFO:用於容許獲取分佈式組網內的設備列表和設備信息。 ohos.permission.GET_BUNDLE_INFO:用於查詢其餘應用的信息。 ohos.permission.DISTRIBUTED_DATASYNC:用於容許不一樣設備間的數據交換。 "reqPermissions": [ {"name": "ohos.permission.DISTRIBUTED_DATASYNC"}, {"name": "ohos.permission.DISTRIBUTED_DEVICE_STATE_CHANGE"}, {"name": "ohos.permission.GET_DISTRIBUTED_DEVICE_INFO" }, {"name": "ohos.permission.GET_BUNDLE_INFO"} ] //主動申明,要多設備協同,讓用戶選擇容許仍是禁止 requestPermissionsFromUser(new String[]{"ohos.permission.DISTRIBUTED_DATASYNC"}, 0);
(2)界面:ability_main.xmlide
<?xml version="1.0" encoding="utf-8"?> <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:orientation="vertical"> <Button ohos:id="$+id:main_start_fa_btn" ohos:height="match_content" ohos:width="300vp" ohos:text="1.啓動遠程設備的FA" ohos:text_size="20fp" ohos:text_color="#ffffff" ohos:background_element="$graphic:button_bg" ohos:layout_alignment="horizontal_center" ohos:top_padding="8vp" ohos:bottom_padding="8vp" ohos:left_padding="40vp" ohos:right_padding="40vp" ohos:top_margin="20vp" /> <Button ohos:id="$+id:main_migration_btn" ohos:height="match_content" ohos:width="300vp" ohos:text="2.遷移到遠程設備" ohos:text_size="20fp" ohos:text_color="#ffffff" ohos:background_element="$graphic:button_bg" ohos:layout_alignment="horizontal_center" ohos:top_padding="8vp" ohos:bottom_padding="8vp" ohos:left_padding="40vp" ohos:right_padding="40vp" ohos:top_margin="20vp" /> </DirectionalLayout>
button_bg.xml函數
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:shape="rectangle"> <solid ohos:color="#007DFF"/> <corners ohos:radius="40"/> </shape>
另外咱們須要的Page Abiltiy:MigrationAbility、RemoveAbility
MainAbilitySlice:工具
public class MainAbilitySlice extends AbilitySlice { private Button mainStartFABtn,mainMigrationBtn; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); mainStartFABtn = (Button)findComponentById(ResourceTable.Id_main_start_fa_btn); mainMigrationBtn = (Button)findComponentById(ResourceTable.Id_main_migration_btn); mainStartFABtn.setClickedListener(mClickListener); mainMigrationBtn.setClickedListener(mClickListener); } private Component.ClickedListener mClickListener = new Component.ClickedListener() { @Override public void onClick(Component component) { int compoentId = component.getId(); switch (compoentId){ case ResourceTable.Id_main_start_fa_btn: //點擊後跨設備打開Fa //第一種寫法 Intent intent = new Intent(); Operation op = new Intent.OperationBuilder() .withDeviceId(Common.getOnLineDeviceId()) .withBundleName("com.ybzy.demo") .withAbilityName("com.ybzy.demo.RemoveAbility") .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE) .build(); intent.setOperation(op); intent.setParam("msg","我誇設備把你這個FA拉起來了!"); startAbility(intent); //第二鍾寫法 intent.setElement(new ElementName(Common.getOnLineDeviceId() ,"com.ybzy.demo","com.ybzy.demo.RemoveAbility")); intent.setFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE); intent.setParam("msg","我誇設備把你這個FA拉起來了!"); startAbility(intent); break; case ResourceTable.Id_main_migration_btn: //點擊後進入要遷移的Ability頁面 Intent migrationIntent = new Intent(); migrationIntent.setElement(new ElementName("","com.ybzy.demo" ,"com.ybzy.demo.MigrationAbility")); startAbility(migrationIntent); break; default: break; } } }; @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } }
ability_migration.xmloop
<?xml version="1.0" encoding="utf-8"?> <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:background_element="#00ffff" ohos:orientation="vertical"> <Text ohos:id="$+id:migration_text" ohos:height="match_content" ohos:width="250vp" ohos:background_element="#0088bb" ohos:layout_alignment="horizontal_center" ohos:text="下面是一個可編輯的文本框" ohos:text_size="50" ohos:padding="5vp" ohos:top_margin="30vp" /> <TextField ohos:id="$+id:migration_textfield" ohos:height="250vp" ohos:width="250vp" ohos:hint="請輸入..." ohos:layout_alignment="horizontal_center" ohos:background_element="#ffffff" ohos:text_color="#888888" ohos:text_size="20fp" ohos:padding="5vp" /> <Button ohos:id="$+id:migration_migration_btn" ohos:height="match_content" ohos:width="match_content" ohos:text="點擊遷移" ohos:text_size="20fp" ohos:text_color="#ffffff" ohos:background_element="$graphic:button_bg" ohos:top_padding="8vp" ohos:bottom_padding="8vp" ohos:left_padding="50vp" ohos:right_padding="50vp" ohos:layout_alignment="horizontal_center" ohos:top_margin="30vp" /> <Button ohos:id="$+id:migration_migration_back_btn" ohos:height="match_content" ohos:width="match_content" ohos:text="點擊遷移回來" ohos:text_size="20fp" ohos:text_color="#ffffff" ohos:background_element="$graphic:button_bg" ohos:top_padding="8vp" ohos:bottom_padding="8vp" ohos:left_padding="50vp" ohos:right_padding="50vp" ohos:layout_alignment="horizontal_center" ohos:top_margin="30vp" /> </DirectionalLayout>
RemoveAbility...把接收到的值顯示到頁面就行,setText()佈局
(3)工具類大數據
public class Common{ public static String getDeviceId(){ List<DeviceInfo> deviceList = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE); if(deviceList.isEmpty()){ return null; } int deviceNum = deviceList.size(); List<String> deviceIds = new ArrayList<>(deviceNum); List<String> deviceNames = new ArrayList<>(deviceNum); deviceList.forEach((device)->{ deviceIds.add(device.getDeviceId()); deviceNames.add(device.getDeviceName()); }); //咱們這裏的實驗環境,就兩部手機,組件還沒講 //我就直接使用deviceIds的第一個元素,作爲啓動遠程設備的目標id String devcieIdStr = deviceIds.get(0); return devcieIdStr; } public static void myShowTip(Context context,String msg){ //提示框的核心組件文本 Text text = new Text(context); text.setWidth(MATCH_CONTENT); text.setHeight(MATCH_CONTENT); text.setTextSize(16, Text.TextSizeType.FP); text.setText(msg); text.setPadding(30,20,30,20); text.setMultipleLine(true); text.setMarginLeft(30); text.setMarginRight(30); text.setTextColor(Color.WHITE); text.setTextAlignment(TextAlignment.CENTER); //給上面的文本設置一個背景樣式 ShapeElement style = new ShapeElement(); style.setShape(ShapeElement.RECTANGLE); style.setRgbColor(new RgbColor(77,77,77)); style.setCornerRadius(15); text.setBackground(style); //構建存放上面的text的佈局 DirectionalLayout mainLayout = new DirectionalLayout(context); mainLayout.setWidth(MATCH_PARENT); mainLayout.setHeight(MATCH_CONTENT); mainLayout.setAlignment(LayoutAlignment.CENTER); mainLayout.addComponent(text); //最後要讓上面的組件綁定dialog ToastDialog toastDialog = new ToastDialog(context); toastDialog.setSize(MATCH_PARENT,MATCH_CONTENT); toastDialog.setDuration(1500); toastDialog.setAutoClosable(true); toastDialog.setTransparent(true); toastDialog.setAlignment(LayoutAlignment.CENTER); toastDialog.setComponent((Component) mainLayout); toastDialog.show(); } }
(4)實現功能
MigrationAbilitySlice:ui
public class MigrationAbilitySlice extends AbilitySlice implements IAbilityContinuation { TextField migrationTextField; Button migrationMigrationBtn,migrationMigrationBackBtn; String msg = ""; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_migration); migrationTextField = (TextField)findComponentById(ResourceTable.Id_migration_textfield); migrationTextField.setText(msg); migrationMigrationBtn = (Button)findComponentById(ResourceTable.Id_migration_migration_btn); migrationMigrationBtn.setClickedListener(component -> { //一、要進行遷移的Ability實現接口 IAbilityContinuation,實現的方法返回值改爲true //二、要進行遷移的Ability下面關聯的全部AbilitySlice都要實現接口 IAbilityContinuation, // 實現的方法上處理數據 //三、進行遷移 String deviceId = Common.getOnLineDeviceId(); if(deviceId != null){ // continueAbility(deviceId); continueAbilityReversibly(deviceId); } }); migrationMigrationBackBtn = (Button) findComponentById(ResourceTable .Id_migration_migration_back_btn); migrationMigrationBackBtn.setClickedListener(component -> { reverseContinueAbility(); }); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } @Override public boolean onStartContinuation() { return true; } @Override public boolean onSaveData(IntentParams intentParams) { intentParams.setParam("msg",migrationTextField.getText()); return true; } @Override public boolean onRestoreData(IntentParams intentParams) { msg = intentParams.getParam("msg").toString(); // getUITaskDispatcher().asyncDispatch(() -> { // migrationTextField.setText(intentParams.getParam("msg").toString()); // }); return true; } @Override public void onCompleteContinuation(int i) { } }
二、跨設備鏈接Service
啓動遠程設備Service的代碼示例以下:
添加按鈕:
<Button ohos:id="$+id:main_start_removeService_btn" ohos:height="match_content" ohos:width="300vp" ohos:text="遠程啓動ServiceAbility" ohos:text_size="20fp" ohos:text_color="#ffffff" ohos:background_element="$graphic:button_bg" ohos:layout_alignment="horizontal_center" ohos:top_padding="8vp" ohos:bottom_padding="8vp" ohos:left_padding="40vp" ohos:right_padding="40vp" ohos:top_margin="20vp" /> <Button ohos:id="$+id:main_stop_removeService_btn" ohos:height="match_content" ohos:width="300vp" ohos:text="遠程關閉ServiceAbility" ohos:text_size="20fp" ohos:text_color="#ffffff" ohos:background_element="$graphic:button_bg" ohos:layout_alignment="horizontal_center" ohos:top_padding="8vp" ohos:bottom_padding="8vp" ohos:left_padding="40vp" ohos:right_padding="40vp" ohos:top_margin="20vp" />
新建RemoteServiceAbility:
public class RemoteServiceAbility extends Ability { private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo"); private Source sVideoSource; private Player sPlayer; @Override public void onStart(Intent intent) { HiLog.error(LABEL_LOG, "RmoteServiceAbility::onStart"); super.onStart(intent); Common.myShowTip(this,"remote onstart"); sPlayer = new Player(RemoteServiceAbility.this); new PlayerThread().start(); } class PlayerThread extends Thread { @Override public void run() { try { File mp3FilePath = getExternalFilesDir(Environment.DIRECTORY_MUSIC); if (!mp3FilePath.exists()) { mp3FilePath.mkdirs(); } File mp3File = new File(mp3FilePath.getAbsolutePath() + "/" + "bj.mp3"); Resource res = getResourceManager() .getRawFileEntry("resources/rawfile/bj.mp3").openRawFile(); byte[] buf = new byte[4096]; int count = 0; FileOutputStream fos = new FileOutputStream(mp3File); while ((count = res.read(buf)) != -1) { fos.write(buf, 0, count); } FileDescriptor fileDescriptor = new FileInputStream(mp3File).getFD(); sVideoSource = new Source(fileDescriptor); sPlayer.setSource(sVideoSource); sPlayer.prepare(); sPlayer.setVolume(0.3f); sPlayer.enableSingleLooping(true); sPlayer.play(); } catch (IOException e) { e.printStackTrace(); } } } @Override public void onStop() { super.onStop(); Common.myShowTip(RemoteServiceAbility.this,"remote onStop"); sPlayer.stop(); } @Override public IRemoteObject onConnect(Intent intent) { return null; } @Override public void onDisconnect(Intent intent) { } }
啓動:
case ResourceTable.Id_main_start_remoteService_btn: Common.myShowTip(MainAbilitySlice.this,deviceId); Intent startRemoteServiceIntent = new Intent(); startRemoteServiceIntent.setElement(new ElementName( deviceId, "com.ybzy.demo", "com.ybzy.demo.RemoteServiceAbility" )); startRemoteServiceIntent.setFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE); startAbility(startRemoteServiceIntent); break;
關閉遠程設備Service:
case ResourceTable.Id_main_stop_remoteService_btn: Common.myShowTip(MainAbilitySlice.this,deviceId); Intent stopRemoteServiceIntent = new Intent(); stopRemoteServiceIntent.setElement(new ElementName( deviceId, "com.ybzy.demo", "com.ybzy.demo.RemoteServiceAbility" )); stopRemoteServiceIntent.setFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE); stopAbility(stopRemoteServiceIntent); break;
僅經過啓動和中止Service Ability兩種方式對Service進行調度沒法應對需長期交互的場景,
簡單地說,信息就是隻能去,回不來!
所以,分佈式任務調度平臺向開發者提供了跨設備Service鏈接及斷開鏈接的能力。
連接上了,信息可去可回!
連接是使用connectAbility()方法,須要傳入目標Service的Intent與接口IAbilityConnection的實例對象。
接口IAbilityConnection提供了兩個方法供開發者實現:
(1)onAbilityConnectDone()用來處理鏈接的回調。
(2)onAbilityDisconnectDone()用來處理斷開鏈接的回調。
咱們能夠在onAbilityConnectDone()中獲取管理連接的代理,進一步爲了使用該代理跨設備調度Service,
開發者須要在本地及遠端分別實現對外接口一致的代理,這個接口是IRemoteBroker。
添加按鈕:
<Button ohos:id="$+id:main_connect_remoteService_btn" ohos:height="match_content" ohos:width="300vp" ohos:text="遠程連接ServiceAbility" ohos:text_size="20fp" ohos:text_color="#ffffff" ohos:background_element="$graphic:button_bg" ohos:layout_alignment="horizontal_center" ohos:top_padding="8vp" ohos:bottom_padding="8vp" ohos:left_padding="40vp" ohos:right_padding="40vp" ohos:top_margin="20vp" /> <Button ohos:id="$+id:main_use_remoteService_btn" ohos:height="match_content" ohos:width="300vp" ohos:text="使用遠程ServiceAbility" ohos:text_size="20fp" ohos:text_color="#ffffff" ohos:background_element="$graphic:button_bg" ohos:layout_alignment="horizontal_center" ohos:top_padding="8vp" ohos:bottom_padding="8vp" ohos:left_padding="40vp" ohos:right_padding="40vp" ohos:top_margin="20vp" /> <Button ohos:id="$+id:main_disconnect_remoteService_btn" ohos:height="match_content" ohos:width="300vp" ohos:text="遠程斷開ServiceAbility" ohos:text_size="20fp" ohos:text_color="#ffffff" ohos:background_element="$graphic:button_bg" ohos:layout_alignment="horizontal_center" ohos:top_padding="8vp" ohos:bottom_padding="8vp" ohos:left_padding="40vp" ohos:right_padding="40vp" ohos:top_margin="20vp" />
發起鏈接的本地側的代理示例以下:
public class MyRemoteProxy implements IRemoteBroker { //IRemoteBroker:獲取遠程代理對象的持有者 private static final int ERR_OK = 0; //COMMAND_PLUS表示有效消息進行通訊的約定的標記,MIN_TRANSACTION_ID是這個標記能夠用的最小值:1 private static final int COMMAND_PLUS = IRemoteObject.MIN_TRANSACTION_ID; //IRemoteObject:此接口 // 可用於查詢或獲取接口描述符、 // 添加或刪除死亡通知、 // 將對象狀態轉儲到特定文件以及發送消息。 private final IRemoteObject remote; public MyRemoteProxy(IRemoteObject remote) { this.remote = remote; } @Override public IRemoteObject asObject() {//獲取遠程代理對象的方法 return remote; } public int plus(int a,int b) throws RemoteException { //MessageParcel:這個類提供了讀寫對象、接口標記、文件描述符和大數據的方法。 MessageParcel data = MessageParcel.obtain(); //obtain()建立索引爲0的空MessageParcel對象 MessageParcel reply = MessageParcel.obtain(); //MessageOption:定義與sendRequest一塊兒發送消息的選項。 // option不一樣的取值,決定採用同步或異步方式跨設備調用Service // 這個例子咱們須要同步獲取對端Service執行加法運算後的結果,同步模式調用sendRequest接口,即MessageOption.TF_SYNC // 對應的是異步:TF_ASYNC MessageOption option = new MessageOption(MessageOption.TF_SYNC); data.writeInt(a); data.writeInt(b); try { remote.sendRequest(COMMAND_PLUS, data, reply, option); //第1個參數:約定通訊雙方肯定的消息標記。 //第2個參數:發送到對等端側的數據包裹MessageParcel對象。 //第3個參數:對等端側返回的數據包裹MessageParcel對象。 //第4個參數:設置發送消息,用同步仍是異步模式。 int ec = reply.readInt(); //返回通訊成不成功,約定的標記ERR_OK if (ec != ERR_OK) { throw new RemoteException(); } int result = reply.readInt(); return result; } catch (RemoteException e) { throw new RemoteException(); } finally { data.reclaim(); //reclaim()清除再也不使用的MessageParcel對象。 reply.reclaim(); } } }
等待鏈接的遠端側的代理示例以下:
public class MyRemote extends RemoteObject implements IRemoteBroker{ private static final int ERR_OK = 0; private static final int ERROR = -1; private static final int COMMAND_PLUS = IRemoteObject.MIN_TRANSACTION_ID; public MyRemote() { super("MyService_Remote"); } @Override public IRemoteObject asObject() { return this; } @Override public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) { if (code != COMMAND_PLUS) { reply.writeInt(ERROR); return false; } int value1 = data.readInt(); int value2 = data.readInt(); int sum = value1 + value2; reply.writeInt(ERR_OK); reply.writeInt(sum); return true; } }
等待鏈接側還須要做以下修改:
// 綁定前面定義的代理,實例化出發起連接側須要的代理 private MyRemote remote = new MyRemote(); @Override public IRemoteObject onConnect(Intent intent) { //連接成功的時候,給發起連接側返回去 return remote.asObject(); }
完成上述步驟後,能夠經過點擊事件實現鏈接、利用鏈接關係控制PA以及斷開鏈接等行爲,代碼示例以下:
private MyRemoteProxy mProxy = null; // 建立鏈接回調實例 private IAbilityConnection conn = new IAbilityConnection() { // 鏈接到Service的回調 @Override public void onAbilityConnectDone(ElementName elementName, IRemoteObject iRemoteObject, int resultCode) { // 在這裏開發者能夠拿到服務端傳過來IRemoteObject對象,從中解析出服務端傳過來的信息 mProxy = new MyRemoteProxy(iRemoteObject); UIUtils.showTip(MainAbilitySlice.this,"拿到remoteObject:" + mProxy); } // 意外斷開鏈接纔會回調 @Override public void onAbilityDisconnectDone(ElementName elementName, int resultCode) { } }; // 鏈接遠程 case ResourceTable.Id_main_connect_remoteService_btn: //一、實現鏈接的本地側的代理 //二、實現等待鏈接的遠端側的代理 //三、修改等待鏈接側的Service //四、在本地(發起連接側)獲取遠端(被連接側)返回過來的連接代理,建立連接後的回調函數 //五、實現連接,經過代理對象使用Service的服務 if (deviceId != null) { Intent connectServiceIntent = new Intent(); connectServiceIntent.setElement(new ElementName( deviceId, "com.ybzy.demo", "com.ybzy.demo.RemoteServiceAbility" )); connectServiceIntent.setFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE); connectAbility(connectServiceIntent, iAbilityConnection); } break; // 連接後使用 case ResourceTable.Id_main_use_remoteService_btn: if (mProxy != null) { int ret = -1; try { ret = mProxy.plus(10, 20); } catch (RemoteException e) { e.printStackTrace(); } Common.myShowTip(MainAbilitySlice.this, "獲取的結果:" + ret); } break; // 用完斷開 case ResourceTable.Id_main_disconnect_remoteService_btn: disconnectAbility(iAbilityConnection); break;