How can set "Ask to use WLAN" off programmatically in Android? - android

I will get " An app requesting permission to use WLAN. Allow?" prompt window in real mobile phone Samsung SM-J5008 with Android 5.1 when I try to change status of WiFi.
I have google some information ,such as https://groups.google.com/d/msg/tasker/C5ZgPA2J7aM/bH7j85buAAAJ
A dialog box "Ask to use WLAN" will be displayed, and require me to choice whether to display the dialog box when I try to change the status of WiFi.
I hope to set "Ask to use WLAN" dialog box off programmatically , so the dialog will not be displayed when I change WiFi status, how can I do? Thanks!
Image
Added Content
I set the status of WiFi using the following code.
fun setWiFi(aWiFiDef: WiFiDef){
val wifiManager =mContext.applicationContext.getSystemService(WIFI_SERVICE) as WifiManager
wifiManager.isWifiEnabled=aWiFiDef.status
}

Use the below code to switch WiFi ON or OFF. Make sure you have added the <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> PERMISSION and also note that you might need to replace "this" by your activity's current context.
WifiManager wifiManager = (WifiManager) this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if(wifiManager.isWifiEnabled()){ // Is ON , switch it OFF
wifiManager.setWifiEnabled(false);
}else{ // Is OFF, switch it ON
wifiManager.setWifiEnabled(true);
}
Using this code i am able to switch the WiFi adapter ON and OFF with no additional dialog prompt.
Here is the documentation taken from developer.android.com :
Make sure that you only try to change the WiFi status using the above code and that there isn't any piece of code elsewhere in your activity trying to accomplish the same thing. There might be a listener in your code that detects your intent to change the WiFi status (switch it ON or OFF) and displays the dialog you've shown us.Find it and remove it.
If after those changes the dialog is still being displayed, then as #VicJordan said, it might be an OEM implementation issue in which case i am sorry but i cannot help you.
Best of luck!

I think you can do it by adding CHANGE_WIFI_STATE permission to your manifest file.
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
CHANGE_WIFI_STATE is a normal permission so maybe that "Ask to use WLAN" popup can be skipped. I am saying maybe because it depends upon OEM's customization, how they are handling this "Ask to use WLAN" popup.
Hope this helps.

Related

Screen Overlay Detected | (Android 6.x) Lenovo, Redmi & Samsung Devices

