今天包含两部分内容1:preference置灰,(比如手机开启飞行模式,相应的设置项需置灰)2:preference与activity,service,broadcast的通信
1
PreferenceScreen设置Preference置灰显示,在xml文件中必须设置以下两个属性:
android:enabled 是否起作用,false则能点取,但是不作用(有焦点)
android:shouldDisableView 当enabled设置为false,此属性设置为true时,preference所代表的item变暗
boolean hascard = bundle.getBoolean("hascard");
mPhoneId = bundle.getInt(IccLockSettings.PHONE_ID, 0);
Log.i("beijing", "StatusSim-mPhoneId---"+mPhoneId);
mPhone = PhoneFactorySprd.getPhone(mPhoneId);
int count = getPreferenceScreen().getPreferenceCount();
for (int i = 0; i < count; i++) {
//PreferenceScreen设置Preference全部置灰
getPreferenceScreen().getPreference(i).setEnabled(false);
getPreferenceScreen().getPreference(i).setShouldDisableView(true);
}
2
我们知道系统Preference提供的控件并不多,只有CheckBoxPreference,ListPreference,EditTextPreference,RingtonePreference等为数不多的控件,有时候并不能满足我们的需求.这时候我们就需要自定义,比如自定义一个SeekBar用来调节音量什么的.这里我就不介绍如何自定义包含SeekBar的Preference,大家可以查看Android自定义SeekBarPreference.
public class VolumeSeekBarPreferences extends DialogPreference implements OnSeekBarChangeListener { private seekBarCallBack back; .... @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub .... back.setSeekBarVolume(mCurrentValue); .... } .... public interface seekBarCallBack{ public void setSeekBarVolume(int volume); } public void setSeekbarListener(seekBarCallBack barCallBack){ back = barCallBack; } }
public class MyPreferenceFragment extends PreferenceFragment implements OnPreferenceClickListener, OnPreferenceChangeListener { private VolumeSeekBarPreferences seekBarPreferences; ..... @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); addPreferencesFromResource(R.layout.preference_setting); //VolumeSeekBarPreferences为我们自定义的Preference控件 seekBarPreferences = (VolumeSeekBarPreferences) findPreference("seekBarPreference"); seekBarPreferences.setSeekbarListener(new seekBarCallBack() { @Override public void setSeekBarVolume(int volume) { // TODO Auto-generated method stub //在这里实现通信 } }); } ..... }
public class MyPreferenceFragment extends PreferenceFragment implements OnPreferenceClickListener, OnPreferenceChangeListener{ private Activity context; ..... @Override public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); context = activity; try { callbackSetting = (CallbackSetting) context; } catch (Exception e) { // TODO: handle exception } } ..... /* * 利用回调函数,让IntentActivity调用Service的方法来实现Fragment间接调用Service的方法 */ public interface CallbackSetting { public int getVolume(); public void setVolume(int volume); public void setRemoteModel(boolean flag); public void readModelInfo(); } ...... @Override public boolean onPreferenceChange(Preference preference, Object newValue) { // TODO Auto-generated method stub if(preference == checkBoxRemote){ if((Boolean)newValue){ callbackSetting.setRemoteModel(true); }else { callbackSetting.setRemoteModel(false); } } return true; } ...... }
public class IntentActivity extends Activity implements ServiceConnection, CallbackSetting{ ...... @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.empty_layout); Intent settingIntent = new Intent("com.example.RadioService"); this.bindService(settingIntent, this, BIND_AUTO_CREATE); } ....... @Override public void setRemoteModel(boolean flag) { // TODO Auto-generated method stub if(flag){ radioService.setRemote(1); }else { radioService.setRemote(0); } } ...... }因为Activity可以通过bindService绑定Service并调用Service中的方法.所以这里,Activity只是通过实现Fragment的接口来充当中介.
...... @Override public void onAttach(Activity activity) { /** 注册广播 */ receiveBroadCast = new ReceiveBroadCast(); IntentFilter filter = new IntentFilter(); filter.addAction("com.gasFragment"); //只有持有相同的action的接受者才能接收此广播 activity.registerReceiver(receiveBroadCast, filter); super.onAttach(activity); } ...... class ReceiveBroadCast extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //得到广播中得到的数据,并显示出来 String gasname = intent.getExtras().getString("gasName"); String address = intent.getExtras().getString("address"); gasadderss.setText("地址:\n "+address); gasName.setText(gasname); } } /** *注销广播 * */ @Override public void onDestroyView() { getActivity().unregisterReceiver(receiveBroadCast); super.onDestroyView(); } ......
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); mRemote = prefs.getBoolean("checkbox_remote_preference", true);//两个参数,一个是Preference控件的android:key属性, //另一个是取不到值时的默认值