NetworkReceiver never called after changes to Network State - android

To conserver battery in my application, whenever my app needs to sync with the cloud, I first check if the network is available. If no connection is available, I register a network broadcast receiver as follows. But this never gets called.
I'm testing this by putting the device into airplane mode. I see the "Network Receiver ENABLED" message. But after I disable airplane mode, and after my Wifi connects, I expect to see the "Received Network Change Intent" message, and it never appears.
Can anyone point out what I may be doing wrong?
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
networkReceiver = new NetworkReceiver();
registerReceiver(networkReceiver, filter);
Log.d(TAG, "Network Receiver ENABLED");
This is the NetworkReceiver:
public class NetworkReceiver extends BroadcastReceiver {
private static String TAG = NetworkReceiver.class.getSimpleName();
#Override
public void onReceive(Context context_, Intent intent_) {
Log.d(TAG, "Received Network Change Intent");
}
}

You can try below option.
Set Network Access Permission if you haven't declare in manifest file: <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"> </uses-permission>.
Register Receiver using context:
Instead of registerReceiver(networkReceiver, filter); use getApplicationContext().registerReceiver(networkReceiver, filter);

Related

Bluetooth scan returns nothing

I am recently learning Android. I tried to implement the function that searches for Bluetooth devices. I am able to list all the previously paired devices, but I am not able to discover new devices.
Permissions are set in AndroidManifest.xml.
Here is my code:
public void listDiscoverableDevices(boolean status) {
// Create a BroadcastReceiver for ACTION_FOUND
mBluetoothAdapter.cancelDiscovery();
mArrayAdapter.clear();
mDisplay.setText("Search");
mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//BluetoothDevice.
if(BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mArrayAdapter.add(device.getAddress());
} else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
//mDisplay.setText(action);
mArrayAdapter.add(action);
} else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
//mDisplay.setText(action);
mArrayAdapter.add(action);
}
}
};
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
if(!mBluetoothAdapter.startDiscovery()) {
mDisplay.setText("Discover failed!");
}
}
It seems to me that the BluetoothDevice.ACTION_FOUND action is never received. I have been struggling with this for two days. Any idea?
It took me two days. Today I finally found out that why my program didn't work the first time. Starting from Android 6.0, you also need either android.permission.ACCESS_FINE_LOCATION or android.permission.ACCESS_FINE_LOCATION permission to scan for remote Bluetooth devices.
And you may need to go to "Settings"->"Apps"->yourApp->"Permissions" to turn on the location service permission. Or I guess you can also request the permission programmatically.

Android: broadcast receiver not working

I have the following class for connection listener
public class connectionListener{
public ConnectivityManager conMgr;
public htmlParserListener vystrahyListener;
public htmlParserListener podmienkyListener;
private ConnectivityBroadcastReceiver mReceiver;
public connectionListener(ConnectivityManager conMgr, htmlParserListener vystrahyListener, htmlParserListener podmienkyListener){
this.conMgr = conMgr;
this.vystrahyListener = vystrahyListener;
this.podmienkyListener = podmienkyListener;
}
private class ConnectivityBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("connected");
}
}
public synchronized void startListening(Context context) {
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
context.registerReceiver(mReceiver, filter);
System.out.println("start");
}
}
I instantiate it with this code (from MainActivity):
connectionListener conLst = new connectionListener(conMgr, vystrahyListener, podmienkyListener);
conLst.startListening(this);
However I am getting "start" at output so startListening method works but I dont get "connected" when I disable or enable mobile data. So it looks like my BroadcastReceiver is not working. What am I doing wrong?
Thanks in forward
I dont get "connected" when I disable or enable mobile data.
That is correct. This Broadcast is not triggered when mobile data is enabled or disabled but only when the network connectivity changes.
So it looks like my BroadcastReceiver is not working. What am I doing wrong?
Your BroadcastReceiver is working correctly. However, you expect the CONNECTIVITY_ACTION Broadcast to be triggered at an event where it isn't triggered.
Note: there exists no (other) Broadcast which is sent by the system when the enabled state of mobile data is changed. You also have no option to register a Listener for this change.

Android bluetooth ACTION_DISCOVERY_FINISHED not working

I have written my first Android app and everything is working really well, except...in the routine, below, the ACTION_DISCOVERY_FINISHED never seems to get called (or broadcast or received or whatever). No matter what the block of code in that "else if" is not working.
I have only tested on my Motorola Atrix, so I am wondering if that is the issue. Since I am testing bluetooth functionality, I don't think I can use the Android emulator for effective testing.
Thoughts?
private BluetoothAdapter mBtAdapter;
mBtAdapter.startDiscovery();
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
//do something
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
//do something else
}
}
}
2 possibles solutions:
Instead of creating an annonymous receiver, subclass BroadcastReceiver with just the same implementation, then declare it in your project manifest (Remember to declare that your receiver receives these actions you want).
Dynamically register it from your activity/service, this way:
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(mReceiver, filter);
I'm not sure if you have to unregister it when registering it from an activity/service (I know you have to when registering from app's context) so check it out.
You need to add the line
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
to your manifest

BroadcastReceiver for screen lock not being triggered

I'm trying to turn the GPS location updates off when the screen locks. Having read the answer to this question Android - how to receive broadcast intents ACTION_SCREEN_ON/OFF?
I've written some code to implement a BroadcastReceiver but it's not working when the screen goes off.
I've registered a BroadcastReceiver in my code with
#Override
public void onResume() {
super.onResume();
IntentFilter iFilter = new IntentFilter();
iFilter.addAction("android.intent.action.ACTION_SCREEN_ON");
iFilter.addAction("android.intent.action.ACTION_SCREEN_OFF");
registerReceiver(screenStatReceiver, iFilter);
and the receiver itself is just a stub for now:
public BroadcastReceiver screenStatReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.d("BCAST_TAG", "Got broadcast");
String action = intent.getAction();
}
};
and in the manifest I have:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Any ideas as to why it's not being triggered when I debug it on my phone?
For this problem, you have to create a infinite service, which is registering a local broadcast receiver for these intents. If you do this way, then your app will look for screen off but make sure you have to make service which will always running in background like reboot receiver
You are registering the reciever in the onResume() method. Why dont you register it in the onCreate() so you can have persistent "listening"?
In this case you are listening for changes only after the activity is resumed.

How can I monitor the network connection status in Android?

I have a Service that is downloading a file from a server. I warn the user to only download the file over Wireless LAN before the download starts.
My problem is that my download gets stuck if I loose the network connection. Is there a way to listen for changes in network connection or if it is lost entirely?
Listen for CONNECTIVITY_ACTION
This looks like good sample code. Here is a snippet:
BroadcastReceiver networkStateReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.w("Network Listener", "Network Type Changed");
}
};
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(networkStateReceiver, filter);

Categories

Resources