Back to project page MovisensGattSensorExample.
The source code is released under:
GNU General Public License
If you think the Android project MovisensGattSensorExample listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.movisens.gattsensorexample.activities; /* w ww . j ava2 s. c o m*/ import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.os.Bundle; import android.preference.EditTextPreference; import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceCategory; import android.preference.PreferenceFragment; import android.preference.PreferenceScreen; import android.view.MenuItem; import com.movisens.gattsensorexample.R; import com.movisens.gattsensorexample.application.App; public class Preferences extends Activity { public static final String SAMPLING_RUNNING = "sampling_running"; public static final String SENSOR_ADDRESS = "sensor_address"; public static final String SENSOR_NAME = "sensor_name"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getActionBar().setDisplayHomeAsUpEnabled(true); // Display the fragment as the main content. getFragmentManager().beginTransaction() .replace(android.R.id.content, new PrefsFragment()).commit(); } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { switch (item.getItemId()) { case android.R.id.home: startActivity(new Intent(this, StartActivity.class)); finish(); return true; default: return super.onOptionsItemSelected(item); } } public static class PrefsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Load the preferences from an XML resource addPreferencesFromResource(R.xml.preferences); for (int i = 0; i < getPreferenceScreen().getPreferenceCount(); i++) { initSummary(getPreferenceScreen().getPreference(i)); } } @Override public void onResume() { super.onResume(); // Set up a listener whenever a key changes getPreferenceScreen().getSharedPreferences() .registerOnSharedPreferenceChangeListener(this); updatePrefSummary(findPreference("select_sensor")); } @Override public void onPause() { super.onPause(); // Unregister the listener whenever a key changes getPreferenceScreen().getSharedPreferences() .unregisterOnSharedPreferenceChangeListener(this); } public void onSharedPreferenceChanged( SharedPreferences sharedPreferences, String key) { updatePrefSummary(findPreference(key)); } private void initSummary(Preference p) { if (p instanceof PreferenceCategory) { PreferenceCategory pCat = (PreferenceCategory) p; for (int i = 0; i < pCat.getPreferenceCount(); i++) { initSummary(pCat.getPreference(i)); } } else { updatePrefSummary(p); } } private void updatePrefSummary(Preference p) { if (p instanceof ListPreference) { ListPreference listPref = (ListPreference) p; p.setSummary(listPref.getEntry()); } else if (p instanceof EditTextPreference) { EditTextPreference editTextPref = (EditTextPreference) p; p.setSummary(editTextPref.getText()); } else if (p instanceof PreferenceScreen) { p.setSummary(App.app().getSensor()); } } } }