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);
Related
Phone A has mobile data ON and is sharing internet via Wi-Fi hotspot to Phone B. If Mobile Data is TURNED OFF in phone A, no connectivity change is received #PhoneB. How can I get this change in Phone B when mobile data is turned ON/OFF in Phone A?
Phone B is Android phone.
Yes there is. Simply register BroadcastReceiver for connectivity changes:
#Override
public void register(Context context) {
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (isOnline()) {
if (listener != null) {
listener.onConnected();
}
hideNoConnectionError();
} else {
showNoConnectionError();
}
}
};
final IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
context.registerReceiver(receiver, intentFilter);
}
and on change event check whatever connectivity change there was
In my application I have to get notified whenever the device connects or disconnects from a WIFI network. For this I have to use a BroadcastReceiver but after reading through different articles and questions here on SO I'm a bit confused which Broadcast action I should use for this. In my opinion I have three choices:
SUPPLICANT_CONNECTION_CHANGE_ACTION
NETWORK_STATE_CHANGED_ACTION
CONNECTIVITY_ACTION
To reduce resources I really only want to get notified whenever the device is CONNECTED to a WIFI network (and it has received an IP address) or when the device has DISCONNECTED from one. I do not care about the other states like CONNECTING etc.
So what do you think is the best Broadcast action I should use for this? And do I have to manully filter the events (because I receieve more then CONNECTED and DISCONNECTED) in onReceive?
EDIT: As I pointed out in a comment below I think SUPPLICANT_CONNECTION_CHANGE_ACTION would be the best choice for me but it is never fired or received by my application. Others have the same problem with this broadcast but a real solution for this is never proposed (in fact other broadcasts are used). Any ideas for this?
You can go for WifiManager.NETWORK_STATE_CHANGED_ACTION works.
Register receiver with WifiManager.NETWORK_STATE_CHANGED_ACTION Action, either in Manifest or Fragment or Activity, which ever suited for you.
Override receiver :
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if(action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
boolean connected = info.isConnected();
if (connected)
//call your method
}
}
Please try
#Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction("android.net.wifi.STATE_CHANGE");
registerReceiver(networkChangeReceiver, filter);
}
#Override
protected void onDestroy() {
unregisterReceiver(networkChangeReceiver);
super.onDestroy();
}
and
BroadcastReceiver networkChangeReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (!AppUtils.hasNetworkConnection(context)) {
showSnackBarToast(getNetworkErrorMessage());
}
}
};
I am using this and it is working for me. Hope it will help you out.
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);
I want to receive an interrupt when the android device starts charging either thr USB or AC
I have registered the receiver through the following code--
registerReceiver(powerMonitor,newIntentFilter("android.intent.action.ACTION_POWER_CONNECTED"));
The code which accepts the broadcast is--
public void onReceive(Context context, Intent intent) {
powerMonitor = new BroadcastReceiver() {
public void onReceive(Context context,Intent intent) {
if (intent.getAction().equals("android.intent.action.ACTION_POWER_CONNECTED"))
{
msg = "power connected";
}
The issue is when I connect the device through a USB cable I fail to get the interrupt and if I try to remove and plug the USB cable,the VM gets disconnected..How do I check it?
Have I gone wrong?
I even tried d following code--
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(broadcastReceiver, filter);
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
if(plugged==BatteryManager.BATTERY_PLUGGED_AC ||plugged==BatteryManager.BATTERY_PLUGGED_USB)
{
msg="power connected"
}
Please help!!
please have a look on this link (The answer given by me)
activate an application when a power button is clicked
this will help you to solve your problem.
im trying to make an android application that whenever a user conncects to a wifi, application sends an http request to my server, later on by using push notification server sends some msgs to the user. but in here i have a problems: how to make this application running in background and also it understand if phone is connected to a wifi, means even if applcation is closed it still be aware and whenever the phone is connected to a wifi it sends the http request. Thanks
private void registerForWifiBroadcasts() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
mContext.registerReceiver(mReceiver, intentFilter);
}
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
handleNetworkStateChanged(
(NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO));
} else if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
handleSupplicantConnectionChanged(
intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false));
} else if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
handleWifiStateChanged(intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
WifiManager.WIFI_STATE_UNKNOWN));
}
}
And: Make it a Service