【发布时间】:2014-10-28 10:10:07
【问题描述】:
是什么原因促使 android 开发团队弃用某些系统方法,我知道有些方法被替换为另一种更少数和有用的方法,但有些甚至没有被另一种方法替换,或者只是被一种通用方法替换big 方法在尝试更具体地处理某些元素时没有帮助,例如来自 Date 类的一些方法:
Date mDate = new Date();
mDate.getDate(); //Deprecated
mDate.getDay(); //Deprecated
mDate.getHours(); //Deprecated
mDate.getMinutes(); //Deprecated
mDate.getMonth(); //Deprecated
mDate.getSeconds(); //Deprecated
mDate.getYear(); //Deprecated
mDate.getTimezoneOffset(); //Deprecated
现在唯一存在的是:
mDate.getTime(); // gives the whole Time & Date as a `long` Value in milliseconds
现在,当我收到 Date Object 并尝试获得 年份 时,我必须:
- 声明一个新的
Calendar Object。 - 使其等于日历类的实例。
- 将此日历对象的时间设置为我得到的日期。
- 从此日历对象中提取所需的属性。
如果该数据对象仅包含时间为零的日期怎么办。现在我必须采取一种解决方法来在应用日期之前保存日历的时间,然后在应用日期后再次将保存的时间重置给它。
找到了另一个示例 here,它说明整个 PictureListener 接口已弃用,并且没有迹象表明应该用什么(如果有的话)来替换它。
第三个例子,当想要得到屏幕width & height,那是直接的:
int windowWidth, windowHeight;
windowWidth = getWindowManager().getDefaultDisplay().getWidth(); // Deprecated
windowHeight = getWindowManager().getDefaultDisplay().getHeight(); // Deprecated
但现在:
int windowWidth, windowHeight;
DisplayMetrics metrices = new DisplayMetrics();
mContext.getWindowManager().getDefaultDisplay().getMetrics(metrices);
windowWidth = metrices.widthPixels;
windowHeight = metrices.heightPixels;
【问题讨论】:
-
@Funkystein looool,确实有道理
-
我相信 this ,昨天我们曾经做过一些事情,今天我们知道可以通过一些不同且有效的方法来实现相同的效果,因此我更喜欢替换今天呢
-
觉得这很有趣,但完全同意@RobMeeuwisse 也指出的快 的事情.. :)
-
@Funkystein +1,请指出...(0_o)
-
stackoverflow.com/questions/2901262/… 更多关于日期弃用。而且由于每个 API 设计人员都会犯错误/改变需求,因此您会一遍又一遍地发生这种情况。
标签: android deprecated android-date