i am new to android but i know few thinks but this one is hard for me:
First i want to check if i am connected to the internet: works. then i want to check if the mobile data/wifi i enabled: works. and then i want to turn on the mobile network (if its disabled) to check if i have a (good?) connection to the internet: dont work
I used the code from https://stackoverflow.com/a/8962211/1879409 but i dont know how i can call this methode? I also want to do this in an seperate class file (.java file) and call this function from my main activity.
Can someone give me an example how i can do this??
Thanks for all answers xD
EDIT
Now it looks like this in my Settings.java:
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import android.content.Context;
import android.net.ConnectivityManager;
import android.util.Log;
public class SettingsHelper {
static void setMobileDataEnabled(Context context, boolean enabled) {
try {
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, enabled);
Log.i("setMobileDataEnabled()","OK");
}
catch (Exception e)
{
e.printStackTrace();
Log.i("setMobileDataEnabled()","FAIL");
}
}
}
If i call Settings.setMobileDataEnabled(context, true); from my MainActivity i get:
02-01 14:51:19.680: W/System.err(23318): java.lang.NullPointerException
02-01 14:51:19.700: W/System.err(23318): at at.htlmbprojekt.wksimonsfeld.iceapp.SettingsHelper.setMobileDataEnabled(SettingsHelper.java:15)
02-01 14:51:19.700: W/System.err(23318): at at.htlmbprojekt.wksimonsfeld.iceapp.MainActivity.checkIfStorageAvailable(MainActivity.java:83)
02-01 14:51:19.700: W/System.err(23318): at at.htlmbprojekt.wksimonsfeld.iceapp.MainActivity.access$4(MainActivity.java:81)
02-01 14:51:19.700: W/System.err(23318): at at.htlmbprojekt.wksimonsfeld.iceapp.MainActivity$2.onClick(MainActivity.java:76)
02-01 14:51:19.700: W/System.err(23318): at android.view.View.performClick(View.java:4211)
02-01 14:51:19.700: W/System.err(23318): at android.view.View$PerformClick.run(View.java:17267)
02-01 14:51:19.700: W/System.err(23318): at android.os.Handler.handleCallback(Handler.java:615)
02-01 14:51:19.700: W/System.err(23318): at android.os.Handler.dispatchMessage(Handler.java:92)
02-01 14:51:19.700: W/System.err(23318): at android.os.Looper.loop(Looper.java:137)
02-01 14:51:19.700: W/System.err(23318): at android.app.ActivityThread.main(ActivityThread.java:4898)
02-01 14:51:19.700: W/System.err(23318): at java.lang.reflect.Method.invokeNative(Native Method)
02-01 14:51:19.700: W/System.err(23318): at java.lang.reflect.Method.invoke(Method.java:511)
02-01 14:51:19.700: W/System.err(23318): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-01 14:51:19.700: W/System.err(23318): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-01 14:51:19.700: W/System.err(23318): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:103)
02-01 14:51:19.700: W/System.err(23318): at dalvik.system.NativeStart.main(Native Method)
Maybe because the context is null? If yes what should i do with he context? If anyone can solve my problem pls send me the WHOLE code and not only some snippets i am a beginner and i dont know what i should do with these snippets xD