Android Introduce(I)concepts and NoteBook example

Android Introduce(I)concepts and NoteBook example

1. Open Handset Alliance
Consist of the system
      View----->lists, grids, text boxes, buttons, web browser
      Content Providers-----> access to other applications(for example, contact database)
      Resource Manager----->
      Notification Manager----->
      Activity Manager ----->

Application lib
      System C -----> libc for embedded linux
      Media lib -----> MPEG4, H.264, MP3, AAC, AMR, JPG, PNG
      Surface Manager ---->
      LibWebCore -----> Web browser engine
      SGL ------> 2D
      3D libraries ----> 3D
      FreeType -------> bitmap and vector
      SQLite  -----> database engine

Dalvik ----> .dex

2. Build Android Envirenment

3. Application Analytic
Activity
Sometimes one activity means one screen. One activity is one class which extends from base activity
(android.app.Activity). There are data send/receive between 2 screens/activities.

Intent
     Action and Data.
     Action: MAIN, VIEW, PICK, EDIT

     Intent ------> IntentFilter  ------> IntentReceiver

IntentReceiver
Service
    Without activity, it will run in the system. Context.startService().

Content Provider
Share the data among in different applications

4. Dalvik Virtual Machine
android.app       ---------->runtime envirement
android.content--------->access and publish the data
android.database------>db
android.graphics--------> diagram lib, point, line and others, draw them on screen
android.location -------->
android.media -----------> music, video
android.net ---------------->
android.os  -----------------> system service, message transfer, IPC
android.opengl  ---------> OpenGL tool
android.provider -------> content provider
android.telephony ----->
android.view --------------> look and feel
android.util ----------------> date and time util
android.webkit ---------->
android.widget ----------> UI

Java file ------> Class -------> Dex ------> apk

5. Android Source Codes HelloActivity
AndroidManifest.xml   configuration file
layout/main.xml               setContentView(R.layout.main);
values/strings.xml           android:text="@string/hello"
R.java                                       gen from the xml files under res

content of main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"/>
</LinearLayout>

steps in java sources:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//create the UI with codes
//TextView tv = new TextView(this);
//tv.setText("This is test about say hello to Carl!");
//setContentView(tv);
//create the UI with the help of XML file
setContentView(R.layout.main);
}

6. Priority of threads
front thread
          Activities are using.
visual thread
         for example, pause activity
service thread
backend thread
         unvisual activity
empty thread

7. Get more steps on the HelloActivity, try to write a Note book
get the sample codes from here
http://www.chinaup.org/docs/intro/codelab/NotepadCodeLab.zip

Reading the code of Notepadv1
public class Notepadv1 extends ListActivity {
public void onCreate(Bundle savedInstanceState);      //load the activity, show the page
public boolean onCreateOptionsMenu(Menu menu); //draw the menu
public boolean onOptionsItemSelected(MenuItem item);// menu was clicked
}

private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(DATABASE_CREATE);
        }
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS notes");
            onCreate(db);
        }
}

Reading the code of Notepadv2
public class Notepadv2 extends ListActivity {
//use intent to communicate with another activity
private void createNote() {
    Intent i = new Intent(this, NoteEdit.class);
    startActivityForResult(i, ACTIVITY_CREATE);
}

//send extra message to another activity
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
        Cursor c = mNotesCursor;
        c.moveToPosition(position);
        Intent i = new Intent(this, NoteEdit.class);
            i.putExtra(NotesDbAdapter.KEY_ROWID, id);
        i.putExtra(NotesDbAdapter.KEY_TITLE, c.getString(
                c.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
        i.putExtra(NotesDbAdapter.KEY_BODY, c.getString(
                c.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
        startActivityForResult(i, ACTIVITY_EDIT);
    }
        //get the message back from Bundle
        protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        Bundle extras = intent.getExtras();
        switch(requestCode) {
        case ACTIVITY_CREATE:
            String title = extras.getString(NotesDbAdapter.KEY_TITLE);
            String body = extras.getString(NotesDbAdapter.KEY_BODY);
            mDbHelper.createNote(title, body);
            fillData();
            break;
        case ACTIVITY_EDIT:
            Long rowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
            if (rowId != null) {
                String editTitle = extras.getString(NotesDbAdapter.KEY_TITLE);
                String editBody = extras.getString(NotesDbAdapter.KEY_BODY);
                mDbHelper.updateNote(rowId, editTitle, editBody);
            }
            fillData();
            break;
        }
    }
}

public class NoteEdit extends Activity {
protected void onCreate(Bundle savedInstanceState) {
        ...snip...
        mTitleText = (EditText) findViewById(R.id.title);
        mBodyText = (EditText) findViewById(R.id.body);
        Button confirmButton = (Button) findViewById(R.id.confirm);
        mRowId = null;
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String title = extras.getString(NotesDbAdapter.KEY_TITLE);
            String body = extras.getString(NotesDbAdapter.KEY_BODY);
            mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
            if (title != null) {
                mTitleText.setText(title);
            }
            if (body != null) {
                mBodyText.setText(body);
            }
        }
        confirmButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putString(NotesDbAdapter.KEY_TITLE, mTitleText.getText().toString());
                bundle.putString(NotesDbAdapter.KEY_BODY, mBodyText.getText().toString());
                if (mRowId != null) {
                    bundle.putLong(NotesDbAdapter.KEY_ROWID, mRowId);
                }
                Intent mIntent = new Intent();
                mIntent.putExtras(bundle);
                setResult(RESULT_OK, mIntent);
                finish();
            }
        });
    }
}

Reading the code of Notepadv3
Only send the id of the data, move the createNote database functions to NoteEdit.java
public class Notepadv3 extends ListActivity {
...snip...
private void createNote() {
        Intent i = new Intent(this, NoteEdit.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, NoteEdit.class);
        i.putExtra(NotesDbAdapter.KEY_ROWID, id);
        startActivityForResult(i, ACTIVITY_EDIT);
    }
    protected void onActivityResult(int requestCode, int resultCode,
                                    Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        fillData();
    }
...snip...
}

public class NoteEdit extends Activity {
...snip...
mTitleText = (EditText) findViewById(R.id.title);
        mBodyText = (EditText) findViewById(R.id.body);
        Button confirmButton = (Button) findViewById(R.id.confirm);
        mRowId = savedInstanceState != null ? savedInstanceState.getLong(NotesDbAdapter.KEY_ROWID)
                : null;
if (mRowId == null) {
Bundle extras = getIntent().getExtras();           
mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID) : null;
}
populateFields();
        confirmButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            setResult(RESULT_OK);
            finish();
        }
        });
    }
    private void populateFields() {
        if (mRowId != null) {
            Cursor note = mDbHelper.fetchNote(mRowId);
            startManagingCursor(note);
            mTitleText.setText(note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
            mBodyText.setText(note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
        }
    }
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putLong(NotesDbAdapter.KEY_ROWID, mRowId);
    }
    protected void onPause() {
        super.onPause();
        saveState();
    }
    protected void onResume() {
        super.onResume();
        populateFields();
    }
...snip...
}

你可能感兴趣的:(C++,android,xml,UI,C#)