如何以编程方式获取自激活移动数据连接以来经过的时间?
我试图做这样的事情:
phone = PhoneFactory.getDefaultPhone();
List<DataConnection> dcs = phone.getCurrentDataConnectionList();
for (DataConnection dc : dcs) {
if (dc.isActive()) {
timeElapsed = (System.currentTimeMillis() - dc.getConnectionTime())/1000;
}
}
但是这些方法在 SDK 中是不可见的,所以我尝试了反射:
try {
Class mPhoneFactory = Class.forName("com.android.internal.telephony.PhoneFactory");
Method mMakeDefaultPhone = mPhoneFactory.getMethod("makeDefaultPhone", new Class[] {Context.class});
mMakeDefaultPhone.invoke(null, this);
Method mGetDefaultPhone = mPhoneFactory.getMethod("getDefaultPhone", null);
Object mPhone = mGetDefaultPhone.invoke(null);
Method mGetCurrentDataConnectionList =
mPhone.getClass().getMethod("getCurrentDataConnectionList", null);
List<Object> dcs = (List)mGetCurrentDataConnectionList.invoke(mPhone, null);
Method mGetConnectionTime =
dcs.get(0).getClass().getMethod("getConnectionTime", null);
long getConnectionTime = (Long)mGetConnectionTime.invoke(dcs.get(0), null);
} catch(Exception e) {
e.printStackTrace();
}
列表“dcs”包含应该存在的 GSMDataConnection,但信息不正确,它总是说 GSM 连接处于非活动状态,这不是真的。
任何想法?谢谢!