I am beyond frustrated in why my preferences are not doing anything. Currently I have it set up in an OnSharedPreferenceChangeListener:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
PreferenceManager.setDefaultValues(this, R.xml.preferences_new, false);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
// TODO Auto-generated method stub
if (key.equals("password")) {
LayoutInflater inflater = LayoutInflater.from(this);
final View text = inflater.inflate(R.layout.changepassword, null);
final EditText currentPassword = (EditText)text.findViewById(R.id.currentPassword);
final EditText newPassword = (EditText)text.findViewById(R.id.newPassword);
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Change Your Password");
alert.setView(text);
alert.setPositiveButton("Change", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String stringData = currentPassword.getText().toString().trim();
String stringNew = newPassword.getText().toString().trim();
dataReturned = myFolder.getString("passwordKey", "");
if(dataReturned.equals(stringData)) {
String newData = newPassword.getText().toString().trim();
SharedPreferences.Editor editor = myFolder.edit();
editor.putString("passwordKey", newData);
editor.commit();
dataReturned = myFolder.getString("passwordKey", "couldn't load data");
Toast.makeText(getApplicationContext(), "Password changed", Toast.LENGTH_SHORT).show();
currentPassword.setText("");
newPassword.setText("");
}
else {
Toast.makeText(getApplicationContext(), "Incorrect Password", Toast.LENGTH_LONG).show();
currentPassword.setText("");
newPassword.setText("");
}
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
alert.show();
;
}
if (key.equals("notification")) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.device_access_secure)
.setOngoing(true)
.setContentTitle("Obstruct")
.setContentText("Start Stealth Mode");
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
}
}
Here's my PreferenceFragment:
public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences_new);
}
}
Here's my preference xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="preferenceScreen" >
<PreferenceCategory android:title="@string/pref_password">
<Preference
android:title="@string/pref_password"
android:summary="@string/pref_change_password"
android:key="password"
android:enabled="true"/>
</PreferenceCategory>
<PreferenceCategory
android:title="@string/pref_notif">
<CheckBoxPreference
android:title="@string/pref_enable_disable"
android:summary="@string/pref_enable_check"
android:key="notification"
android:defaultValue="true"
android:enabled="true"/>
</PreferenceCategory>
Every time I click on one of my preferences it doesn't do anything. Am I missing something or is there an easier way?