202k. User Preferences




























 package com.chicagoandroid.w160;


import android.app.Activity;

import android.app.PendingIntent;

import android.content.Context;

import android.content.Intent;

import android.content.SharedPreferences;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;


public class Main extends Activity

{

private static final String CONTACT_INFO = "contactInfo";

protected static EditText contactInfo;

protected static PendingIntent pendingIntent;

private static final String PREFERENCE_NAME = Main.class.getCanonicalName();

private SharedPreferences preferences;

private Button smsTestButton, saveButton;


private void addSaveButtonListener()

{

saveButton.setOnClickListener(new OnClickListener()

{

@Override

public void onClick(View arg0)

{

String text = (String) contactInfo.getText().toString();

Log.i("saveButton.setOnClickListener", (String) contactInfo.getText().toString());

Toast.makeText(getApplicationContext(), "Saving: " + text, Toast.LENGTH_SHORT).show();

saveUserPreferences();

}

});

}


private void addTestButtonListener()

{

smsTestButton.setOnClickListener(new OnClickListener()

{

@Override

public void onClick(View arg0)

{

smsTestButton.setClickable(false);

String text = (String) contactInfo.getText().toString();

Log.i("smsTestButton.setOnClickListener", text);

Toast.makeText(getApplicationContext(), "Sending: " + text, Toast.LENGTH_SHORT).show();

SmsSender.validateAndSendSms(getApplicationContext(), "6508156603", text);

}

});

}


@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);


contactInfo = (EditText) findViewById(R.id.contact_info);

smsTestButton = (Button) findViewById(R.id.sms_test_button);

saveButton = (Button) findViewById(R.id.save_button);

restoreUserPreferences();

addTestButtonListener();

addSaveButtonListener();

pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, Main.class), 0);

}


private void restoreUserPreferences()

{

if (preferences == null)

preferences = getSharedPreferences(PREFERENCE_NAME, Context.MODE_APPEND);


String savedText = preferences.getString(CONTACT_INFO, "my contact info has not be set yet.. someone@ChicagoAndroid.com");

Log.i("restoreUserPreferences() for " + PREFERENCE_NAME, "contactInfo: " + savedText);

contactInfo.setText(savedText);

}


private void saveUserPreferences()

{


if (preferences == null)

preferences = getSharedPreferences(PREFERENCE_NAME, Context.MODE_APPEND);


String info = contactInfo.getText().toString();

Log.i("saveUserPreferences() for " + PREFERENCE_NAME, "saving EditText value: " + info);


SharedPreferences.Editor editor = preferences.edit();

editor.putString(CONTACT_INFO, info);

editor.commit();

}


}


Comments