Turn off Restrict background data programmatically - android

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

Related

How can set "Ask to use WLAN" off programmatically in 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.

Badge on App icon

I need to show badge on App icon in my application
what i have tried:
This https://github.com/leolin310148/ShortcutBadger library do it on many devices.I have checked its manifest and saw that there exists a permission for each type of launcher
<!--for Samsung-->
<uses-permission android:name="com.sec.android.provider.badge.permission.READ"/>
<uses-permission android:name="com.sec.android.provider.badge.permission.WRITE"/>
<!--for htc-->
<uses-permission android:name="com.htc.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.htc.launcher.permission.UPDATE_SHORTCUT"/>
<!--for sony-->
<uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE"/>
<uses-permission android:name="com.sonymobile.home.permission.PROVIDER_INSERT_BADGE"/>
<!--for apex-->
<uses-permission android:name="com.anddoes.launcher.permission.UPDATE_COUNT"/>
<!--for solid-->
<uses-permission android:name="com.majeur.launcher.permission.UPDATE_BADGE"/>
<!--for huawei-->
<uses-permission android:name="com.huawei.android.launcher.permission.CHANGE_BADGE" />
<uses-permission android:name="com.huawei.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="com.huawei.android.launcher.permission.WRITE_SETTINGS" />
<!--for ZUK-->
<uses-permission android:name="android.permission.READ_APP_BADGE"/>
2.I have gone through many available answers that are already on stackoverflow like
How to display count of notifications in app launcher icon
can we show badge number on android app icon like iphone?
How to show badge count with app icon on Redmi?
Add unread notification badge on android app icon
Is there a way to add a badge to an application icon in Android?
All these resources helped me to attain the task on above mentioned devices but i can still see there are lot man devices like xiomi,micromax etc in which app like wtsapp,facebook etc are showing badges but i am unable to show badge on these devices.
Have anyone here has achieved it.Any help will be appreciated.
This is not a feature the of Android system. Device manufacturers (like Xiaomi you mentioned) or launcher developers (like Nova Launcher) can implement it and expose an api for app developers, but there is no standard for that, nor is it expected by the users to be there.
In general, you should not try to do this on Android.
For displaying badge, Google may have stopped showing numbers count on App Icon as a badge but I think it's still showing a dot to let the user know there is something new to that app.
For more information you can go through this official link from Google : https://developer.android.com/training/notify-user/badges
You can! From the doc: https://developer.android.com/training/notify-user/badges
By default, each notification increments a number displayed, but you can override this number for your app. For example, this might be useful if you're using just one notification to represent multiple new messages but you want the count here to represent the number of total new messages.
To set a custom number, call setNumber() on the notification, as shown here:
var notification = NotificationCompat.Builder(this#MainActivity, CHANNEL_ID)
.setContentTitle("New Messages")
.setContentText("You've received 3 new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
.setNumber(messageCount)
.build()

Start an activity even if we lock the device

I'm developing an alarm aplication, so when the alarm goes on it start an activity with an off button
It's very simple, but my question, is there a way to start the activity even if the device is locked???
You need to use the WakeLock and also disable your keyboard for the respective Activity. They can be achieved by help of WindowManager.LayoutParams class.
WakeLock allows your app to wake up the screen i.e. turn the display on while your Activity or your complete app is running in the foreground. To learn more about implementing the WakeLock go through the following link:
http://developer.android.com/training/scheduling/wakelock.html
To disable your keyguard you need to use WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED and/or WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD flags for the respective Activity.
To know more about them, go to the following links:
http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_DISMISS_KEYGUARD
http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_SHOW_WHEN_LOCKED
You will also need to use the following permissions in the Manifest file of your Android Project:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
You need the following permission in AndroidManifest.xml file:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
Check this link

Air for Android app is keeping screen awake when not in app

I have an air for android app that has event listeners for ACTIVATE and DEACTIVATE, inside the activate I tell the screen to stay awake and in the deactivate I tell it to go back to normal like so :
stage.addEventListener(Event.DEACTIVATE, deactivateHandler);
stage.addEventListener(Event.ACTIVATE, activateHandler);
protected function deactivateHandler(event:Event):void{
SFX.disableSound();
NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.NORMAL;
}
protected function activateHandler(event:Event):void{
NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;
}
But the screen will stay awake at all times even when on the android home screen unless you force close the app... any ideas?
Thanks
I had this exact problem. My app was able to keep the screen on by setting systemIdleMode to SystemIdleMode.KEEP_AWAKE, and it would force the screen to stay on. However, when the app tried to set the systemIdleMode back to SystemIdleMode.NORMAL, so the screen could turn off, the screen was still staying on.
What turned out to be the problem in my case is was a missing android permission. I had already added this permission to my app XML file, so that i could use the keep-alive function:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
Turns out this is not the only permission you need. I added this permission also:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
And suddenly my app was able to let the screen be turned off again.
You could try using this in your java code:
// Gets one of the views visible on the screen and sets keepScreenOn to true.
// This means the screen will stay on as long as the specified view is visible.
this.findViewById(R.id.viewId).setKeepScreenOn(true);
Or you could put android:keepScreenOn="true" in your layout.

Android: How to fade out search / home / menu / back buttons?

I'm using my Android (Archos 43) only as an extended display in an industrial application. I just need a single program to display data received, and send user inputs by bluetooth. This little program should start directly after booting and should disable (fade out) the android-buttons (search, home, menu and back).
Here's my problem:
I know, there are some applications which can fade out these search/home/menu/back-buttons (like DeskClock or some videoplayers), but how does it work? Just using:
android:theme="#android:style/Theme.Light.NoTitleBar.Fullscreen"
only disables the titlebar, not the 4 android-buttons.
It's not an android-problem, this is a special feature from my Archos 43 internet tablet.
androidManifest.xml:
<uses-permission android:name="archos.permission.FULLSCREEN.FULL" />
<uses-permission android:name="archos.permission.FULLSCREEN.FULL" />

Categories

Resources