android.net.conn.CONNECTIVITY_CHANGE issues - android

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)

Related

BOOT_COMPLETED Broadcast Receiver Not working in Some Phones

Lenovo A 1000 mobile not able to listen to Boot Completed Broadcast receiver.The Same code works on other mobiles perfectly.What might be the reason for that? Are there any other alternatives to capture Boot Completed event in android other than Broadcast Receivers?
Some devices (mostly HTC) has a feature called fast reboot which doesn't trigger BOOT_COMPLETED. Instead they trigger QUICKBOOT_POWERON. So you need to register a recever for both broadcasts.
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
You can try this one.

Automatically launching Android app or service when Bluetooth connection is made?

When my app or service is running, I can offer a Bluetooth profile (e.g. SPP, DUN, or my own UUID) using BluetoothAdapter.listenUsingRfcommWithServiceRecord(...).
Is it possible to instead have this Bluetooth profile visible on the device all the time, even when my app or service is not running, and have Android launch my app or service automatically when someone connects to this Bluetooth profile?
I could envision something like this inside AndroidManifest.xml:
<receiver android:name="com.example.MyBluetoothReceiver" android:exported="true">
<intent-filter>
<action android:name="android.bluetooth.adapter.action.RFCOMM_ACCEPT" />
<data android:name="My SPP Service"
android:uuid="00001101-0000-1000-8000-00805F9B34FB" />
</intent-filter>
</receiver>
The goal would be for the Android framework to recognize this receiver purely based on AndroidManifest.xml, automatically offer the Bluetooth profile as specified in the <data ...>-tag whenever the Bluetooth radio is turned on, and then instantiate and call my intent receiver whenever an actual connection is made. This would be similar to the way custom URL handlers work by registering an ACTION_VIEW intent with CATEGORY_BROWSABLE, which the browser then calls to launch my app, even if my app is not currently running.
The closest I could find so far are notifications for general Bluetooth status changes (radio on/off, device paired), e.g. here:
Android Broadcast Receiver bluetooth events catching, or the BluetoothAdapter's ACTION_CONNECTION_STATE_CHANGED intent, which is called for connection to an existing remote profile, rather than a newly offered local one.
Ya its possible in your onReceive() need to add below code to launch the app.
PackageManager pm = context.getPackageManager();
Intent launchIntent = pm.getLaunchIntentForPackage("com.example.blutoothapp");
launchIntent.putExtra("some_data", "value");
context.startActivity(launchIntent);
Hope this will help.

Keeping off Bluetooth/WiFi during application runtime

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.

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

how do I set a broadcast reciever for getting pending intents (proximate alerts)

i have used broadcast reciever for getting alerts on specific location using pending intents(proximate alerts)
but it do not fire any notification when app is closed or in background how i can do this please help me to set this .
here what i tried in xml:
<receiver android:name=".ProximityIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
i also register it in activity :
IntentFilter filter = new IntentFilter(PROXIMTY_ALERT_INTENT);
registerReceiver(new ProximityIntentReceiver(), filter);
in this way im getting notification when activity is running but afert i closed it i do not get any notification. what is proper way to do this.
You could add more actions to your intent filter, this question here should help you with that:
BroadcastReceiver for location
Or there may be a way of creating a Listener to listen for changes in GPS location. Check here for information: http://developer.android.com/reference/android/location/LocationListener.html
I haven't found any documentation regarding PROXIMTY_ALERT_INTENT. Some broadcasts can't be registered from manifest.xml. If this is one of them you can receive those updates only during the application lifetime.
The receiver you defined for BOOT_COMPLETED needs the following permission to work:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
You can start a service in the receiver for BOOT_COMPLETED intent and register there for the proximity alert. Usually having a service running all the time is not recommended and you might want to think for another approach.

Categories

Resources