日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

android l是哪个版本,从Android L及更高版本开始,setMobileDataEnabled方法不再可调用...

發布時間:2023/11/27 生活经验 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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方法不再可调用...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。