【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android輔助權(quán)限的示例分析_第1頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android輔助權(quán)限的示例分析_第2頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android輔助權(quán)限的示例分析_第3頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android輔助權(quán)限的示例分析_第4頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android輔助權(quán)限的示例分析_第5頁(yè)
免費(fèi)預(yù)覽已結(jié)束,剩余1頁(yè)可下載查看

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android輔助權(quán)限的示例分析

一、介紹二、配置/**

*

核心服務(wù):執(zhí)行自動(dòng)化任務(wù)

*

Created

by

czc

on

2017/6/13.

*/

public

class

TaskService_

extends

AccessibilityService{

@Override

public

void

onAccessibilityEvent(AccessibilityEvent

event)

{

//注意這個(gè)方法回調(diào),是在主線(xiàn)程,不要在這里執(zhí)行耗時(shí)操作

}

@Override

public

void

onInterrupt()

{

}

}<service

android:name=".service.TaskService"

android:enabled="true"

android:exported="true"

android:label="@string/app_name_setting"

android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">

<intent-filter>

<action

android:name="android.accessibilityservice.AccessibilityService"/>

</intent-filter>

<meta-data

android:name="android.accessibilityservice"

android:resource="@xml/accessibility"/>

</service><?xml

version="1.0"

encoding="utf-8"?>

<accessibility-service

xmlns:android="/apk/res/android"

<!--監(jiān)視的動(dòng)作-->

android:accessibilityEventTypes="typeAllMask"

<!--提供反饋類(lèi)型,語(yǔ)音震動(dòng)等等。-->

android:accessibilityFeedbackType="feedbackGeneric"

<!--監(jiān)視的view的狀態(tài),注意這里設(shè)置flagDefault會(huì)到時(shí)候部分界面狀態(tài)改變,不觸發(fā)onAccessibilityEvent(AccessibilityEvent

event)的回調(diào)-->

android:accessibilityFlags="flagDefault|flagRetrieveInteractiveWindows|flagIncludeNotImportantViews|flagReportViewIds|flagRequestTouchExplorationMode"

<!--是否要能夠檢索活動(dòng)窗口的內(nèi)容,此設(shè)置不能在運(yùn)行時(shí)改變-->

android:canRetrieveWindowContent="true"

<!--功能描述-->

android:description="@string/description"

<!--同一事件間隔時(shí)間名-->

android:notificationTimeout="100"

<!--監(jiān)控的軟件包名-->

android:packageNames="com.tencent.mm,com.eg.android.AlipayGphone"

/>三、核心方法accessibilityNodeInfo.findAccessibilityNodeInfosByText(text)accessibilityNodeInfo.findAccessibilityNodeInfosByViewId(id)/upload/information/20200623/125/120142.jpg四、輔助權(quán)限判斷是否開(kāi)啟public

static

boolean

hasServicePermission(Context

ct,

Class

serviceClass)

{

int

ok

=

0;

try

{

ok

=

Settings.Secure.getInt(ct.getContentResolver(),

Settings.Secure.ACCESSIBILITY_ENABLED);

}

catch

(Settings.SettingNotFoundException

e)

{

}

TextUtils.SimpleStringSplitter

ms

=

new

TextUtils.SimpleStringSplitter(':');

if

(ok

==

1)

{

String

settingValue

=

Settings.Secure.getString(ct.getContentResolver(),

Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);

if

(settingValue

!=

null)

{

ms.setString(settingValue);

while

(ms.hasNext())

{

String

accessibilityService

=

ms.next();

if

(accessibilityService.contains(serviceClass.getSimpleName()))

{

return

true;

}

}

}

}

return

false;

}五、輔助的開(kāi)啟方法public

static

void

openServicePermissonRoot(Context

ct,

Class

service)

{

String

cmd1

=

"settings

put

secure

enabled_accessibility_services

"

+

ct.getPackageName()

+

"/"

+

service.getName();

String

cmd2

=

"settings

put

secure

accessibility_enabled

1";

String[]

cmds

=

new

String[]{cmd1,

cmd2};

ShellUtils.execCmd(cmds,

true);

}public

static

void

openServicePermission(Context

ct,

Class

serviceClass)

{

Set<ComponentName>

enabledServices

=

getEnabledServicesFromSettings(ct,

serviceClass);

if

(null

==

enabledServices)

{

return;

}

ComponentName

toggledService

=

ComponentName.unflattenFromString(ct.getPackageName()

+

"/"

+

serviceClass.getName());

final

boolean

accessibilityEnabled

=

true;

enabledServices.add(toggledService);

//

Update

the

enabled

services

setting.

StringBuilder

enabledServicesBuilder

=

new

StringBuilder();

for

(ComponentName

enabledService

:

enabledServices)

{

enabledServicesBuilder.append(enabledService.flattenToString());

enabledServicesBuilder.append(":");

}

final

int

enabledServicesBuilderLength

=

enabledServicesBuilder.length();

if

(enabledServicesBuilderLength

>

0)

{

enabledServicesBuilder.deleteCharAt(enabledServicesBuilderLength

-

1);

}

Settings.Secure.putString(ct.getContentResolver(),

Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,

enabledServicesBuilder.toString());

//

Update

accessibility

enabled.

Settings.Secure.putInt(ct.getContentResolver(),

Settings.Secure.ACCESSIBILITY_ENABLED,

accessibilityEnabled

?

1

:

0);

}

public

static

Set<ComponentName>

getEnabledServicesFromSettings(Context

context,

Class

serviceClass)

{

String

enabledServicesSetting

=

Settings.Secure.getString(context.getContentResolver(),

Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);

if

(enabledServicesSetting

==

null)

{

enabledServicesSetting

=

"";

}

Set<ComponentName>

enabledServices

=

new

HashSet<ComponentName>();

TextUtils.SimpleStringSplitter

colonSplitter

=

new

TextUtils.SimpleStringSplitter(':');

colonSplitter.setString(enabledServicesSetting);

while

(colonSplitter.hasNext())

{

String

componentNameString

=

colonSplitter.next();

ComponentName

enabledService

=

ComponentName.unflattenFromString(componentNameString);

if

(enabledService

!=

null)

{

if

(enabledService.flattenToString().contains(serviceClass.getSimpleName()))

{

return

null;

}

enabledServices.add(enabledService);

}

}

return

enabledServices;

}public

static

void

jumpSystemSetting(Context

ct)

{

//

jump

to

setting

permission

Intent

intent

=

new

Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

ct.startActivity(intent);

}public

static

void

openServicePermissonCompat(final

Context

ct,

final

Class

service)

{

//輔助權(quán)限:如果root,先申請(qǐng)root權(quán)限

if

(isAppRoot())

{

if

(!hasServicePermission(ct,

service))

{

new

Thread(new

Runnable()

{

@Override

public

void

ru

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論