Here we go again,
Hint: this post will be just a wrap up to what you can find here.
Unfortunately, the data-connection enable/disable APIs are not exposed to the user, So we'll access them using JAVA reflection.
Starting from API level 8 (platform 2.2) we can use the following snippet:
final ConnectivityManager conman =
(ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(conman);
final Class iConnectivityManagerClass =
Class.forName(iConnectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod =
iConnectivityManagerClass
.getDeclaredMethod("setMobileDataEnabled",Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
// (true) to enable 3G; (false) to disable it.
setMobileDataEnabledMethod.invoke(iConnectivityManager, true);
We need to add the following permission to the manifest file
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
Hint: the previous piece of code was tested on SAMSUNG GALAXY SII API level 9(platform 2.3).
Ok, now I'll post another snippet that I got it working on the emulator for APIs levels prior to (8):
Method dataConnSwitchmethod;
Class telephonyManagerClass;
Object ITelephonyStub;
Class ITelephonyClass;
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if(telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED){
isEnabled = true;
}else{
isEnabled = false;
}
telephonyManagerClass = Class.forName(telephonyManager.getClass().getName());
Method getITelephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony");
getITelephonyMethod.setAccessible(true);
ITelephonyStub = getITelephonyMethod.invoke(telephonyManager);
ITelephonyClass = Class.forName(ITelephonyStub.getClass().getName());
if (isEnabled) {
// if you want to disable it.
dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("disableDataConnectivity");
} else {
// if you want to enable it.
dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("enableDataConnectivity");
}
dataConnSwitchmethod.setAccessible(true);
dataConnSwitchmethod.invoke(ITelephonyStub);
That's it, don't hesitate to comment, to share your knowledge and to correct me.
This comment has been removed by the author.
ReplyDeleteSorry. It does not run on Android 2.3/3.0
ReplyDeletehow to do this in a simple way?
ReplyDeleteWow nice informative.Thanks for your post.
ReplyDeletetelephony leased line
http://www.videocontelecom.com
ReplyDeleteGreenline+Simcard Initiative Framework Treasure From Videocon Telecom
SIM CARD PREPAID SERVICES
SIM CARD POSTPAID SERVICES
SIM CARD ROAMING SERVICES
Diligent Initiative Gern Inherit Telecom : Videocon GSM Network
Hi, can You write how i can use this on mobile phone with Dual Sim?
ReplyDeletehow can deactivate SIM card in android programming
ReplyDeletebitlis
ReplyDeletesakarya
van
tunceli
ankara
RJX
B19A9
ReplyDeletePoloniex Güvenilir mi
Amasya Evden Eve Nakliyat
Batman Parça Eşya Taşıma
Hakkari Şehirler Arası Nakliyat
Ankara Parke Ustası
Kocaeli Evden Eve Nakliyat
Çerkezköy Evden Eve Nakliyat
Bayburt Şehir İçi Nakliyat
Nevşehir Parça Eşya Taşıma
57BE7
ReplyDeleteAmasya Şehirler Arası Nakliyat
Kastamonu Parça Eşya Taşıma
Tekirdağ Boya Ustası
Keçiören Fayans Ustası
Tekirdağ Parça Eşya Taşıma
Kütahya Lojistik
Ordu Lojistik
Amasya Parça Eşya Taşıma
Denizli Evden Eve Nakliyat
6A576
ReplyDeleteOrt Coin Hangi Borsada
Satoshi Coin Hangi Borsada
Binance Referans Kodu
Tumblr Takipçi Hilesi
Bitcoin Kazanma
Telegram Görüntüleme Hilesi
Shinja Coin Hangi Borsada
Threads İzlenme Satın Al
Dxgm Coin Hangi Borsada
6E13A
ReplyDeleteFacebook Grup Üyesi Hilesi
Sohbet
Twitter Trend Topic Hilesi
Mexc Borsası Kimin
Bitcoin Nasıl Alınır
Binance Borsası Güvenilir mi
Bitcoin Yatırımı Nasıl Yapılır
Btcst Coin Hangi Borsada
Madencilik Nedir
yuyiyikuoo
ReplyDeleteشركة المثالية للتنظيف
مكافحة الحشرات بالاحساء GvvsPvnR4D
ReplyDelete<a href="https://alryan-services.com.sa/%d8%b4%d8%b1%d9%83%d8%a9-%d8%b9%d8%b2%d9%84-%d9%85%d9%88%d8%a7%d8%b3%d9%8a%d8%b1-%d8%a7%d9%8
ReplyDeleteشركة عزل اسطح براس تنورة PVBAopFDWM
ReplyDelete6DA12ACCC5
ReplyDeletetakipçi satın al gerçek
146F73C2BA
ReplyDeletetiktok ucuz takipçi
B635780A06
ReplyDeletebot takipçi instagram
84EB5CF240
ReplyDeletetwitter takipci satin alma
548D45C410
ReplyDeletekadın takipçi
87201DAF8D
ReplyDeletetakipçi kasma
A72D20E6C4
ReplyDeletegüvenilir takipçi satın al
Para Kazandıran Oyunlar
101 Okey Yalla Hediye Kodu
Google Yorum Satın Al
Raid Promosyon Kodu
Hay Day Elmas Kodu
Lords Mobile Promosyon Kodu
İdle Office Tycoon Hediye Kodu
Brawl Stars Elmas Kodu
شركة تركيب طارد الحمام بالنعيرية Pja06yzEsP
ReplyDeleteشركة تركيب طارد الحمام بالنعيرية K5FibFT0iM
ReplyDeleteشركة تنظيف بالقطيف nbqLZWeLkT
ReplyDelete