我的應用有時需要通過移動數據連接才能工作。我知道有些應用程序可以讓某個應用程序只使用移動數據,我知道如何使用我的應用程序關閉無線網絡。但是,有沒有辦法告訴我的應用程序從現在開始使用移動連接,並在另一個點解除這個限制?強制安卓應用使用移動數據而無需關閉WiFi
0
A
回答
0
我使用了一種只適用於固定電話的解決方法。
已將setMobileDataEnabled方法從ConnectivityManager中刪除,但兩種方法getDataEnabled和setDataEnabled已添加到TelephonyManager中以實現此功能。
public void setMobileDataState(boolean mobileDataEnabled)
{
try
{
TelephonyManager telephonyService = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Method setMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("setDataEnabled", boolean.class);
if (null != setMobileDataEnabledMethod)
{
setMobileDataEnabledMethod.invoke(telephonyService, mobileDataEnabled);
}
}
catch (Exception ex)
{
Log.e(TAG, "Error setting mobile data state", ex);
}
}
public boolean getMobileDataState()
{
try
{
TelephonyManager telephonyService = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Method getMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("getDataEnabled");
if (null != getMobileDataEnabledMethod)
{
boolean mobileDataEnabled = (Boolean) getMobileDataEnabledMethod.invoke(telephonyService);
return mobileDataEnabled;
}
}
catch (Exception ex)
{
Log.e(TAG, "Error getting mobile data state", ex);
}
return false;
}
但你需要這個權限(MODIFY_PHONE_STATE)添加到清單文件,否則你會得到一個SecurityException。
0
您不能在每個應用程序的基礎上明確強制通信通道(您可以通過ConnectivityManager.setNetworkPreference(...)
請求使用首選模式,但這不是「強制」)。
// and to be sure:
ConnectivityManager.setNetworkPreference(ConnectivityManager.TYPE_MOBILE);
相關問題
- 1. 安卓應用程序強制關閉後啓動畫面
- 2. 強制關閉安卓設備中的android應用程序4
- 3. 安卓:總是強制關閉
- 4. 如何使用javascript強制關閉安卓鍵盤
- 5. 清除應用程序的緩存而無需強制關閉應用程序
- 6. 安卓:無法移動應用
- 7. 關閉應用中的移動數據
- 8. 安卓:強制關閉時試圖調用Preferences.java
- 9. 安卓:startChildActivity(tabgroupactivity)關閉應用程序
- 10. 安卓應用程序部隊關閉
- 11. 安卓應用做力關閉
- 12. SharedPreferences使應用程序強制關閉
- 13. 強制關閉使用getContentResolver()
- 14. 安卓「關閉」活動
- 15. 如何強制關閉wifi和數據漫遊
- 16. 按鈕無法使用。保持強制關閉應用程序
- 17. 流動音樂在Android使用移動數據,而不是WiFi
- 18. 如何強制iOS應用程序使用3G而不是WiFi?
- 19. 強制打開並強制關閉WiFi連接的Android代碼
- 20. 安卓:我們需要關閉遊標
- 21. 安卓:強制關閉的onclick按鈕列表視圖
- 22. 強制寫入SD卡時關閉(安卓)
- 23. 強制關閉過程中的ArrayList實現 - 安卓
- 24. 嘗試關閉SQLite數據庫時應用程序強制關閉
- 25. 安卓完成當前活動導致應用關閉
- 26. 安卓:關閉並重新啓動應用程序
- 27. 將數據提交到mysql後應用程序強制關閉
- 28. Android應用程序強制關閉數據庫連接嘗試
- 29. 將值插入數據庫(應用程序被強制關閉)
- 30. 應用程序強制關閉
你需要經理做什麼?你的第一行什麼都沒做,是嗎? – Ginso
是的,我編輯的代碼,但在那個時候你必須關閉WiFi – Pitty