I have created an app which continuously run in background and show an floating overlay TextView(By using the permission android.permission.SYSTEM_ALERT_WINDOW).
I have problem in Lenovo android devices, when other applications try to request for user permission there is an alert dialog says "Screen Overlay Detected". While I have test the same application on Redmi 3S Prime device the "Allow" button in permission dialog is not clickable, until I have turned off the Floating Overlay TextView in my application.
So is there any solution to resolve this device specific issue? One of the possible solution may be disable floating TextView while permission dialog is visible to user and show floating overlay when permission dialog is closed, but the problem is how can I detect the permission dialog open/close state for other foreground application from my app.
Please suggest...
Unfortunately it seems that this is inherent to the Android OS, and as far as I know there is no way to detect when another app is requesting permissions. The general flow for interacting with other apps is to send intents to them in a push manner, so theoretically it could be done if the other apps send an intent to your app to disable the overlay, though this is not a practical solution.
I could be completely wrong, but I am yet to see a programmatic solution to this problem. The best you can probably do is warn your users that your app may cause this problem, and provide them with a quick temporary disable button.
how can I detect the permission dialog open/close state from my app??
Implement Method Mentioned Below, to run this check at the onCreate of the first Activity
public final static int PERM_REQUEST_CODE_DRAW_OVERLAYS = 1234;
/**
* Permission to draw Overlays/On Other Apps, related to
'android.permission.SYSTEM_ALERT_WINDOW'
in Manifest
Resolves issue of popup in Android M and above "Screen overlay detected- To change this permission setting you first have to turn off the screen overlay from Settings > Apps"
If app has not been granted permission to draw on the screen, create an Intent &
set its destination to
Settings.ACTION_MANAGE_OVERLAY_PERMISSION
&
* add a URI in the form of
"package:"
to send users directly to your app's page.
Note: Alternative Ignore URI to send user to the full list of apps.
public void permissionToDrawOverlays() {
if (android.os.Build.VERSION.SDK_INT >= 23) { //Android M Or Over
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, PERM_REQUEST_CODE_DRAW_OVERLAYS);
}
}
}
Called on the activity, to check on the results returned of the user action within the settings
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PERM_REQUEST_CODE_DRAW_OVERLAYS) {
if (android.os.Build.VERSION.SDK_INT >= 23) { //Android M Or Over
if (!Settings.canDrawOverlays(this)) {
// ADD UI FOR USER TO KNOW THAT UI for SYSTEM_ALERT_WINDOW permission was not granted earlier...
}
}
}
}
NOTE:Above code and extract is taken from following gist.
Hope this Helps!!!..
Just clear data of the Es explorer from device then after restart device.
I think these two links could help you.
firstlink
secondlink
Try to use for debug build -- targetSdkVersion 22.
After signed APK (for all targetSdkVersion(s)) application will work fine.

Turn off Restrict background data programmatically

I have a HTC one M8 running Android 5. I have gone into settings -> more -> data usage -> restrict background data (checked)
This should turn background data off.
I would like to turn it back on (uncheck the checkbox) programmatically with the following code in onCreate of one of my Activities.
ContentResolver.setMasterSyncAutomatically(true);
.
Unfortunately it doesn't turn back on. I have the following in the manifest
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
Are there any ideas why this isn't working?
thanks in advance

How to enable Window Animations Dynamically in android

I want to enable animations in my app, I am doing this but to no effect.
Settings.System.putInt(getContentResolver(), Settings.System.WINDOW_ANIMATION_SCALE, 1);
Settings.System.putInt(getContentResolver(), Settings.System.TRANSITION_ANIMATION_SCALE, 1);
I have given permssion in manifest too,
uses-permission android:name="android.permission.WRITE_SETTINGS"
Please advise,
It may be that the settings won't work until you finish and then re-create the activity. I know this is the case for changing localisation settings and may be the same for the settings you are trying to set?
Also, just for debugging purposes, you could call Settings.System.getInt after putInt to check the system is actually storing the correct settings value.

Android Device Admin dialogue not showing up - Auto declined

I'm trying to implement locking the screen with my app.
I've worked my way through google documentation and studied the sample.
When i call this
void getAdmin(){
Intent activateDeviceAdminIntent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
activateDeviceAdminIntent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mPolicy.getPolicyAdmin());
activateDeviceAdminIntent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
getResources().getString(R.string.AdminReceiverDescr));
startActivityForResult(activateDeviceAdminIntent, REQ_ACTIVATE_DEVICE_ADMIN);
}
The dialogue doesn't show up (maybe it flashes a little, can't say for sure), it just jumps in my onResult routine and result is "not granted". But my app afterwards shows up in the list in settings/security/device admins and if i enable admin rights manually it locks my screen like a charm.
I've added the permission
uses-permission android:name="android.permission.BIND_DEVICE_ADMIN"
as well as
receiver ... android:permission="android.permission.BIND_DEVICE_ADMIN"
to my manifest.
Does anyone have a clue what i may have missed?
Thx in advance!
Found the problem in my manifest: you have to specify the receiver subclass with a $ sign.
Example:
android:name="com.exampl.PolicyClass$PolicyReceiver"

Set Audible selection option programmatically in Android

I am creating an application in Android for which I need to be able to enable or disable programmatically, the audible selection option that we see in the Sound and DIsplay settings.
I am not able to find the API for this. Please help me with it.
from https://github.com/CyanogenMod/android_packages_apps_Settings/blob/ics/src/com/android/settings/SoundSettings.java
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.loadSoundEffects();
Settings.System.putInt(getContentResolver(), Settings.System.SOUND_EFFECTS_ENABLED,1);

Categories

Resources