简体   繁体   English

无法从 BroadcastReceiver 的上下文中获取主题属性

[英]Unable to get theme attribute from BroadcastReceiver's context

I am trying to get ?colorSecondary in my BroadcastReceiver , so that I can set colour to notification icon and action text.我试图在我的BroadcastReceiver获取?colorSecondary ,以便我可以将颜色设置为通知图标和操作文本。 I am using following method to get value of my R.attr.colorSecondary from my current theme.我正在使用以下方法从当前主题中获取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);

Now the problem is, I am getting false result from resolveAttribute() call.现在的问题是,我从resolveAttribute()调用中得到false结果。 Looks like the context provided by BroadcastReceiver is not able to find colorSecondary .看起来BroadcastReceiver提供的上下文无法找到colorSecondary How to get the desired attribute from BroadcastReceiver 's context?如何从BroadcastReceiver的上下文中获取所需的属性?

Unlike Activity, BroadcastReceiver does not provide context with a theme, as it is a non-UI context.与 Activity 不同, BroadcastReceiver不提供主题上下文,因为它是非 UI 上下文。 So, attributes of activity's theme are not available here.因此,此处不提供活动主题的属性。 You can try to set the theme manually to this context, as follows, to get colorSecondary :您可以尝试将主题手动设置为此上下文,如下所示,以获取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.

相关问题 即使 BroadcastReceiver 收到所有传入号码,也无法从 BroadcastReceiver 获取电话号码 - Unable to get phoneNumber from BroadcastReceiver even if BroadcastReceiver receiving all incomingnumber 无法在Android中使用BroadcastReceiver上下文初始化界面 - Unable to initialise interface using BroadcastReceiver context in Android 无法从片段获取上下文 - Unable to get context from Fragment 从应用程序类中获取主题属性 - Get Theme Attribute from Application Class 无法从 API ZE1E1D3D40573127E9EE0480CAFZ 中的 BroadcastReceiver Class 开始活动(上下文,意图)(android studio) - unable to startActivity(context, intent) from BroadcastReceiver Class in API R (android studio) BroadcastReceiver实现了LocationListener:如何在onLocationChanged中获取上下文 - BroadcastReceiver implements LocationListener: How to get context in onLocationChanged 获得一个BroadcastReceiver比它的活动更长久 - Get a BroadcastReceiver to outlive it's Activity 如何将上下文转换为从 Fragment 注册的 BroadcastReceiver 的 onReceive 中的侦听器? - How to cast context into listener in BroadcastReceiver's onReceive which is registering from Fragment? 从传递给BroadcastReceiver的上下文中访问“主要活动”? - Access the “main activity” from a context passed to a BroadcastReceiver? 从BroadcastReceiver上下文发送额外数据为空 - Sending Extra Data from BroadcastReceiver Context is Null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM