RemoteViews是Android框架中的一個特殊View,它可以在任意進程中生成,並且作為一個可序列化的對象通過Binder傳遞到其他進程中,這也使得RemoteViews成為跨進程遠程視圖的載體。RemoteViews是運行在Context上下文環境之外的,它不能訪問其他布局資源,也不能訪問除了Intent之外的其他數據。下面將從安全、布局、性能等方面來介紹RemoteViews的設計原則和最佳實踐。
一、RemoteViews的安全性
當通過Intent傳遞RemoteViews時,建議不要在RemoteViews中使用動態綁定的服務或廣播。這是因為這些動態綁定可能會導致類加載器和進程之間的錯誤。而且,更重要的是,如果攻擊者獲取了您的應用程序的Intent,他們可能會通過初始化和運行有害的模塊來攻擊您的應用程序。
二、RemoteViews的布局選擇
RemoteViews必須在無法訪問布局的情況下,將其布局和資源打包傳遞。因此,在設計布局的時候,必須考慮以下因素:
1.盡量使用基本布局,例如LinearLayout和FrameLayout,因為其效率和兼容性都更高
2.盡量避免使用可調整大小或可旋轉的元素,因為Android系統必須對其進行翻譯並且傳達到客戶端的App,這通常會使傳輸的數據量增加。
3.不要使用自適應寬度或填滿父布局等屬性,因為這通常很難保證布局的正確性和效率
以下是一個使用RemoteViews實現一個簡單的布局的示例:
// RemoteViews定義 RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.layout_remote_view); remoteViews.setTextViewText(R.id.tv_title, "Hello World"); remoteViews.setImageViewBitmap(R.id.iv_logo, bitmap); // PendingIntent定義 PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); // Notification構造 Notification.Builder builder = new Notification.Builder(context) .setSmallIcon(R.mipmap.icon) .setContent(remoteViews) .setContentIntent(pendingIntent); Notification notification = builder.build(); // Notification顯示 NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(100, notification);
三、RemoteViews的性能考慮
由於RemoteViews需要在客戶端進行處理和渲染,因此使用RemoteViews可能會影響應用程序的性能。以下是一些優化建議:
1.嘗試將視圖的數量減少到最小值,因為視圖數量的增加會導致帶寬的佔用率和內存使用量的增加。
2.盡量使用內存友好的方法和數據類型,例如緩存圖像和使用基本數據類型而不是對象。
3.緩存RemoteViews,盡量減少生成RemoteViews的次數。例如,在每個帶v的setViewVisibility()函數調用中出現的新布局生成使得視圖綁定和文件系統I / O在此時都處於類鎖定和內存模型鎖定狀態。您可以使用InflateService服務(https://developer.android.com/reference/android/R.styleable.html#AppWidgetProviderInfo_remoteViewsService),在這種服務中,RemoteViews被創建,並生成視圖緩存,以便稍後的調用。
以下是一個使用RemoteViews實現一個簡單的布局的示例:
// RemoteViews定義 RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.layout_remote_view); remoteViews.setTextViewText(R.id.tv_title, "Hello World"); remoteViews.setImageViewBitmap(R.id.iv_logo, bitmap); // PendingIntent定義 PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); // Notification構造 Notification.Builder builder = new Notification.Builder(context) .setSmallIcon(R.mipmap.icon) .setContent(remoteViews) .setContentIntent(pendingIntent); Notification notification = builder.build(); // Notification顯示 NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(100, notification);
四、RemoteViews的樣式設計
RemoteViews通常用於遠程視圖,例如傳遞通知的遠程視圖到另一個進程。由於RemoteViews不能訪問與應用程序的資源和主題相關的數據,因此在設計RemoteViews時,必須使用簡單且可以匹配各種主題的樣式。
以下是一個使用RemoteViews實現一個簡單的樣式設計的示例:
int layoutId = R.layout.custom_notification; if (!TextUtils.isEmpty(theme)) { if (theme.equals("dark")) { layoutId = R.layout.custom_notification_dark; } else { layoutId = R.layout.custom_notification_light; } } RemoteViews remoteViews = new RemoteViews(context.getPackageName(), layoutId); remoteViews.setImageViewResource(R.id.notification_image_view, R.drawable.notification_logo); remoteViews.setTextViewText(R.id.notification_title, title); remoteViews.setTextViewText(R.id.notification_text, message);
五、RemoteViews的事件監聽
在RemoteViews創建過程中,可以為RemoteViews中的所需組件設置PendingIntent。PendingIntent不會直接調用組件,而是先將調用封裝在Intent對象中,然後等待推送操作。當用戶觸發操作時,Intent將傳遞到系統的發射器和ActiviyManagerService,以便獲取存儲在Intent對象中的組件詳細信息。
以下是一個使用RemoteViews實現一個簡單處理事件監聽的示例:
// RemoteViews定義 RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.layout_remote_view); remoteViews.setTextViewText(R.id.tv_title, "Hello World"); remoteViews.setImageViewBitmap(R.id.iv_logo, bitmap); // 設置打開Activity Pending Intent Intent openActivityIntent = new Intent(context, YourActivity.class); openActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); openActivityIntent.putExtra("extra_key", "extra_value"); PendingIntent openActivityPendingIntent = PendingIntent.getActivity(context, 0, openActivityIntent, PendingIntent.FLAG_ONE_SHOT); remoteViews.setOnClickPendingIntent(R.id.ll_container, openActivityPendingIntent); // Notification構造 Notification.Builder builder = new Notification.Builder(context) .setSmallIcon(R.mipmap.icon) .setContent(remoteViews) .setContentIntent(pendingIntent); Notification notification = builder.build(); // Notification顯示 NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(100, notification);
上面的代碼中,為RemoteViews中的LinearLayout添加了一個onClick事件,當用戶點擊RemoteViews中的布局時,它將打開一個Activity,並將Extra數據傳遞給Activity。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/192012.html