android l是哪个版本,从Android L及更高版本开始,setMobileDataEnabled方法不再可调用...
許多Android開發人員通過電子郵件向我發送了關于為Android 5+開啟/關閉移動網絡的問題,但我不會回答個人電子郵件,而是在這里發布我的答案,以便每個人都可以使用它并使其適應他們的Android應用程序。
首先,讓我們澄清一些誤解和誤解:svc?data?enable
svc?data?disable
上述方法只能打開/關閉后臺數據,而不是訂閱服務,因此電池將耗盡一點點,因為訂閱服務 - 一個Android系統服務 - 仍將在后臺運行。對于支持多個SIM卡的Android設備,這種情況更糟,因為訂閱服務會不斷掃描可用的移動網絡,以便與Android設備中提供的有效SIM卡一起使用。使用此方法需要您自擔風險。
現在,通過SubscriptionManagerAPI 22中引入的類關閉移動網絡的正確方法,包括其相應的訂閱服務,是:public?static?void?setMobileNetworkfromLollipop(Context?context)?throws?Exception?{
String?command?=?null;
int?state?=?0;
try?{
//?Get?the?current?state?of?the?mobile?network.
state?=?isMobileDataEnabledFromLollipop(context)???0?:?1;
//?Get?the?value?of?the?"TRANSACTION_setDataEnabled"?field.
String?transactionCode?=?getTransactionCode(context);
//?Android?5.1+?(API?22)?and?later.
if?(Build.VERSION.SDK_INT?>?Build.VERSION_CODES.LOLLIPOP)?{
SubscriptionManager?mSubscriptionManager?=?(SubscriptionManager)?context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
//?Loop?through?the?subscription?list?i.e.?SIM?list.
for?(int?i?=?0;?i?
if?(transactionCode?!=?null?&&?transactionCode.length()?>?0)?{
//?Get?the?active?subscription?ID?for?a?given?SIM?card.
int?subscriptionId?=?mSubscriptionManager.getActiveSubscriptionInfoList().get(i).getSubscriptionId();
//?Execute?the?command?via?`su`?to?turn?off
//?mobile?network?for?a?subscription?service.
command?=?"service?call?phone?"?+?transactionCode?+?"?i32?"?+?subscriptionId?+?"?i32?"?+?state;
executeCommandViaSu(context,?"-c",?command);
}
}
}?else?if?(Build.VERSION.SDK_INT?==?Build.VERSION_CODES.LOLLIPOP)?{
//?Android?5.0?(API?21)?only.
if?(transactionCode?!=?null?&&?transactionCode.length()?>?0)?{
//?Execute?the?command?via?`su`?to?turn?off?mobile?network.
command?=?"service?call?phone?"?+?transactionCode?+?"?i32?"?+?state;
executeCommandViaSu(context,?"-c",?command);
}
}
}?catch(Exception?e)?{
//?Oops!?Something?went?wrong,?so?we?throw?the?exception?here.
throw?e;
}???????????}
要檢查移動網絡是否已啟用:private?static?boolean?isMobileDataEnabledFromLollipop(Context?context)?{
boolean?state?=?false;
if?(Build.VERSION.SDK_INT?>=?Build.VERSION_CODES.LOLLIPOP)?{
state?=?Settings.Global.getInt(context.getContentResolver(),?"mobile_data",?0)?==?1;
}
return?state;}
為了獲得該TRANSACTION_setDataEnabled領域的價值(借鑒PhongLe的解決方案):private?static?String?getTransactionCode(Context?context)?throws?Exception?{
try?{
final?TelephonyManager?mTelephonyManager?=?(TelephonyManager)?context.getSystemService(Context.TELEPHONY_SERVICE);
final?Class>?mTelephonyClass?=?Class.forName(mTelephonyManager.getClass().getName());
final?Method?mTelephonyMethod?=?mTelephonyClass.getDeclaredMethod("getITelephony");
mTelephonyMethod.setAccessible(true);
final?Object?mTelephonyStub?=?mTelephonyMethod.invoke(mTelephonyManager);
final?Class>?mTelephonyStubClass?=?Class.forName(mTelephonyStub.getClass().getName());
final?Class>?mClass?=?mTelephonyStubClass.getDeclaringClass();
final?Field?field?=?mClass.getDeclaredField("TRANSACTION_setDataEnabled");
field.setAccessible(true);
return?String.valueOf(field.getInt(null));
}?catch?(Exception?e)?{
//?The?"TRANSACTION_setDataEnabled"?field?is?not?available,
//?or?named?differently?in?the?current?API?level,?so?we?throw
//?an?exception?and?inform?users?that?the?method?is?not?available.
throw?e;
}}
要執行命令su:private?static?void?executeCommandViaSu(Context?context,?String?option,?String?command)?{
boolean?success?=?false;
String?su?=?"su";
for?(int?i=0;?i?
//?Default?"su"?command?executed?successfully,?then?quit.
if?(success)?{
break;
}
//?Else,?execute?other?"su"?commands.
if?(i?==?1)?{
su?=?"/system/xbin/su";
}?else?if?(i?==?2)?{
su?=?"/system/bin/su";
}
try?{
//?Execute?command?as?"su".
Runtime.getRuntime().exec(new?String[]{su,?option,?command});
}?catch?(IOException?e)?{
success?=?false;
//?Oops!?Cannot?execute?`su`?for?some?reason.
//?Log?error?here.
}?finally?{
success?=?true;
}
}}
希望此更新能夠解決您在有根據的Android 5+設備上開啟/關閉移動網絡時可能遇到的任何誤解,誤解或疑問。
總結
以上是生活随笔為你收集整理的android l是哪个版本,从Android L及更高版本开始,setMobileDataEnabled方法不再可调用...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《日长》第四句是什么
- 下一篇: 体检,去医院可以只检查尿酸和甘油三酯,这