So I've created the majority of my application but I am having an issue with power saving applications interfering with it. I use the AlarmManager to run a piece of code that send information to a server every x minutes (minimum 1h), the main issue I am having is that power managers are disabling with WiFi because the device is sleeping.
What's the most effective way to ensure WiFi is available at wakeup? Is it to simply enable WiFi and reconnect it?
Maybe an other way to your solution is to listen for the connection_changed intents. That way you know there is a connection to the internet and you can upload.
You can also enable WiFi but you will need permissions for that: (I guess these)
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(enabled);
Haven't tried it but this should do the trick.
Related
I have Two buttons to disable WIFI and GPS functionality. On Clicking on button user will not allowed to change the state of gps or wifi. it has to completely block the wifi and gps. Any one having solutions?
WifiManager wifiManager = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(status);
also, add permission. You can find the details from here
You also need the following permissions in your manifest file:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
WifiManager wifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(status);
to Block wifi Completely we need a Broadcast receiver who will detect the event for wifi status change. And if the status is on we need to make it off.. by this i resoled it issue.
I am trying to scan wifi networks every 2 minutes. I am using service for that. When the phone is "active" or "in use" after startscan() called I get SCAN_RESULTS_AVAILABLE_ACTION in 6 sec. So i can scan for wifis periodically. But after the phone has not been touched by anyone for a certain time (10 min) startscan() stops "working" and only after 10 min getting result. Anybody experienced this?
According to this I have found the solution - this is because the Wifi sleep policy.
You can set your wifi device never goes to sleep with this:
Settings.System.putInt(getContentResolver(),
Settings.System.WIFI_SLEEP_POLICY,
Settings.System.WIFI_SLEEP_POLICY_NEVER);
Be sure, that you added this permission in AndroidManifest.xml file:
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
the scanning period will be 5-15s - and this is automatic - you dont need to call startscan(). more info about Android scanning process can be found here.
Edit:
maybe this is even better solution if you only want to scan for hotspots:
WIFI_MODE_SCAN_ONLY - it can be activated by:
WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiLock wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_SCAN_ONLY , "MyWifiLock");
if(!wifiLock.isHeld()){
wifiLock.acquire();
}
dont forget to release it, more info about WifiLock here
also define this permission:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
Edit2 this works as well:
you can enable, and disable wifi periodically:
WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if(wm.isWifiEnabled() == false) {
wm.setWifiEnabled(true);
}
then scan after your broadcastreceiver gets the action containing WIFI_STATE_ENABLED extra
The following will not turn wifi on, if the user has it turned off, right?:
WifiManager manager = (WifiManager)context.getSystemService(
Context.WIFI_SERVICE);
manager.startScan(); <-- !
I want to make sure the wifi isn't being turned on (if off) when the above is called,
Thanks
Yes, you are correct. The above code will not turn on wifi.
You can however use setWifiEnabled(true) to enable wi-fi access. You will need to add android.permission.CHANGE_WIFI_STATE to your manifest file.
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.
I'm trying to receive data from a multicast address, but the call to MulticastSocket.receive() blocks until a timeout takes place.
I did some network sniffing and found out that my device (and the emulator) never send a MulticastSocket.joinGroup request.
I tried running the same Java code from my PC as a standalone application and it worked well. Could it be that the Android platform blocks IGMP join requests?
Has anyone succeeded with Multicast on Android before?
My manifest file contains the following permission:
I am running my application on 2.1 (Both emulator & device).
Any ideas anyone?
Thanks,
Lukas gives the best explanation and examples that I've seen on his blog: http://codeisland.org/2012/udp-multicast-on-android
In summary:
1. You need the permissions:
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
2. You need a lock:
WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
if (wifi != null){
WifiManager.MulticastLock lock = wifi.createMulticastLock("mylock");
lock.acquire();
}
3. You have to have a device that supports receiving multicast. Or you can follow his work around for rooted devices.
As it seems, there is no proper multicast support in the emulator.
Here's a bug report and related thread. It is being fixed for froyo.
You need to do something like this
WifiManager wifi = (WifiManager)getSystemService( Context.WIFI_SERVICE );
if(wifi != null)
{
MulticastLock mcLock = wifi.createMulticastLock("mylock");
mcLock.acquire();
}
Reference:
http://developer.android.com/reference/android/net/wifi/WifiManager.MulticastLock.html
I read all 2.1 devices not supporting IGMP stack.
IGMP was missing on different HTC, Samsung, and Motorola devices of all android version from 2.1 up to 3.2.
Link in which i read http://www.programmingmobile.com/2012/01/multicast-and-android-big-headache.html