国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

RemoteViews內部實現機制及應用

2019-11-06 10:05:17
字體:
來源:轉載
供稿:網友

RemoteViews內部實現機制及應用

一、介紹 RemoteViews是一種特殊的View視圖結構,作用是顯示并更新其他進程中的View界面;因其實現了Parcelable接口,所以可以實現跨進程通信;典型的應用場景是通知欄和桌面小部件開發(兩者均是運行在系統進程中);因其運行在遠程的進程中(此處可以理解為系統進程),所以沒有辦法通過findViewById()獲取子控件,但是內部提供了一系列set方法用來更新界面;作為一種特殊的視圖結構,RemoteViews支持的View類型有限,類型如下: 1. Layout FrameLayout、LinearLayout、RelativeLayout、GridLayout 2. View Button、ImageButton、ImageView、PRogressBar、TextView、ListView、GridView、StackView、AnalogClick、Chronometer、ViewFlipper、adapterViewFlipper、ViewStub。 二、內部機制 1、首先來看一下RemoteViews內部實現機制的原理圖(網絡圖片): RemoteViews內部機制原理圖 2、RemoteViews的工作流程: 上圖中的本地進程假設為我們所開發的程序,遠程進程假設成系統進程;本地進程設置好一系列View顯示更新的操作,然后轉換成一系列Action并將其打包,通過Binder將這些操作傳遞給系統進程;系統進程獲取到Binder后,實現解析包并執行這些Action對象中的操作,最后實現系統進程界面的顯示更新。 3、Action說明: Action是RemoteViews的內部靜態抽象類,官方的解釋如下: /** * Base class for all actions that can be performed on an * inflated view. * SUBCLASSES MUST BE IMMUTABLE SO CLONE WORKS!!!!! */ Action代表一個View操作,因其實現了Parcelable接口所以可以實現跨進程通信,RemoteViews中的一系列操作視圖的set方法內部都轉換成了Action對象(Action代表一個View操作),轉換過程應用反射機制實現,并且添加到一個Aciton列表中,等待發送到系統進程。 系統進程獲取到一系列Action對象之后,調用RemoteViews的apply()加載布局并更新界面,調用reApply()更新界面,繼而實現遠程進程的顯示更新。 三、簡單應用場景 1. RemoteViews應用于Notification核心代碼:

PendingIntent pendingIntent1 = PendingIntent.getActivity(this,1,new Intent(this,ThirdActivity.class),PendingIntent.FLAG_CANCEL_CURRENT); RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.remote); remoteViews.setImageViewResource(R.id.iv,R.mipmap.ic_launcher); remoteViews.setTextViewText(R.id.title,"已作為媒體設備連接"); remoteViews.setTextViewTextSize(R.id.title, TypedValue.COMPLEX_UNIT_SP,16); remoteViews.setTextViewText(R.id.content,"觸摸可顯示其他USB選項。"); remoteViews.setTextViewTextSize(R.id.content, TypedValue.COMPLEX_UNIT_SP,13); remoteViews.setOnClickPendingIntent(R.id.content,pendingIntent1); Notification.Builder builder = new Notification.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setWhen(System.currentTimeMillis()) .setAutoCancel(true) .setContent(remoteViews) .setContentIntent(PendingIntent.getActivity(this,0,new Intent(this,SecondActivity.class),PendingIntent.FLAG_UPDATE_CURRENT)); Notification notification = builder.build(); NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.notify(1,notification);

2、RemoteViews應用于桌面小部件的核心代碼:

RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget); remoteViews.setImageViewBitmap(R.id.image_view,rotateBitmap(context,srcBitmap,degree)); Intent intent1 = new Intent(); intent1.setAction(CLICK_ACTION); PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0,intent1,0); remoteViews.setOnClickPendingIntent(R.id.image_view,pendingIntent); appWidgetManager.updateAppWidget(new ComponentName(context,MyAppWidgetProvider.class),remoteViews); SystemClock.sleep(10);

四、文章說明 本篇文章是自己對RemoteViews的理解總結,也是為了記憶,如有不對之處,歡迎閱覽者提出,謝謝!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江门市| 东阳市| 江油市| 富顺县| 宜阳县| 聂荣县| 滦平县| 新平| 民和| 平利县| 荣昌县| 浏阳市| 新田县| 灵山县| 蕉岭县| 于都县| 河津市| 离岛区| 华容县| 罗源县| 合阳县| 永泰县| 南溪县| 静安区| 建水县| 和林格尔县| 容城县| 利津县| 田阳县| 锦屏县| 深水埗区| 林西县| 萝北县| 当阳市| 荥阳市| 团风县| 靖州| 巫山县| 天水市| 阳西县| 古浪县|