W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
IntentAgent封裝了一個(gè)指定行為的 Intent,可以通過(guò) triggerIntentAgent 接口主動(dòng)觸發(fā),也可以與通知綁定被動(dòng)觸發(fā)。具體的行為包括:?jiǎn)?dòng) Ability 和發(fā)送公共事件。例如:收到通知后,在點(diǎn)擊通知后跳轉(zhuǎn)到一個(gè)新的 Ability,不點(diǎn)擊則不會(huì)觸發(fā)。
IntentAgent 相關(guān)基礎(chǔ)類包括 IntentAgentHelper、 IntentAgentInfo、 IntentAgentConstant 和 TriggerInfo,基礎(chǔ)類之間的關(guān)系如下圖所示:
圖1 IntentAgent 基礎(chǔ)類關(guān)系圖
IntentAgentHelper封裝了獲取、激發(fā)、取消IntentAgent等靜態(tài)方法。
接口名 | 描述 |
---|---|
getIntentAgent(Context context, IntentAgentInfo paramsInfo) | 獲取一個(gè) IntentAgent 實(shí)例。 |
triggerIntentAgent(Context context, IntentAgent agent, IntentAgent.Oncompleted onCompleted, EventHandler handler, TriggerInfo paramsInfo) | 主動(dòng)激發(fā)一個(gè) IntentAgent 實(shí)例。 |
cancel(IntentAgent agent) | 取消一個(gè) IntentAgent 實(shí)例。 |
judgeEquality(IntentAgent agent, IntentAgent otherAgent) | 判斷兩個(gè) IntentAgent 實(shí)例是否相等。 |
getHashCode(IntentAgent agent) | 獲取一個(gè) IntentAgent 實(shí)例的哈希碼。 |
getBundleName(IntentAgent agent) | 獲取一個(gè) IntentAgent 實(shí)例的包名。 |
getUid(IntentAgent agent) | 獲取一個(gè) IntentAgent 實(shí)例的用戶ID。 |
IntentAgentInfo 類封裝了獲取一個(gè) IntentAgent 實(shí)例所需的數(shù)據(jù)。使用構(gòu)造函數(shù) IntentAgentInfo(int requestCode, OperationType operationType, List <Flags> flags, List <Intent> intents, IntentParams extraInfo) 獲取 IntentAgentInfo 對(duì)象。
IntentAgentConstant 類中包含 OperationType 和 Flags 兩個(gè)枚舉類:
類名 | 枚舉值 |
---|---|
IntentAgentConstant.OperationType | UNKNOWN_TYPE:不識(shí)別的類型。START_ABILITY:開啟一個(gè)有頁(yè)面的 Ability。START_ABILITIES:開啟多個(gè)有頁(yè)面的 Ability。START_SERVICE:開啟一個(gè)無(wú)頁(yè)面的 ability。SEND_COMMON_EVENT:發(fā)送一個(gè)公共事件。 |
IntentAgentConstant.Flags | ONE_TIME_FLAG:IntentAgent 僅能使用一次。只在 operationType 的值為 START_ABILITY,START_SERVICE 和 SEND_COMMON_EVENT 時(shí)有意義。NO_BUILD_FLAG:如果描述 IntentAgent 對(duì)象不存在,則不創(chuàng)建它,直接返回 null。只在 operationType 的值為 START_ABILITY, START_SERVICE 和 SEND_COMMON_EVENT 時(shí)有意義。CANCEL_PRESENT_FLAG:在生成一個(gè)新的IntentAgent 對(duì)象前取消已存在的一個(gè) IntentAgent 對(duì)象。只在 operationType 的值為 START_ABILITY,START_SERVICE 和 SEND_COMMON_EVENT 時(shí)有意義。 UPDATE_PRESENT_FLAG:使用新的 IntentAgent 的額外數(shù)據(jù)替換已存在的 IntentAgent 中的額外數(shù)據(jù)。只在 operationType 的值為 START_ABILITY,START_SERVICE 和 SEND_COMMON_EVENT 時(shí)有意義。CONSTANT_FLAG:IntentAgent 是不可變的。 REPLACE_ELEMENT:當(dāng)前I ntent 中的 element 屬性可被 IntentAgentHelper.triggerIntentAgent() 中 Intent 的 element 屬性取代。 REPLACE_ACTION: 當(dāng)前 Intent 中的 action 屬性可被 IntentAgentHelper.triggerIntentAgent()中 Intent 的 action 屬性取代。 REPLACE_URI:當(dāng)前 Intent 中的 uri 屬性可被 IntentAgentHelper.triggerIntentAgent() 中 Intent 的 uri 屬性取代。 REPLACE_ENTITIES:當(dāng)前 Intent 中的 entities 屬性可被 IntentAgentHelper.triggerIntentAgent() 中 Intent 的 entities 屬性取代。 REPLACE_BUNDLE:當(dāng)前 Intent 中的 bundleName 屬性可被 IntentAgentHelper.triggerIntentAgent() 中 Intent 的 bundleName 屬性取代。 |
TriggerInfo 類封裝了主動(dòng)激發(fā)一個(gè) IntentAgent 實(shí)例所需的數(shù)據(jù),使用構(gòu)造函數(shù)TriggerInfo( String permission, IntentParams extraInfo, Intent intent, int code) 獲取 TriggerInfo 對(duì)象。
獲取 IntentAgent 的代碼示例如下:
// 指定要啟動(dòng)的Ability的BundleName和AbilityName字段
// 將Operation對(duì)象設(shè)置到Intent中
Operation operation = new Intent.OperationBuilder()
.withDeviceId("")
.withBundleName("com.huawei.testintentagent")
.withAbilityName("com.huawei.testintentagent.entry.IntentAgentAbility")
.build();
intent.setOperation(operation);
List<Intent> intentList = new ArrayList<>();
intentList.add(intent);
// 定義請(qǐng)求碼
int requestCode = 200;
// 設(shè)置flags
List<Flags> flags = new ArrayList<>();
flags.add(Flags.UPDATE_PRESENT_FLAG);
// 指定啟動(dòng)一個(gè)有頁(yè)面的Ability
IntentAgentInfo paramsInfo = new IntentAgentInfo(requestCode, IntentAgentConstant.OperationType.START_ABILITY, flags, intentList, null);
// 獲取IntentAgent實(shí)例
IntentAgent agent = IntentAgentHelper.getIntentAgent(this, paramsInfo);
通知中添加 IntentAgent 的代碼示例如下:
int notificationId = 1;
NotificationRequest request = new NotificationRequest(notificationId);
String title = "title";
String text = "There is a normal notification content.";
NotificationNormalContent content = new NotificationNormalContent();
content.setTitle(title)
.setText(text);
NotificationContent notificationContent = new NotificationContent(content);
request.setContent(notificationContent); // 設(shè)置通知的內(nèi)容
request.setIntentAgent(agent); // 設(shè)置通知的 IntentAgent
主動(dòng)激發(fā) IntentAgent 的代碼示例如下:
int code = 100;
IntentAgentHelper.triggerIntentAgent(this, agent, null, null, new TriggerInfo(null, null, null, code ));
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: