![](/img/trans.png)
[英]Unable to get phoneNumber from BroadcastReceiver even if BroadcastReceiver receiving all incomingnumber
[英]Unable to get theme attribute from BroadcastReceiver's context
我試圖在我的BroadcastReceiver
獲取?colorSecondary
,以便我可以將顏色設置為通知圖標和操作文本。 我正在使用以下方法從當前主題中獲取R.attr.colorSecondary
值。
@ColorRes
public static int getAttrColorResId(Context context, @AttrRes int resId) {
TypedValue outValue = new TypedValue();
Resources.Theme theme = context.getTheme();
boolean success = theme.resolveAttribute(resId, outValue, true);
return outValue.resourceId;
}
// usage
int colorRes = getAttrColorResId(context, R.attr.colorSecondary);
現在的問題是,我從resolveAttribute()
調用中得到false
結果。 看起來BroadcastReceiver
提供的上下文無法找到colorSecondary
。 如何從BroadcastReceiver
的上下文中獲取所需的屬性?
與 Activity 不同, BroadcastReceiver
不提供主題上下文,因為它是非 UI 上下文。 因此,此處不提供活動主題的屬性。 您可以嘗試將主題手動設置為此上下文,如下所示,以獲取colorSecondary
:
@ColorRes
public static int getAttrColorResId(Context context, @AttrRes int resId) {
TypedValue outValue = new TypedValue();
Resources.Theme theme = context.getTheme();
boolean success = theme.resolveAttribute(resId, outValue, true);
// if not success, that means current call is from some non-UI context
// so set a theme and call recursively
if (!success) {
context.getTheme().applyStyle(R.style.YourTheme, false);
return getAttrColorResId(context, resId);
}
return outValue.resourceId;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.