Keeping off Bluetooth/WiFi during application runtime - android

We can turn on/off WiFi/Bluetooth programmatically.
But is it possible that as long as the application is running that has
turned off WiFi/Bluetooth they would remain off.
I mean to say that if User turn WiFi on then application will again turn that to off?

You can add an intent filter and register a receiver when your application is started
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
<action android:name="android.net.wifi.STATE_CHANGE"/>
</intent-filter>
register receiver on onCreate
IntentFilter filters = new IntentFilter();
filters.addAction("android.net.wifi.WIFI_STATE_CHANGED");
filters.addAction("android.net.wifi.STATE_CHANGE");
super.registerReceiver(wifiStateListener, filters);
although in your case I think only "android.net.wifi.WIFI_STATE_CHANGED" will suffice. Whenever the state of WIFI (enable, disable, connect, disconnect) is changed it is fired by os.
You need to add the permission to read/modify WIFI state.
Hope this helps.

Related

Monitor WiFi sleep mode events

I have to monitor the connectivity of the device automatically, to do that I have my receiver registered with
<receiver android:name=".NetworkStateReceiver" android:exported="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
<action android:name="android.net.wifi.STATE_CHANGE"/>
<action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" />
</intent-filter>
</receiver>
It works fine, but when the WiFi goes to sleep mode, the network info will have the state DISCONNECTED/BLOCKED. This is ok, I understand why it does this to save battery etc.
The issue is that the receiver doesn't receive an intent when the WiFi goes to sleep mode. Same thing if I wake up the device, the WiFi will switch to connected state but I won't receive an intent.
(Tested on a Nexus 5X)
Any way I could receive those events without having to use an alarm that would fire every X minutes to check the state?
Checked Connectivity Change Broadcast receiver not triggering when phone is in sleep mode but answer seems irrelevant

android.net.conn.CONNECTIVITY_CHANGE issues

I would like to implement a listener to change the state of WiFi and mobile Internet
created a simple BroadcastReceiver c intent filter android.net.conn.CONNECTIVITY_CHANGE
it works, but I've noticed that when you turn on/off my WiFi BroadcastReceiver receives two identical intent with a delay of about 100 milliseconds.
Why and how can I fix it?
please add this Intent Filter too..
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
this will notify you about the Wifi State (ON or OFF)

Android shutdown/power off broadcast receiver not starting

I want to create a traffic widget, and store the used traffic at each device shutdown. The problem is that I can't get the shutdown receiver to trigger.
I used the following code:
IntentFilter actionShutdown = new IntentFilter("android.intent.action.ACTION_SHUTDOWN");
IntentFilter quickPOFF = new IntentFilter("android.intent.action.QUICKBOOT_POWEROFF");
TrafficDataUpdate trafficDataUpdate = new TrafficDataUpdate();
getContext().registerReceiver(trafficDataUpdate, actionShutdown);
getContext().registerReceiver(trafficDataUpdate, quickPOFF);
I also tried with the receiver declared in manifest, but no success:
<receiver android:name=".receivers.TrafficDataUpdate">
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN"/>
<action android:name="android.intent.action.QUICKBOOT_POWEROFF"/>
</intent-filter>
</receiver>
Do you have suggestions?
Can someone recommend me another way of counting the data traffic, without the shutdown receiver?
Use one IntentFilter, not two, teaching it to listen for both actions. Your second registerReceiver() is implicitly unregistering the first receiver.
Also note that this will only work so long as your process is still in memory. Usually, your process is not in memory, and it is in the user's best interests for your process to not be in memory. Please replace this implementation with one where the receiver is registered in the manifest, as is shown in the following StackOverflow threads:
Android ACTION_SHUTDOWN Broadcast not working
BroadcastReceiver's behaviour for ACTION_SHUTDOWN
handling phone shutdown event in android

What determines app auto start on boot in Android?

In my application, there's a feature that allows users to dial a specific number and brings up an activity to front. I have the following receiver, and the only receiver registered in AndroidManifest.xml.
<receiver android:name="com.example.myapp.OutgoingCallListener" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
Please note there's no BOOT_COMPLETED intent or service.
Now here's the thing I couldn't figure out. When I reboot my device, go check the Running Apps, my application is not listed there. But, if I dial the specific number, my application starts and the activity is brought to front.
My question is: If the app is not a service, and not started on boot, how could it recieve intent from Android? That is, in my case, how could my app listen to NEW_OUTGOING_CALL while it's not started at all?
A BroadcastReceiver that is registered in the manifest is always capable of responding to a matching broadcast. If your process is not running for any reason, Android will start up a process for you.

handling phone shutdown event in android

Could you post a simple code to handle shutdown event.
I need manifest and receiver.
I need the following:
I have a service running in background and I want to detect phone shutting down in order to make it in silent mode so that the terrible operator music doesn't play.
After restarting the phone (I already have on boot receiver) the service will enable phone sound.
You need to listen for the following intent action in your receiver:
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
To make it compatible with some HTC (or other) devices which offer quick power-off feature, you need to include the following action too with in the same intent-filter:
<action android:name="android.intent.action.QUICKBOOT_POWEROFF" />
For more info, read this
<receiver android:name=".ShutdownReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
<action android:name="android.intent.action.QUICKBOOT_POWEROFF" />
</intent-filter>
</receiver>
public static final String ACTION_SHUTDOWN
Broadcast Action: Device is shutting down. This is broadcast when the device is being shut down (completely turned off, not sleeping). Once the broadcast is complete, the final shutdown will proceed and all unsaved data lost. Apps will not normally need to handle this, since the foreground activity will be paused as well.
This is a protected intent that can only be sent by the system.
Constant Value: "android.intent.action.ACTION_SHUTDOWN"
And this ACTION_SHUTDOWN may differ from device to device
Like HTC its android.intent.action.QUICKBOOT_POWEROFF

Categories

Resources