How do I send data only by WiFi - android

In my downloader I have to let the user choose whether he wants to download by WiFi or by using network connectivity. I know I can do this via:
final ConnectivityManager connMgr = (ConnectivityManager)
this.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
and then use the isConnected() method to see if I have WiFi connectivity. I also have another class named Sharable, not an Activity, which is supposed to hold all the data that is to be shared across Activities. These 'sharable' things include connections to database and so on.
How can I get this WiFi information in this Sharable class ?
Can this not be achieved without it extending Activity ?
If not, where am I supposed to instantiate the various variables ? In the onCreate?

Just pass Application context to Sharable.

Related

Which API decides if the call is going through VoLTE or VoWiFi in Android?

I want to write an xposed module where I can redirect VoLTE calls through VoWiFi. I want to know which method decides if the call is going through VoLTE or VoWifi and I would hook that method and get the work done.
Basically using WiFi I want to give the network an illusion that the device is using mobile data and send calls through wifi
Note : I am new to android programming. Excuse me if my question looks vague.
So you want to give illusion that you are connected to WIFI even though you are on mobile data.
the way we check that is this:
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
So what you can do is, hook getType method of ConnectivityManager and in afterhook method always return ConnectivityManager.TYPE_WIFI
the way to do that is by calling
param.setResult(ConnectivityManager.TYPE_WIFI);//may be you need casting here
in afterHook.
read more about connectivity here:
https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html
Edit: I hope you are familiar with afterHook and beforeHook methods and how xposed works.

Android is device connected to the internet method

I want to create a method which tells if the device is online. As far as I understand ConnectivityManager tells only if the device is connected to a network. This doesn't mean that the device is connected to the internet. To ensure that the device is online I'm using InetAddress.getByName("google.com").isReachable(3); but I can't use it on the main thread. I can create a separate thread to check the connectivity and then use a callback function but is there another way? I don't want my app to do anything before it is connected. Do you have any solutions? Thank you!
With any networking, there isn't a guaranteed way to check whether or not you are connected to an endpoint without actually sending data. Even if the device is connected to a network, has an ip address, recently received data, e.t.c, it doesn't mean that you still have a connection.
I would suggest allowing the user to progress into your application as much as possible, queuing up the requests to your server in the background whilst a connection is established. Use the same framework to resend data if the connection is lost whilst the user is using the app. Make the connection to the server as transparent to the user as possible, unless it fails to connect after ~1 minute
try this:
public static boolean isOnline(Context context) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
return true;
}
return false;
}

Android: different permission for two activites

I know the basic rules of the manifest, but it works on all activites.
I have two activites in my apliccation.
I want that one have to internet access, and another not.
How can i do that?
And hardest:
I want that one have to 3G internet access, and another can use only wi-fi.
How can i do that?
Use this to determine if WiFi is connected and act properly
final ConnectivityManager conectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo mWifi = conectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
return mWifi.isConnected();
Basicly, you can't give permission to activities separatly as it's not you who are giving the permission, but the user. Thus, user give permission to the whole app and doesn't care about activities.
You must add all permisions to your manifest. Persmissions are global. That means that you are giving permission for your application, not for every single activity. If you'll add wifi and 3g permission you can use this connection in your activities.
You can define custom permissions in android for each different activity
You can refer these
How to use custom permissions in Android?
http://developer.android.com/guide/topics/security/permissions.html

Reading WiFi properties from APs without getting connected

I would like to know if there is any way in Android to read some configurable property from a WiFi access points without getting connected/authenticated to the network. Basically I would like to list only the networks that implements/advertises a specific web service I am working on.
Thanks,
- Rafael
You can retrieve WifiManager instance:
WifiManager wifiManager = ( WifiManager ) mContext.getSystemService ( mContext.WIFI_SERVICE ) ;
You can also get NetworkInfo object:
ConnectivityManager connManager = ( ConnectivityManager ) context
.getSystemService ( Context.CONNECTIVITY_SERVICE ) ;
NetworkInfo mWifi = connManager.getNetworkInfo ( ConnectivityManager.TYPE_WIFI ) ;
You can use the WifiManager.
This class provides the primary API for managing all aspects of Wi-Fi connectivity. Get an instance of this class by calling Context.getSystemService(Context.WIFI_SERVICE). It deals with several categories of items:
The list of configured networks. The list can be viewed and updated, and attributes of individual entries can be modified.
The currently active Wi-Fi network, if any. Connectivity can be established or torn down, and dynamic information about the state of the network can be queried.
Results of access point scans, containing enough information to make decisions about what access point to connect to.
It defines the names of various Intent actions that are broadcast upon any sort of change in Wi-Fi state.
This is the API to use when performing Wi-Fi specific operations. To perform operations that pertain to network connectivity at an abstract level, use ConnectivityManager.
This is the link:
http://developer.android.com/reference/android/net/wifi/WifiManager.html

Android terminate wifi ability within application

One of the major requirments of my application is using cellular internet connection only.
In case of detecting wifi connection, the application have to disable the wifi ability and connecting through the cellualr internet connection.
How it can be done?
Thanks,
Eyal.
Checking WIFI connection
You can use the following code to check if the active network (if any - do some null checks here) is of type WIFI.
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connManager.getActiveNetworkInfo();
if (netInfo!=null) {
// detect type using netInfo.getType()
}
There's a TYPE_WIFI int constant in ConnectivityManager that you can use for your check. (getType returns an int to identify the network type).
More info on the Activity Manager can be found on the Android dev site.
Disabling WIFI
n order to disable the WiFi state, you have to grant the following permission in the application manifest
android.permission.CHANGE_WIFI_STATE
You can then use the WifiManager to set enable/disable the WIFI.
WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
More info on http://developer.android.com/reference/android/net/wifi/WifiManager.html
For a complete sample, check the following post : http://android-er.blogspot.com/2011/01/turn-wifi-onoff-using.html

Categories

Resources