一嵌灰、簡介
由無障礙(一)功能實(shí)現(xiàn)我們知道募逞,無障礙服務(wù)可以獲取界面上的控件和控件的文案信息背率。那么就可以通過文案做一些自動(dòng)化操作细层。
比如:自動(dòng)刷新儒鹿,找到列表中符合條件的票務(wù)信息弧岳、訂單信息凳忙,找到搶票按鈕业踏,實(shí)現(xiàn)自動(dòng)搶單操作。
二涧卵、無障礙抓取屏幕數(shù)據(jù)
以一個(gè)簡單的listview為例勤家,寫一個(gè)簡單的布局,嘗試使用無障礙抓取頁面數(shù)據(jù)柳恐。
item布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp">
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginLeft="58dp"
android:gravity="center_vertical"
android:text="我是一個(gè)title" />
<TextView
android:id="@+id/click_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="50dp"
android:background="#ff0000"
android:padding="10dp"
android:text="點(diǎn)我"
android:textColor="@color/white"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
顯示效果:
抓取結(jié)果:
下方是抓到的數(shù)據(jù)伐脖,可以看到,每個(gè)條目的的title都被抓取到了乐设。
setttleNodeInfo getClassName:android.widget.TextView, getText:Demo
setttleNodeInfo getClassName:android.widget.ListView, getText:null
setttleNodeInfo getClassName:android.widget.RelativeLayout, getText:null
setttleNodeInfo getClassName:android.widget.TextView, getText:我是title:0
setttleNodeInfo getClassName:android.widget.TextView, getText:點(diǎn)我
setttleNodeInfo getClassName:android.widget.RelativeLayout, getText:null
setttleNodeInfo getClassName:android.widget.TextView, getText:我是title:1
setttleNodeInfo getClassName:android.widget.TextView, getText:點(diǎn)我
setttleNodeInfo getClassName:android.widget.RelativeLayout, getText:null
setttleNodeInfo getClassName:android.widget.TextView, getText:我是title:2
setttleNodeInfo getClassName:android.widget.TextView, getText:點(diǎn)我
setttleNodeInfo getClassName:android.widget.RelativeLayout, getText:null
setttleNodeInfo getClassName:android.widget.TextView, getText:我是title:3
setttleNodeInfo getClassName:android.widget.TextView, getText:點(diǎn)我
setttleNodeInfo getClassName:android.widget.RelativeLayout, getText:null
setttleNodeInfo getClassName:android.widget.TextView, getText:我是title:4
setttleNodeInfo getClassName:android.widget.TextView, getText:點(diǎn)我
setttleNodeInfo getClassName:android.widget.RelativeLayout, getText:null
setttleNodeInfo getClassName:android.widget.TextView, getText:我是title:5
setttleNodeInfo getClassName:android.widget.TextView, getText:點(diǎn)我
setttleNodeInfo getClassName:android.widget.RelativeLayout, getText:null
setttleNodeInfo getClassName:android.widget.TextView, getText:我是title:6
setttleNodeInfo getClassName:android.widget.TextView, getText:點(diǎn)我
三讼庇、無障礙檢測與防范
上述問題對于一些需要搶單的軟件來說,可能會(huì)有第三方插件開發(fā)近尚,從而導(dǎo)致不公平的搶單操作蠕啄。
為了避免以上問題,可以有以下的方法:
1戈锻、對無障礙功能檢測歼跟,如果開啟無障礙功能,提醒用戶運(yùn)行環(huán)境問題格遭。最好不要禁止用戶使用app哈街,因?yàn)橛行o障礙功能并不是真的用來搶單的。
2拒迅、針對無障礙app的應(yīng)用名稱骚秦、包名進(jìn)行封禁。
3坪它、對于敏感信息骤竹,轉(zhuǎn)換成圖片形式進(jìn)行展示,防止信息被輕易抓取往毡。
1蒙揣、無障礙功能開啟檢測
// 判斷無障礙功能是否開啟
private boolean isAccessibilityOpen() {
AccessibilityManager accessibilityManager = (AccessibilityManager) getSystemService(Context.ACCESSIBILITY_SERVICE);
return accessibilityManager.isEnabled();
}
// 直接查詢已經(jīng)啟用的service
public boolean isAccessibilityOpen2() {
AccessibilityManager accessibilityManager = (AccessibilityManager) getSystemService(Context.ACCESSIBILITY_SERVICE);
List<AccessibilityServiceInfo> accessibilityservices = accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_GENERIC);
return accessibilityservices != null && !accessibilityservices.isEmpty();
}
2、listView的item中包含title的數(shù)據(jù)轉(zhuǎn)換成圖片展示
item布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp">
<ImageView
android:id="@+id/img_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/click_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="50dp"
android:background="#ff0000"
android:padding="10dp"
android:text="點(diǎn)我"
android:textColor="@color/white"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
Adapter代碼:
public class MyImgAdapter extends BaseAdapter {
private final Context mContext;
private RelativeLayout mRealLayout;
public MyImgAdapter(Context context) {
this.mContext = context;
}
@Override
public int getCount() {
return 10;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_test_img, null);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.mImgLayout.setImageBitmap(genRealLayout(position));
viewHolder.mClickMe.setOnClickListener(v -> Toast.makeText(mContext, "點(diǎn)了我了:" + position, Toast.LENGTH_SHORT).show());
return convertView;
}
// 通過數(shù)據(jù)獲取bitmap
private synchronized Bitmap genRealLayout(int position) {
if (mRealLayout == null) {
mRealLayout = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.item_real_layout, null);
}
((TextView) mRealLayout.findViewById(R.id.tv_title)).setText("我是Img的title:" + position);
// 主動(dòng)測量一次
mRealLayout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int w = mRealLayout.getMeasuredWidth();
int h = mRealLayout.getMeasuredHeight();
mRealLayout.layout(0, 0, w, h); // 擺放布局
Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bmp);
mRealLayout.draw(c);
return bmp;
}
static class ViewHolder {
private TextView mClickMe;
private ImageView mImgLayout;
public ViewHolder(View view) {
mClickMe = view.findViewById(R.id.click_me);
mImgLayout = view.findViewById(R.id.img_layout);
}
}
}
顯示效果:
抓取結(jié)果:
setttleNodeInfo getClassName:android.widget.TextView, getText:Demo
setttleNodeInfo getClassName:android.widget.ListView, getText:null
setttleNodeInfo getClassName:android.widget.RelativeLayout, getText:null
setttleNodeInfo getClassName:android.widget.TextView, getText:點(diǎn)我
setttleNodeInfo getClassName:android.widget.RelativeLayout, getText:null
setttleNodeInfo getClassName:android.widget.TextView, getText:點(diǎn)我
setttleNodeInfo getClassName:android.widget.RelativeLayout, getText:null
setttleNodeInfo getClassName:android.widget.TextView, getText:點(diǎn)我
setttleNodeInfo getClassName:android.widget.RelativeLayout, getText:null
setttleNodeInfo getClassName:android.widget.TextView, getText:點(diǎn)我
setttleNodeInfo getClassName:android.widget.RelativeLayout, getText:null
setttleNodeInfo getClassName:android.widget.TextView, getText:點(diǎn)我
setttleNodeInfo getClassName:android.widget.RelativeLayout, getText:null
setttleNodeInfo getClassName:android.widget.TextView, getText:點(diǎn)我
setttleNodeInfo getClassName:android.widget.RelativeLayout, getText:null
setttleNodeInfo getClassName:android.widget.TextView, getText:點(diǎn)我
上述方法會(huì)產(chǎn)生很多bitmap對象开瞭,從而增加內(nèi)存開銷懒震,可以考慮list中的前幾個(gè)item使用圖片形式展示,其他的item繼續(xù)使用控件方式嗤详,從而減少內(nèi)存開銷个扰,并可以影響抓取結(jié)果。