記錄Unity中顯示android view的方法

地圖導航開啓後,將地圖app弄到後臺運行,會發送系統通知。
在Unity中去截獲這些通知內容,這些內容包括文字以及導航icon圖標,這些信息的獲取通過NotificationListenerService得到:
1,首先從通知中取得contentView或bigContentView,他們是RemoteViews類型
2,把contentView轉換爲View:
View contentView = notification.contentView.apply(this, null);
3, 得到的View是一個ViewGroup, 得到所有的子View並遍歷
ArrayList contentViewList = getAllChildren(contentView);
4, 如果子View是一個TextView,拿到上面的文本
String str = ((TextView) v).getText().toString();
5, 如果子View是一個ImageView,通過調試發現顯示的是icon數據,所以把這個ImageView轉換爲bitmap,再轉換爲byte數組

關鍵代碼(繼承通知接口的類中):
private byte[] mNaviIcon= new byte[0];
public byte[] getNaviIconByteArray() {
return mNaviIcon;
}
public void onNotificationPosted(StatusBarNotification sbn) {
Notification notification = sbn.getNotification();
if (notification.contentView != null) {
View contentView = notification.contentView.apply(this, null);
if (contentView != null && contentView instanceof ViewGroup) {
ArrayList contentViewList = getAllChildren(contentView);
for (int n = 0; n < contentViewList.size(); n++) {
View v = contentViewList.get(n);
if ((v.getClass().getName().equalsIgnoreCase(“android.support.v7.widget.AppCompatTextView”))
|| (v.getClass().getName().equalsIgnoreCase(“android.widget.TextView”))) {
String str = ((TextView) v).getText().toString();
}
if(v.getClass().getName().equalsIgnoreCase(“android.widget.ImageView”)){
Bitmap bit = createViewBitmap(v);
byte[] tmpNaviIcon = getNaviIcon(bit);
mNaviIcon = tmpNaviIcon;
}
}
private byte[] getNaviIcon(Bitmap bmp) {
if (bmp != null) {
ByteBuffer buffer = ByteBuffer.allocate(bmp.getByteCount());
bmp.copyPixelsToBuffer(buffer);
return buffer.array();
} else {
return new byte[0];
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章