Android:启用/禁用自动同步和后台数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/767771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: Enable/Disable Auto Sync and Background Data
提问by ?mer
I want to develop an application that disables the Background Data (new feature in Android 1.5) and Auto Sync and then enables GPRS/EDGE connection and vice versa.
我想开发一个应用程序,禁用后台数据(Android 1.5 中的新功能)和自动同步,然后启用 GPRS/EDGE 连接,反之亦然。
I figured out how to enable/disable GPRS/EDGE by changing the APN settings. (weird solution. However; Android developers couldn't think a user may want to disable GPRS/EDGE) But, I couldn't find a way to enable/disable Auto Sync and Background data.
我想出了如何通过更改 APN 设置来启用/禁用 GPRS/EDGE。(奇怪的解决方案。然而;Android 开发人员无法认为用户可能想要禁用 GPRS/EDGE)但是,我找不到启用/禁用自动同步和后台数据的方法。
I investigated the Android code and as I understood, the Sync operation is an intent. So, I wanted to reach with putExtra to the intent and trigger the enabling/disabling. But; I couldn't find the correct keyword. Or maybe I was totally wrong.
我调查了 Android 代码,据我所知,同步操作是一个意图。所以,我想通过 putExtra 达到意图并触发启用/禁用。但; 我找不到正确的关键字。或者也许我完全错了。
What is the right way to solve this?
解决这个问题的正确方法是什么?
回答by e-satis
In my HTC dreams, there is a checkbox to disable the auto sync. I can look for the menu arborescence if you wish so you can find what the callback function is in the Android source code. But I am pretty sure auto sync cannot be completely disabled. Unchecking auto sync will prevent sync from being performed on a timed basis, but it will occur everytime you run an app with sync capabilities if any network data connection is available.
在我的 HTC 梦中,有一个禁用自动同步的复选框。如果您愿意,我可以查找菜单树状结构,以便您可以在 Android 源代码中找到回调函数。但我很确定不能完全禁用自动同步。取消选中自动同步将阻止按时间执行同步,但如果有任何网络数据连接可用,则每次运行具有同步功能的应用程序时都会发生这种情况。
Good luck anyway.
总之祝你好运。
EDIT :
编辑 :
There are two ways to get the info you desire.
有两种方法可以获取您想要的信息。
First, I think you can use the code in android-sources/packages/apps/Settings/src/com/android/settings/Utils.java to create an activity that will enlist all the keys of the intent then find the one you want.
首先,我认为您可以使用 android-sources/packages/apps/Settings/src/com/android/settings/Utils.java 中的代码来创建一个活动,该活动将征集意图的所有键,然后找到您想要的.
The other way is to write a nice mail to the guy who made the Toggle Setting app (http://smartphoneandroid.com/2008/12/28/toggle-setting-perfect-app-for-android-phone.html) since he obviously found a solution to your problem. His email address is written in the app sheet on the android market. I won't write it here, but if you do not have access to real android phone, I can mail it to you on your mail address.
另一种方法是给制作切换设置应用程序的人写一封漂亮的邮件(http://smartphoneandroid.com/2008/12/28/toggle-setting-perfect-app-for-android-phone.html),因为他显然找到了解决您问题的方法。他的电子邮件地址写在安卓市场的应用程序表中。我不会写在这里,但是如果您无法使用真正的Android手机,我可以通过您的邮件地址邮寄给您。
回答by user3068883
To Disable the AutoSynch
禁用自动同步
ContentResolver.setMasterSyncAutomatically(false);
To Enable the AutoSynch
启用自动同步
ContentResolver.setMasterSyncAutomatically(true);
Permission you require is
您需要的权限是
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
回答by user3068883
Background data is a secure setting, so cannot be changed by user applications. But bear in mind, it's just a setting - it's not enforced. Apps are meant to read it and respect it but I bet some don't.
后台数据是安全设置,因此用户应用程序无法更改。但请记住,这只是一个设置 - 它没有强制执行。应用程序旨在阅读并尊重它,但我敢打赌有些不会。
回答by kingston
setMasterSyncAutomatically() on ContentResolver should do it. Check: general-sync-settings-auto-sync-checkbox-programtically
ContentResolver 上的 setMasterSyncAutomatically() 应该这样做。检查:general-sync-settings-auto-sync-checkbox-programtically