Alternative to enabling data connection - android

On android 2.3 the permission <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" /> is not allowed anymore for non-system applications. Is there an alternative to enable/disable data connection, besides that one where you modify APN name? I found Data Enabler Widget on Android Market that does that, but I can't seem to understand how. Can anyone help me?
Thanks! - Alex Ady

I don't know how to change data connection (3g, 2g, etc), but you can enable/disable wifi connection through this:
WifiManager wifiManager = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(boolean enabled);

I found a solution to my problem, so I'm closing this question. The alternative is to simply display the mobile settings activity if a level 10 API or higher is detected, or continue with direct enable from code otherwise. I keep the android.permission.MODIFY_PHONE_STATE, but only use it the API is under level 10.

Related

WiFi suggestion API fail on Android

I've been trying to use the new Android WiFi Suggestion API with the code taken exactly from the example, but each time I try to connect to a network, when I call wifiManager.addNetworkSuggestions(suggestionsList) I get status 3 in response, which (according to this site) means IP provision failure. What does that mean? How can I handle that?
My app has all the required permissions and even some more (CHANGE_WIFI_STATE, ACCESS_WIFI_STATE, INTERNET, ACCESS_FINE_LOCATION, CHANGE_NETWORK_STATE, ACCESS_NETWORK_STATE), and the location is turned on. I tried with a few WiFi networks (all are visible in the WiFi settings), but still got the same IP provisioning error.
Had the same issue and found it quite confusing because status = 3 can mean two things: STATUS_SUGGESTION_APPROVAL_REJECTED_BY_USER or STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_DUPLICATE.
In my case it was the latter. Basically the suggestion is remembered once submitted. I solved this by removing my suggestion before it's added:
wifiManager.removeNetworkSuggestions(suggestions)
val status = wifiManager.addNetworkSuggestions(suggestions)

Read Preferred Network Type Setting

I am trying to read the Preferred network type setting from the device. But nowhere android API's are available.
Use case:
Trying to read the Preferred network type and connected network type so that if the device has LTE enabled and the user is forcefully switched back to the lower network(3G,2G); then there should be a notification sent to the user.
I have checked the system setting code, But it's deprecated.
Settings.Secure.getString(getApplicationContext().getContentResolver(),
Settings.System.NETWORK_PREFERENCE);
Is there any alternate way to read the system secured settings(By reflection?).
And Also is it possible to write back the setting with the user permission?
Help is much appreciated.
I think the right code is:
Settings.Global.getString(context.getContentResolver(),Settings.Global.NETWORK_PREFERENCE)

Enabling Wifi direct in Android 4.0

I have bought a new android 4.0.3 tablet- Micromax Funbook P300 to learn developing android applications.
I started with Wifi Direct, so that the tablet could be used as a remote for robotic platform.
To my disappointment the stock OS doesn't provide this function in the settings menu.
Is it possible to check if we can programatically start wifi direct feature?
If not can someone direct to some tutorials which addresses this?
Thanks.
The Android SDK contains a sample application for using the DirectWiFi mode:
WiFiDirectDemo - Wi-Fi Direct Demo
The used API itself is located in android.net.wifi.p2p
WiFi Direct should be supported by Android 4.0.3 (it has been around since Android API 14, or Android 4.0). It is possible that your tablet does not support WiFi Direct due to a hardware limitation, but I doubt it. You probably aren't seeing it in the settings because there is a custom Android skin running on the tablet that prevents you from seeing it, or maybe the WiFi Direct settings interface wasn't implemented until Android 4.1 or something.
Whatever the case, you can test it easily in the code.
First, put the proper permissions in your manifest.xml
http://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html#permissions
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.nsdchat"
...
<uses-permission
android:required="true"
android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission
android:required="true"
android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission
android:required="true"
android:name="android.permission.INTERNET"/>
...
Next, try initializing the Android WiFiP2pManager class to see if it is supported.
http://developer.android.com/reference/android/net/wifi/p2p/WifiP2pManager.html
public void onCreate(Bundle savedInstanceState) {
WifiP2pManager manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
WifiP2pManager.Channel channel = manager.initialize(this, getMainLooper(), null);
}
Where I put null, you could pass a callback to check for failure. If it works, follow the rest of the Wi-Fi Direct guide to create your app.
He's not asking for a coded program example, so much as a way to programatically switch on the technology (ie one line of code). As far as I am aware there is no way to do this. It all has to be done from within your devices settings. While some devices allow Wi-Fi Direct to be switched on, other devices treat it the same as having regular Wi-Fi enabled - have you tried this? And have you checked if the P300 even supports Wi-Fi Direct?

How to enable HSDPA programmatically in Android?

Is it possible to enable or disable HSDPA connection programatically in Android?
If so, how to do that? I didn't find any API in TelephonyManager.
It is not possible to enable Network(CDMA, 3G or HSDPS) programmatically as of now. As we know we can enable WiFi programatically.
But we can set the preferable network among Mobile / WiFi.
ConnectionManager.setNetworkPreference(ConnectionManager.TYPE_MOBILE/TYPE_WIFI);
Unfortunately, it is not working for me. When I tried to set TYPE_MOBILE, I am getting Security Exception even if I had required permission in my manifest.
When I tried to set TYPE_WIFI, It is not throwing exception, but while getting network preference, I am getting "-1", means there is no preferred network(No Documentation, its my prediction).

Settings management from code

I just want to ask you if you anyone knows how to change the Setting
available in:
Settings->Wireless Settings->Mobile Network from code or
Setting: Settings->Wireless Settings->Mobile Network Settings->Network
operators->Select Automatically?
I had been unable to find a property that match this setting in the
Settings.System / Settings.Secure classes.
Is there any alternative way to "turn on/off" the MOBILE
connectivity ?
The reason I need to solve this is to workaround the
bug occur during transitioning from WIFI to MOBILE that do not enable
CELL data automatically.
I think support for this sort of thing(changing settings from code) was removed/deprecated in the newer versions of the SDK; it is not advisable. What I've said is based on Dan Morill's Blog titled: Future Proofing your Android Apps
Eitherways, generally, you would write:
Settings.System.putString(getContentResolver(), <Settings.System.CONST>, <value>);
and have the following permission in your maifest: android.permission.WRITE_SETTINGS
The List of Settings.System.CONST can be found in the official documentation: see here

Categories

Resources