I am developing an application which does some downloading depends on whether the
user has enabled the option in Settings-> Wireless and network-> Mobile networks -> Use packet data.
I need to check the user has enabled this options.
Please help me to find out how can i get this settings.
For eg to check the roaming mode I use.,
import android.provider.Settings.Secure;
String android_id = Secure.getString(getContentResolver(),
Secure.DATA_ROAMING);
Thanks in advance
Deepu
works on 2.3:
ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
Method method = ConnectivityManager.class.getDeclaredMethod("getMobileDataEnabled");
method.setAccessible(true);
boolean res = (Boolean)method.invoke(manager);
Here I access private method ( boolean getMobileDataEnabled() ) by reflection, it is a not so good practice, but as we know, in Android it, in usual, the only way to implement something (even though CommonWare and other nerds think :)
Related
I want to see to which wifi network i am connected on my android device using kivy
Platform?
cant find any reference, do you know if its possible and if it is, how its done.
Thanks
I tried implementing it as you suggested:
for some reason none of the wifimanger methods i am using(getconnectioninfo) keep on failing.
anyone sees what i am missing here?
import jnius
PythonActivity = jnius.autoclass('org.renpy.android.PythonActivity')
activity = PythonActivity.mActivity
WifiManager = jnius.autoclass('android.net.wifi.WifiManager')
WifiName= jnius.autoclass('android.net.wifi.WifiInfo')
network_name = WifiName()
wifi_service = activity.getSystemService(PythonActivity.WIFI_SERVICE)
network= wifi_service.getConnectionInfo()
network_name = network.getBSSID()
Logger.debug('wifi: wifi_names{0}'.format(network))
Logger.debug('wifi: wifi_names{0}'.format(type(wifi_service)))
This is probably possible, you will need to look up the normal android api way to do it then access it directly with pyjnius.
As of Android 4.1, your device can detect if it's connected to a mobile hotspot (given that the mobile hotspot is also running Android 4.1 or higher). Also, you have the option to flag networks as mobile hotspots (under Settings / Data Usage / Overflow menu / Mobile Hotspots).
But how do I detect this as a -user- I meant developer? It's not stored in the WifiConfiguration, so where is it?
Some context: I want to build a simple tool for Android that checks if you are connected to a network that you or Android has flagged as a mobile hotspot. If so, it will check if no other (non-hotspot) networks are available. If so, it should connect to these other networks since those should be much faster and have no data cap. Why? Because my phones and tablets connect to (mobile) hotspots quite often, even when a better network is available.
Here is some pseudo code of what I'm looking for:
// Check if android has detected mobile hotspot
WifiManager wifiMgr = getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr .getConnectionInfo();
boolean isMobileHotspot = wifiInfo.isMobileHotspot;
UPDATE Jul 3rd 2014
Okay so Matiash' answer is good but ConnectivityManager.isActiveNetworkMetered() will only return the value for the current network. I do need that, so it helped me along, but it bring me to the next part in my tool/app:
IF the device is connected to a mobile hotspot (or a 'metered network' as Android calls it) I want to check if any of the nearby access points is a better option. So I need to know whether any of the known AP's (WifiManager.getConfiguredNetworks()) is also flagged as such before I connect to it...
I have a List<ScanResult> and a List<WifiConfiguration>, looks like neither of them has this information.
Which bring me back to my initial question: Is there a way to retrieve the Mobile Hotspots (as configured by Android and/or user) under Data Usage? And this time I mean ALL of them.
UPDATE Jul 7th 2014
I've posted a feature request in the AOSP Issue Tracker for access (readonly) to the NetworkPolicyManager. Plz vote on it here: https://code.google.com/p/android/issues/detail?id=73206&thanks=73206&ts=1404719243
You can access this information by calling ConnectivityManager.isActiveNetworkMetered().
This will return whether the active connection is a hotspot (as defined in Data Usage -> Mobile Hotspots).
About the second part, I'm sorry but I don't think that's possible. The flag is not public, and even if you get the object that could be used to retrieve it (android.net.NetworkPolicyManager) by reflection:
Object npm = Class.forName("android.net.NetworkPolicyManager").getDeclaredMethod("from", Context.class).invoke(null, this);
Object policies = npm.getClass().getDeclaredMethod("getNetworkPolicies").invoke(npm);
calling getNetworkPolicies() requires the MANAGE_NETWORK_POLICY permission, which cannot be obtained by non-system apps, because it has a "signature" protection level.
I hope to be proved incorrect though. :) Maybe looking at the source code of the Android activity that manages this information (https://github.com/android/platform_packages_apps_settings/blob/master/src/com/android/settings/net/DataUsageMeteredSettings.java), in particular the buildWifiPref() method, will provide some clue.
I do not know if what you want is possible but you can check whether your device is connected to a network by checking the ip.
You can use the tool below to see if you has ip, and shalt know if he is connected to a network or not.
public static Boolean check_connection(final Context _context)
{
boolean connected;
ConnectivityManager conectivtyManager = (ConnectivityManager) _context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conectivtyManager.getActiveNetworkInfo() != null
&& conectivtyManager.getActiveNetworkInfo().isAvailable()
&& conectivtyManager.getActiveNetworkInfo().isConnected())
{
connected = true;
} else
{
connected = false;
}
return connected;
}
//Check if hotspot tethering is enabled
try {
ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
boolean isMobileData = connectivityManager.isActiveNetworkMetered();
if(isMobileData) {
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface networkInterface : interfaces) {
if (networkInterface.getName().equals("ap0")) {
//Tethering is enabled
SendHotspotEnabledHandler sendHotspotEnabledHandler = new SendHotspotEnabledHandler(new WeakReference<Context>(SendInstalledAppsService.this));
sendHotspotEnabledHandler.execute();
break;
}
}
}
} catch (SocketException e) {
}
suggest me source code. My source code is
TelephonyManager manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if(manager.getNetworkType()==TelephonyManager.NETWORK_TYPE_UMTS){
// tx.setText("Edge");
//int enabled = Settings.Secure.getInt(getContentResolver(),
// "preferred_network_mode", -1);
cm.setNetworkPreference(TelephonyManager.NETWORK_TYPE_EDGE);
cm.startUsingNetworkFeature(TelephonyManager.NETWORK_TYPE_EDGE, "Deneme");
}
You cannot do this as it is a restricted setting. You need a special permission, to change it. Take a look at this post.
There is no exported to the SDK functionality to switch between 2G and 3G. For a given device you could probably figure out the private functionality, but it wouldn't work unless the app was signed with the system key.
You can disable the radios though, by turning on airplane mode.
And you might be able to make a shortcut to open the appropriate settings activity directly, instead of going through a few levels of menus to get there.
If you make your own build, you can presumably add the capability you really want, but that's likely not useful to anyone but yourself.
Is it considered best practice or more acceptable or, for any other reason, preferable, to initialise WiFI on an Android device via a programmatic approach:
WifiManager oWiFIMgr = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
oWiFIMgr .setWifiEnabled(true);
vs. launching the WiFI settings activity?
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
I guess it depends on the purpose of your app.
If you are going to create a home screen widget like wifi toggle or something similar, the user would be pleased if just touching the widget turns On the wifi, but if its some app that just require the wifi access to do certain task, it would be better to open the wifi page allowing the user to take his own choice.
What really matters is you should design your app in such a way that once the purpose of wifi is done, it should be turned off again.
PS: No matter which choice you make in your app design the permissions of that app is going to be displayed during the installation.
So just keep in mind for the user friendliness of your app and its performance.
In Android Q (Android 10) you can't enable/disable wifi programmatically anymore. So you don't have a choice, you need to use Settings Panel to toggle wifi connectivity:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val panelIntent = Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY)
startActivityForResult(panelIntent, 0)
}
Always let user take those kind of decisions, enable WiFi, GPS .. stuff like that .. new Google maps app does that .. i think it's best ..
I'm developing a simple application for the Google Android to turn on and off the wifi or 3g or 2g.
I see http://developer.android.com/reference/android/net/wifi/WifiManager.html#isWifiEnabled()
that you can see if the wifi is enabled or disabled and also us
http://developer.android.com/reference/android/net/wifi/WifiManager.html#setWifiEnabled(boolean)
to turn on and off the wifi.
I'm wondering if it's possible to do the same for 3G and for 2G/GPRS?
I know it's possible because you can turn off 3G and left 2G on.
2G/3G
To determine your network type use:
TelephonyManager.getNetworkType();
here's some example code:
bool is3G = (manager.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
Docs for the class can be found at: TelephonyManager
On/Off
To check if your telephone radio is on or off use:
ServiceState.getState();
To set it use:
ServiceState.setState(STATE_POWER_OFF);
It's unclear whether the setState method exists on all devices and functions in all states. There is no documentation for this method. Documentation for the class can be found at: ServiceState
This issue might also be relevant: http://code.google.com/p/android/issues/detail?id=1065
You can also use ConnectivityManager. Something like that:
ConnectivityManager connectivityManager =(ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();