Android 针对MIUI系统Dialog显示
2016-07-19 本文已影响409人
Android_lml
在MIUI系统,Dialog设置属性.getWindow().setType( WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
调Dialog.show();的时候并没有显示Dialog,或者报错:
Android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
应该是MIUI针对这个属性修改过,针对MIUI系统,可以
if (!MIUIUtils.isMIUI()) { Dialog.getWindow().setType( WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); } else { Dialog.getWindow().setType( WindowManager.LayoutParams.TYPE_TOAST); }
是否MIUI,可以根据这个获取
public class MIUIUtils { private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code"; private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name"; private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage"; public static boolean isMIUI() { try { final BuildProperties prop = BuildProperties.newInstance(); return prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null || prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null || prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null; } catch (final IOException e) { return false; } }}
public class BuildProperties { private final Properties properties; private BuildProperties() throws IOException { properties = new Properties(); properties.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop"))); } public boolean containsKey(final Object key) { return properties.containsKey(key); } public boolean containsValue(final Object value) { return properties.containsValue(value); } public Set<Map.Entry<Object, Object>> entrySet() { return properties.entrySet(); } public String getProperty(final String name) { return properties.getProperty(name); } public String getProperty(final String name, final String defaultValue) { return properties.getProperty(name, defaultValue); } public boolean isEmpty() { return properties.isEmpty(); } public Enumeration<Object> keys() { return properties.keys(); } public Set<Object> keySet() { return properties.keySet(); } public int size() { return properties.size(); } public Collection<Object> values() { return properties.values(); } public static BuildProperties newInstance() throws IOException { return new BuildProperties(); }}