禁用或启用移动数据使用复选框preference? [英] Disable or enable Mobile Data with CheckBoxPreference?
问题描述
我有一个preferenceActivity,其中有一个复选框preference
。我想点击复选框preference启用或禁用移动数据,所以我写了这篇code。
I have a PreferenceActivity in which there is a CheckboxPreference
. I would like clicking on CheckboxPreference enable or disable Mobile Data, so I wrote this code.
//Outside of Methods
void turnData(boolean ON) throws Exception
{
final ConnectivityManager conman = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(conman.getClass().getName());
final java.lang.reflect.Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(conman);
final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, ON);
}
//In the onCreate
final boolean o = false;
boolean mobileDataEnabled = false;
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
try {
Class cmClass = Class.forName(cm.getClass().getName());
Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
method.setAccessible(true);
mobileDataEnabled = (Boolean)method.invoke(cm);
} catch (Exception e) {
}
if(mobileDataEnabled) {
((CheckBoxPreference)Data).setChecked(true);
//The Mobile Data are enabled
Data.setSummary("Mobile Data enabled");
dati = true;
}
else {
//The Mobile Data are disabled
((CheckBoxPreference)Data).setChecked(false);
try {
turnData(mobileDataEnabled);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Data.setSummary("Mobile Data disabled");
dati = false;
}
Data.setOnPreferenceClickListener( new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
if (dati==true) {
Data.setSummary("Mobile Data enabled");
dati = false;
}
else if (dati==false){
try {
//The problem is here
//What should I put in the parentheses?
turnData(o);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Data.setSummary("Mobile Data disabled");
dati = true;
}
return false;
}
});
这个问题,因为在code的评论指出,与调用方法时,放在括号中的内容。我很抱歉,如果这个问题似乎微不足道**,但请大家多多包涵。我是初学者。
The problem, as noted in the comments in the code, is what to put in the parentheses when calling the method. I apologize if the question may seem trivial **but please bear with me. I'm beginner.
推荐答案
如果我理解正确的,你不能调用code,因为,正如你所说的,
If I understood correctly, you can't call the code because, as you said,
当我点击复选框preference,什么也没发生。
"when i Click on the CheckBoxPreference, nothing happens."
您是否尝试过的复选框绑定到preference.On preferenceChangeListener?默认的preference活动模板展示了如何做到这一点。检查了这一点:<一href="http://stackoverflow.com/questions/13596250/how-to-listen-for-$p$pference-changes-within-a-$p$pferencefragment?answertab=votes#tab-top">http://stackoverflow.com/questions/13596250/how-to-listen-for-$p$pference-changes-within-a-$p$pferencefragment
Did you try binding the checkbox to the Preference.OnPreferenceChangeListener? The default "Preference Activity template" shows how to do this. Check this out: http://stackoverflow.com/questions/13596250/how-to-listen-for-preference-changes-within-a-preferencefragment
private OnPreferenceChangeListener onPrefChangeListener = new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// TODO Auto-generated method stub
return false;
}
};
这篇关于禁用或启用移动数据使用复选框preference?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!