在Android11以上版本,項目開啟了嚴格模式StrictMode下,經(jīng)常會看到以下報錯:
StrictMode policy violation:
android.os.strictmode.IncorrectContextUseViolation:
WindowManager should be accessed from Activity or other visual Context. Use an Activity or a Context created with Context#createWindowContext(int, Bundle),
which are adjusted to the configuration and visual bounds of an area on screen.
這個問題的原因是在StrictMode下,使用了非UI的Context來訪問UI相關的API,如ViewConfiguration。這樣會導致配置不正確,無法獲取到正確的顯示參數(shù)或窗口參數(shù)。
例如,如果您在Adapter中使用了inflater來加載布局,那么您應該使用Activity的Context而不是Application的Context來創(chuàng)建inflater。
public class ImageAdapter extends BaseAdapter {
private LayoutInflater inflater;
public ImageAdapter(Context context) {
inflater = LayoutInflater.from(context); //使用Activity的Context
}
public View getView (int pos, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate (R.layout.thumbitem, parent, false); //不會報錯
...
}
...
return convertView;
}
}
解決方案是使用UI的Context來訪問UI相關的API,有三種解決方案:
- Application的context替換使用Activity的context
- 通過Application.createWindowContext(Display, int, Bundle)獲取UI的context
- createConfigurationContext(Configuration)創(chuàng)建的Context
一、Application的context替換使用Activity的context
二、通過Application.createWindowContext(Display, int, Bundle)獲取UI的context
官網(wǎng)提供提供了示例解決方案,見地址:
[Context | Android Developers](https://developer.android.com/reference/android/content/Context#createWindowContext(int,)
...
//重點代碼,start
//anyContext為Application
final DisplayManager dm = anyContext.getSystemService(DisplayManager.class);
final Display primaryDisplay = dm.getDisplay(DEFAULT_DISPLAY);
final Context windowContext = anyContext.createDisplayContext(primaryDisplay)
.createWindowContext(TYPE_APPLICATION_OVERLAY, null);
//重點代碼,end
final View overlayView = Inflater.from(windowContext).inflate(someLayoutXml, null);
// WindowManager.LayoutParams initialization
...
// The types used in addView and createWindowContext must match.
mParams.type = TYPE_APPLICATION_OVERLAY;
...
windowContext.getSystemService(WindowManager.class).addView(overlayView, mParams);
基于方案進行封裝,可直接使用Java/Kotlin方法使用:
/**
* 兼容Android11以后。無UI Context(Application context)不能獲取UI(Activity context)相關資源問題
* @param context {@link android.app.Application}
* @return
*/
private static Context getWindowContext(Context context) {
if (context instanceof Activity){
return context;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
final DisplayManager dm = context.getSystemService(DisplayManager.class);
final Display primaryDisplay = dm.getDisplay(Display.DEFAULT_DISPLAY);
return context.createDisplayContext(primaryDisplay)
.createWindowContext(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, null);
}else {
return context;
}
}
//測試方法
private void Test(Context context){
Context windowContext = getWindowContext(context);
//需要使用Activity的Context
LayoutInflater inflater = LayoutInflater.from(windowContext);
//....以下其他邏輯
}
/**
* 兼容Android11以后。無UI Context(Application context)不能獲取UI(Activity context)相關資源問題
* @param context {@link android.app.Application}
* @return
*/
fun Context.getWindowContext(context: Context): Context? {
if (context is Activity) {
return context
}
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val dm = context.getSystemService(DisplayManager::class.java)
val primaryDisplay = dm.getDisplay(Display.DEFAULT_DISPLAY)
context.createDisplayContext(primaryDisplay)
.createWindowContext(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, null)
} else {
context
}
}
使用該方法后Android11的設備在嚴格模式下就不會報android.os.strictmode.IncorrectContextUseViolation: 錯誤了
三、createConfigurationContext(Configuration)創(chuàng)建的Context
目前該方法沒有找到官網(wǎng)實現(xiàn)鏈接,后續(xù)補充