DOC PREVIEW
SDSU CS 696 - Data

This preview shows page 1-2-15-16-17-32-33 out of 33 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 696 Mobile Phone Application DevelopmentFall Semester, 2009Doc 4 DataSept 14, 2009Copyright ©, All rights reserved. 2009 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent (http://www.opencontent.org/opl.shtml) license defines the copyright on this document.References2Google Android Documentation, http://code.google.com/android/documentation.htmlIntent - Passing Data3IntentExample PersonEditorDisplays/Edits ageGo buttonCalls PersonEditorPasses dataNameAgeDisplays/Edits Name and ageDone buttonReturns edited data back Age = 0 cancels editIntentExample.java4public class IntentExample extends Activity implements View.OnClickListener { private EditText numberText; private static final int INTENT_EXAMPLE_REQUEST = 123; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.intent); Button ok = (Button) findViewById(R.id.go); ok.setOnClickListener(this); numberText = (EditText) this.findViewById(R.id.number); numberText.setText("21"); }When we want a reply back from an Intent request we supply a request number. The request number is return with the answer. That way it is possible to know where the request originated from.IntentExample.Java continued5 public void onClick(View v) { Intent go; go = new Intent(); go.setAction("android.intent.action.EDIT"); go.addCategory("person_editor"); String newAge = numberText.getText().toString(); go.putExtra("age", newAge); go.putExtra("name", "Roger"); startActivityForResult(go, INTENT_EXAMPLE_REQUEST); }Sending the data to PersonEditorThe name was sent just to show we can send multiple items. They can be of any base type or serializable. See the putExtra methods in the Intent class.IntentExample.Java continued6 protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode != INTENT_EXAMPLE_REQUEST) { numberText.setText("Not from me"); return; } switch (resultCode) { case RESULT_OK: String editedAge = data.getStringExtra("age"); numberText.setText(editedAge); break; case RESULT_CANCELED: numberText.setText("Cancelled"); break; } }}Getting the Results backWhen an activity is started after a return from a startActivityForResult request, the method onActivityResult is called. The requestCode is the request code sent in the startActivityForResult method. The result code is the set by the called activity and is RESULT_OK or RESULT_CANCELED by convention. The intent is used to send data back to this activity. RESULT_OK and RESULT_CANCELED are constants defined in the Activity class.PersonEditor.java7public class PersonEditor extends Activity implements View.OnClickListener { private EditText ageText; private EditText nameText; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.person_editor); Button done = (Button) findViewById(R.id.edit_done); done.setOnClickListener(this); ageText = (EditText) this.findViewById(R.id.edit_age); nameText = (EditText) this.findViewById(R.id.edit_name); Bundle personData = getIntent().getExtras(); String age = personData.getString("age"); String name = personData.getString("name"); if ((age != null) && (name != null)) { ageText.setText(age); nameText.setText(name); } }Showing how to access the intent that started the activity and extracting the extras from the intent. If the key-value pair was not set in the intent the value will be returned as null.PersonEditor.java8 public void onClick(View v) { String newAge = ageText.getText().toString(); Intent result = getIntent(); result.putExtra("age", newAge); if (newAge.equals("0")) setResult(RESULT_CANCELED, result); else setResult(RESULT_OK, result); finish(); }Returning the datasetResult(int, Intent) returns information to the calling activity. The first parameter is the result code passed back in onActivityResult. The intent is the intent passed back in onActivityResult.AndroidManifest.xml9<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="edu.sdsu.cs683.example" android:versionCode="1" android:versionName="1.0.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".IntentExample" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:label="PersonEditor" android:name="PersonEditor"> <intent-filter> <action android:name="android.intent.action.EDIT"></action> <category android:name="person_editor"></category> <category android:name="android.intent.category.DEFAULT"></category> </intent-filter> </activity> </application></manifest> The intent filter for the activity must contain all the categories used by the intent to select the activity. It can contain more categories. The example does not work without the default category.10Data StorageData Topics11PreferencesFilesSQLite databaseContent ProvidersNetworkPreferences12Key value pairs for programKey - stringValue booleanfloatintlongstring getPreferences(int mode)For access in activity onlygetSharedPreferences(String name,int mode)To share preferences with other activitiesmode0 = MODE_PRIVATEMODE_WORLD_READABLEMODE_WORLD_WRITEABLECannot share preferences across applications or threadsExample13public class Calc extends Activity {public static final String PREFS_NAME = "MyPrefsFile"; protected void onCreate(Bundle state){ super.onCreate(state); SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); boolean silent = settings.getBoolean("silentMode", false); setSilent(silent); } protected void onStop(){ super.onStop(); SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putBoolean("silentMode", mSilentMode); editor.commit(); }}Example from http://code.google.com/android/devel/data/preferences.html. See http://developer.android.com/reference/android/app/Activity.html for an example of using getSharedPreferences()Files14Application can write/read files on phoneCannot directly read files


View Full Document

SDSU CS 696 - Data

Download Data
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Data and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Data 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?