I have an AlarmManager which sets a repeating alarm. (Not actually an alarm, but send mail). So in the BroadcastReceiver I do the actual sending of the mail. But to send mail one needs Internet. So when the phone doesn't have an Internet connection the email doesn't get sent. How do I "snooze" the action which is inside onReceive() in my BroadcastReceiver? Do I make a new Receiver which fires with this (when phon haz internietz) or???
private boolean isNetworkAvailable() {
ConnectivityManager cm
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ai= cm.getActiveNetworkInfo();
return ai!= null;
}
EDIT:
Got it to work. Used a IntentService which checks every 10 minutes if internet is avaliable, and if: send mail. The IntentService is started from my BroadcastReceiver, where the email initially is to be sent, if there's no internet.
Just Use Background Service Which Checks The Internet Connection.
if Internet available it just send the Email unless start your service which checks the internet connection in every minute until the email send and after that close your service.
Related
I am developing the android app which runs my web service at app startup. My app is basically the home app of the device. So that's mean that When user will reboot the device, My app will come in front of him.
So far that is great. But I have noticed it that on app start the device takes 5 to 10 seconds to get the internet connected. whereas my web service gets run before the internet is established thus throwing an error of "An error occurred please try again later."
What I Want: Though I am showing that message I am really not satisfied as I really do want to show the user a Message. That message should be "Device is connecting to the internet"
but unfortunately, I am unable to do so. I am using the following code
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager
= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
}
but it really indicates me if the net is connected or not.
So is not there the case where I can show a message like "Please wait device is connecting to the internet"
Or how can I get that device is restarted not the only app. ... Please help
Register a local (or Manifest-declared if you want) broadcast receiver in your activity (documentation here https://developer.android.com/guide/components/broadcasts), listening to connectivity changes, using this intent filter: IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION).
Then check if is the device has internet connection with the code you posted.
Remember to register your broadcast receiver inside onResume() method and to unregister it inside onPause() method
From api 24 and above:
you can use registerDefaultNetworkCallback https://developer.android.com/reference/android/net/ConnectivityManager.html#registerDefaultNetworkCallback(android.net.ConnectivityManager.NetworkCallback)
The callback has a public method called "onAvailable" https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback.html#onAvailable(android.net.Network) that is called when a new network is ready to be used.
I want to develop an application which will run in the background when the wifi is turned on. The background app will check whether the device is connected to wifi network for every 15 minutes. If it is not connected then turn off the wifi in the device. Now how do I start the application in the background when wifi is turned on?
You need to create service which runs in background and you can write logic there where it checks whether wifi is available or not after every 15 mins.
If it detect wifi then run your background operation in the same service.
you can refer this
http://stacktips.com/tutorials/android/android-service-example
Also you can detect the broadcast while system detects wifi.
public class WifiReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager conMan = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMan.getActiveNetworkInfo();
if (netInfo != null && netInfo.getType() ==
ConnectivityManager.TYPE_WIFI)
Log.d("WifiReceiver", "Have Wifi Connection");
else
Log.d("WifiReceiver", "Don't have Wifi Connection");
}
};
You must use a Broadcast receiver.
The event which starts the Broadcast receiver is the change of the connectivity.
To let it starts in background you can start a service when the Broadcast Receiver reveals this change of connectivity
You need to use BroadcastReceiver to get wifi event.
Releated answer explains how to do it pretty well.
I'm am developing an Android app that uses Volley to send some data entered by the user to my server. I am also saving this data in a local SQLite database. All this works just fine. The problem occours if the internet connection is lost. How do I know when I must try to re-upload the data.
If the network connection is lost I can use a BroadcastReceiver to find out when the connection is established. However how do I ensure this works even when the device is connected to the network but there is no access to the internet.
Try this way
1.Check your Internet Connection before you want to upload data to some View (Ex.ListView)
If Available, upload server data.
else
upload local/saved data.
This may helps you
Create a broadcast receiver to listen to the internet changes and register it:
class NetworkStateBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo.isConnected()){
//netwok just connected, upload
}
}
Create an instance and register it.(may be in application class, which runs throughout your app)
reciever = new NetworkStateBroadcastReceiver();
registerReceiver(mReciever, new intentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
I'm making a app that is displaying the available wireless networks and a user can choose one network and connect to it.
I have a broadcast receiver and I receive the status of the connection :connected , when the connection is done , but I would like also to display more info to the user like , Authenticating, Obtaining IP address , cause now I only know when network is connected but noting till then.
Any idea how to do this?
Thanks you very much
Fine-grained connection state is available from the NetworkInfo object - getDetailedState
NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
DetailedState state = ni.getDetailedState();
You also need to register your broadcast receiver to receive state change events from WifiManager
IntentFilter filter =
new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
filter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
I'm working on an app that logins on a public wifi network. I've got the login working by doing a http post to the authentication server.
My next step is making the app do the http post automatically when there's a new wifi connection, checking that we are connected to the correct ssid.
My question is, which is the best way of doing this? Is the best way to create a service and registering to a broadcastreceiver?
Thanks for your help
The way I do it is that i have a broadcastreceiver that listens to android.net.conn.CONNECTIVITY_CHANGE intents. In that broadcast receiver you can check what kind of connection is made by polling the ConnectivityManager:
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean hasWifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI) != null && cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isAvailable() && cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
And then start the service doing the post to the auth server.
Alternatively you could indeed let the service listen to android.net.conn.CONNECTIVITY_CHANGE intents and do the wifi check there.