Android: different permission for two activites - android

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

Related

Upload Firestore Cache Write Data Upon Getting Internet

My app allows the user make firestore writes when the user is offline. I have noticed that cache data is only written to firestone when the user makes another write (so it takes all the initial writes when user was offline, and then adds the new write data and send to firestore).
I am looking for a way to send the cached data to firestore without the user having to make another write entry. Please how can I achieve this functionality? I'd greatly appreciate any help you can provide
You can get around this by detecting when the user gets an internet connection again, I suggest prompting the user before doing so. But when this event is done. I suggest writing to a dedicated location with a "last updated' field such as a dedicated collection for all users where you can track the last update timestamp, it'll incur a write but this way you have control over it.
It will require permission to check from the device such as a simple query to a URL, or ideally with the getActiveNetworkInfo() method from the ConnectivityManager returns a NetworkInfo instance representing the first connected network interface it can find or null if none of the interfaces are connected.
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
You will also need:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
in your android manifest.
Note that having an active network interface doesn't guarantee that a particular networked service is available.

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 - force network requests go through wifi instead of mobile network

I have an app which connects to a hardware device Wi-Fi hotspot.
It seems that Android forward requests over other networks (3G/4G for example) instead the hotspot, since my hot spot has no internet connection.
Is there any way to force network stream to work on the wifi?
I've come across the following function, but it's deprecated:
https://developer.android.com/reference/android/net/ConnectivityManager.html#setNetworkPreference(int)
Per the Connecting your App to a Wi-Fi Device blog post:
To direct all the network requests from your app to an external Wi-Fi device, call ConnectivityManager#setProcessDefaultNetwork on Lollipop devices, and on Marshmallow call ConnectivityManager#bindProcessToNetwork instead, which is a direct API replacement.
Use the ConnectivityManager to get the state of the Wifi adapter and then you can check if it is connected or even available using NetworkInfo.
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifi.isConnected()) {
// Continue with logic
}
In case he is not connected to wifi then don't proceed else continue you flow.
Add the following permission in you Manifest file
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

How do I send data only by WiFi

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.

Restricting Android syncing to WIFi

I am new to Android and am trying to write a SyncAdapter - is it possible to restrict the SyncAdapter to only perform a sync when the network is WiFi (and not 3G etc.)?
you can make check for wifi as
ConnectivityManager cm = (ConnectivityManager) YourActivity.this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if(info.getType()==ConnectivityManager.TYPE_WIFI)
{
System.out.println("WiFi Connected");
}
else
{
System.out.println("WiFi not connected");
}
and also add permission in manifest as
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
The answer above is good, but I would add a HttpHead-Request (HTTPGet is also possible but you receive more data than needed) that is calling e.g. google.com so you can be sure that you are really connected to the internet.
The above posted solution just checks if you have network access but does not guarantee that you have inernet access.

Categories

Resources