记录一下项目中遇到andoid 6.0以上版本的适配问题,懒癌发作好多该总结的拖拖拉拉就忘了。
参考的博文:
http://www.tuicool.com/articles/zYniuyZ
http://blog.163.com/insightinto@126/blog/static/67383690201402455048495/
http://blog.csdn.net/lmj623565791/article/details/50709663
之前项目没有考虑到android 6.0 及以上版本的适配问题,后来再APP中加百度lbs地图时,遇到华为荣耀8定位不了的问题。
1 android 6.0以上的部分危险权限需要通过运行时动态申请。
2 android 6.0对应的api 23,将android编译版本换成23,才可以找到@TargetApi(value = Build.VERSION_CODES.M) Build.VERSION_CODES.M表示android 6.0
3 之前把最低版本设为api 16,发现代码中指定编译版本为android 6.0 也即Build.VERSION_CODES.M时找不到,将编译api改为23后,出现一些兼容错误。例如:
(1) android sdk 23,Google 移出了Apache HttpClient相关的类,推荐使用HttpUrlConnection,如果需要继续使用Apache HttpClient,需要在libs中添加org.apache.http.legacy.jar
和HttpClient一样,HttpResponse类也被移除了,解决方法如上所述,jar文件位置在sdk目录下\platforms\android-23\optional文件夹中
(2) android 6.0 The method sin(float) is undefined for the type FloatMath 报错
Android6.0使用 Math.floor 代替 FloatMath.floor 即可
(3) The method initializeScrollbars(TypedArray) is undefined for the type PLA_AbsListView
initializeScrollbars android 6.0中该方法不存在,使用反射机制找到被屏蔽
final TypedArray a = context.getTheme().obtainStyledAttributes(new int[0]);
try {
// initializeScrollbars(TypedArray)
Method initializeScrollbars = Android.view.View.class.getDeclaredMethod("initializeScrollbars", TypedArray.class);
initializeScrollbars.invoke(this, a);
} catch (Exception e) {
e.printStackTrace();
}
4 公司测试机比较少,也就后来才用高版本的手机测试,像华为荣耀8只开启app定位权限不够,还需要检测总开关定位服务有没有开启,如果没有开启总开关,检测定位权限都是无效的。所以首先检测总定位服务有没有开启。
/**
* 判断GPS是否开启,GPS或者AGPS开启一个就认为是开启的
* @param