How can i turn on MobileNetwork? - android

I tried to turn on MobileNetwork by using code below,but it does not work and it throws **NoSuchMethodexception **. How can I turn on MOBILE NETWORK without NoSuchMethodException?
public void setMobileDataEnabled(boolean enabled,Context ctx) {
try{
final ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(cm.getClass().getName());
final Field connectivityManagerField = conmanClass.getDeclaredField("mService");
connectivityManagerField.setAccessible(true);
final Object connectivityManager = connectivityManagerField.get(cm);
final Class connectivityManagerClass = Class.forName(connectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled",Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(connectivityManager, enabled);
}
catch(Exception e){
e.printStackTrace();
}}
And this is permission I Added.
"android.permission.CHANGE_NETWORK_STATE"

For accessing the WIFI state :
WifiManager wifiManager ;
wifiManager = (WifiManager)this.getSystemService(this.WIFI_SERVICE);
wifiManager.setWifiEnabled(true); //True - to enable WIFI connectivity .
For accessing the DATA/3G state :
ConnectivityManager dataManager;
dataManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Method dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
dataMtd.setAccessible(true);
dataMtd.invoke(dataManager, true); //True - to enable data connectivity .
Now you need to add this permissions in Manifest file :
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>

Related

How can I get SSID of my device on Android?

I want to get SSID of my device and I am using below code:
private void getSSID() throws InvocationTargetException, IllegalAccessException {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
Method[] methods = wifiManager.getClass().getDeclaredMethods();
for (Method m: methods) {
if (m.getName().equals("getWifiApConfiguration")) {
WifiConfiguration config = (WifiConfiguration)m.invoke(wifiManager);
String ssid = config.SSID;
String bssid = config.BSSID;
Log.d(TAG, "getSSID: " + ssid);
Log.d(TAG, "getSSID: " + bssid);
}
}
}
But it is not working and giving null value.
You can get SSID as following
WifiManager wifiManager = (WifiManager) getSystemService (Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo ();
String ssid = info.getSSID();
From android 8.0 onwards we wont be getting SSID of the connected network unless GPS is turned on.
use this code
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo();
String ssid = info.getSSID();
String bssid = info.getBSSID();
add these permissions
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
since android 8 required coarse location permission
Try the below code and remember it if you are using android 7.0+ you have to turn on gps
private void getSSID(){
WifiManager wifiManager = (WifiManager) getSystemService
(Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo ();
String ssid = info.getSSID();
}
public static String getCurrentSSID(Context context) throws Exception {
try {
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
return wifiInfo.getSSID();
} catch (Exception e) {
throw new Exception("Unable to read SSID");
}
}
Just do not forget to add necessary permissions to the manifest
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Android L: Data turn off error

I've been using the following code on android 2.3 - 4.4.4 without any errors. But on running it on android L developer preview it gives me the following error
10-15 15:51:53.499: D/phone(30419): java.lang.NoSuchMethodException: setMobileDataEnabled [boolean]
try {
// log.i("Application running on Ginger bread+");
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.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);
setMobileDataEnabledMethod.invoke(iConnectivityManager, ON);
} catch (Exception e) {
Log.d(TELEPHONY_SERVICE, e.toString());
}
Is there any work round for this problem.
Any help is appreciated
Sahil
setMobileDataEnabled has been removed in Android L.
Use this instead:
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
Method methodSet = Class.forName(tm.getClass().getName()).getDeclaredMethod( "setDataEnabled", Boolean.TYPE);
methodSet.invoke(tm,true);
Make sure you have this permission on your manifest:
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>
This permission is for System apps only

Enable/Disable Mobile Data (GPRS) using code on Froyo 2.2

I'm trying to Enable / Disable Mobile Data Connexion.
I've used this code by rIHaN JiTHiN (Enable/Disable Mobile Data (GPRS) using code) and it's works perfectly on Android 4.0, but it's doesn't on my Galaxy S (Froyo 2.2)...
Is there a way to enable / disable data connexion programmatically ?
If anyone had any idea why it's doesn't work on Froyo, would be really helpful. According to rIHaN JiTHiN, this code works on all Android version...
You can check whether it is enabled or disabled by using below code
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mMobile = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mMobile.isConnected()) {
//if internet connected
}
if it is disabled, you can enable it on your froyo device by using this one
void turnData(boolean ON) throws Exception
{
if(bv == Build.VERSION_CODES.FROYO)
{
Log.i("version:", "Found Froyo");
try{
Method dataConnSwitchmethod;
Class telephonyManagerClass;
Object ITelephonyStub;
Class ITelephonyClass;
TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
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 (ON) {
dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("enableDataConnectivity");
} else {
dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("disableDataConnectivity");
}
dataConnSwitchmethod.setAccessible(true);
dataConnSwitchmethod.invoke(ITelephonyStub);
}catch(Exception e){
Log.e("Error:",e.toString());
}
}
else
{
Log.i("version:", "Found Gingerbread+");
final ConnectivityManager conman = (ConnectivityManager) getApplicationContext().getSystemService(Context.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);
setMobileDataEnabledMethod.invoke(iConnectivityManager, ON);
}
and also dont forget to add these to manifest
android.permission.UPDATE_DEVICE_STATS
android.permission.CHANGE_NETWORK_STATE
android.permission.ACCESS_NETWORK_STATE
android.permission.MODIFY_PHONE_STATE
android.permission.READ_PHONE_STATE

How to provide option to select wi-fi or GPRS for network connectivity in android app

In my app i want to provide the user with the option to choose wi-fi / GPRS for network connectivity to the web server. May be answers to the following ques solve my prob ...
1. How to check which is the current default network connectivity option enabled.
2. How to enable wi-fi/GPRS on user selection or (disable the wi-fi if user chooses GPRS - if only this option will be required for GPRS to work)
or is there some other way around to do this ?
Try this:
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected())
//if wifi connected
}
ConnectivityManager connManager1 = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mMobile = connManager1.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mMobile.isConnected()) {
//if internet connected
}
Don't forget to add these permissions in the manifest file;
android.permission.CHANGE_WIFI_STATE
android.permission.ACCESS_WIFI_STATE
android.permission.UPDATE_DEVICE_STATS
android.permission.CHANGE_NETWORK_STATE
android.permission.ACCESS_NETWORK_STATE
android.permission.MODIFY_PHONE_STATE
android.permission.READ_PHONE_STATE
To enable or disable Wifi, use mWiFi.setWifiEnabled(true|false)
To enable/disable GPRS/3G, use the following code snippet.
void turnData(boolean ON) throws Exception
{
if(bv == Build.VERSION_CODES.FROYO)
{
Log.i("version:", "Found Froyo");
try{
Method dataConnSwitchmethod;
Class telephonyManagerClass;
Object ITelephonyStub;
Class ITelephonyClass;
TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
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 (ON) {
dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("enableDataConnectivity");
} else {
dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("disableDataConnectivity");
}
dataConnSwitchmethod.setAccessible(true);
dataConnSwitchmethod.invoke(ITelephonyStub);
}catch(Exception e){
Log.e("Error:",e.toString());
}
}
else
{
Log.i("version:", "Found Gingerbread+");
final ConnectivityManager conman = (ConnectivityManager) getApplicationContext().getSystemService(Context.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);
setMobileDataEnabledMethod.invoke(iConnectivityManager, ON);
}
}
You can provide option to user on screen using following code block....
public static ShowAvailable()
{
ConnectivityManager connectivityMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] nwInfos = connectivityMgr.getAllNetworkInfo();
for (NetworkInfo nwInfo : nwInfos)
{
Log.d(TAG, "Network Type Name: " +
nwInfo.getTypeName()); Log.d(TAG, "Network available: " +
nwInfo.isAvailable()); Log.d(TAG, "Network c_or-c: " +
nwInfo.isConnectedOrConnecting()); Log.d(TAG, "Network connected: "
+ nwInfo.isConnected());
}
}
Continuing the #rIHaN JiTHiN answer, it should be noted that two permissions android.permission.UPDATE_DEVICE_STATS and android.permission.MODIFY_PHONE_STATE are only granted to system app (as it is initially set in Eclipse or Android Studio). So if someone will be faced with this issue, look at this solution.

Data connection enable/disable code crashes inside a android service

void turnData(boolean ON)
{
final ConnectivityManager conman = (ConnectivityManager) getApplicationContext().getSystemService(Context.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);
setMobileDataEnabledMethod.invoke(iConnectivityManager, ON);
}
The above code is to enable/disable Data Connection in android 2.
3+.
It works without any errors when I run it in an Activity. But when i tried to run it inside a service the application crashed. Can anyone tell me why this happened?
Problem solved by adding the following additional permissions to the Manifest file:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>

Categories

Resources