`
sillycat
  • 浏览: 2551102 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

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...
}
分享到:
评论

相关推荐

    Android.for.the.BeagleBone.Black

    Several example projects within this book introduce you to using the GPIO, SPI, and I2C hardware interfaces of the BeagleBone Black. You'll create Android apps that communicate directly with actual ...

    Programming.Concepts.in.Cplusplus.1499044836

    Programming Concepts in C++ is one in a series of books that introduce the basic concepts of computer programming, using a selected programming language. Other books in the series use languages like ...

    Android.Games.Practical.Programming.By.Example.Quickstart

    Lastly, we'll introduce a set of Android tools that help you debug and profile your apps. Unit 2, Project Framework, discusses the storyline and framework of your first game Raccoon Rob. You'll be ...

    Decompiling Android.pdf

    ### Decompiling Android: Key Insights and Concepts The document "Decompiling Android.pdf" delves into the complex world of decompiling on the Android platform, a topic of significant importance for ...

    Introduce Android OHA.

    标题与描述中的核心知识点是关于Android开放手机联盟(Open Handset Alliance,简称OHA)的介绍,以及Android操作系统的概述。以下是对这些知识点的详细解释: ### Android开放手机联盟(OHA) Android开放手机...

    Asynchronous Android Programming

    This book is for Android developers who want to learn how to build multithreaded and reliable Android applications using high-level and advanced asynchronous techniques and concepts. What You Will ...

    03 PD introduce and application - 20190825.pptx

    里面介绍了PD方案和在实际中应用说明,及遇到的问题,解决方法,里面有市场上主流IC的介绍,供各工程技术人员学习和参考。

    cuda_by_example

    This book aims to introduce readers to the fundamental concepts of general-purpose GPU programming using CUDA. The book's target audience includes programmers, researchers, and students interested in...

    Android7.0 Audio Framework——framework introduce.pdf

    Android 7.0 Audio Framework 介绍 Android 7.0 Audio Framework 是 Android 操作系统中负责音频播放和录音功能的框架。该框架主要由三部分组成:Audio 的 JAVA 程序部分、Audio 的 JNI 部分和 Audio 的客户端部分...

    Microsoft Excel 2016 Programming by Example

    If you are looking to automate Excel routine tasks, this book will progressively introduce you to programming concepts via numerous, illustrated, hands-on exercises. Includes a comprehensive disc ...

    Programming Language Explorations

    As each language is presented, the authors introduce new concepts as they appear, and revisit familiar ones, comparing their implementation with those from languages seen in prior chapters....

    Android代码-green-coffee

    Green Coffee Green Coffee is a library that allows you to run your acceptance tests written in Gherkin in your Android instrumentation tests using the step ... And I introduce an invalid password

    android security framework

    We introduce theAndroid Security Framework (ASF),a generic, extensible security framework for Android that enables the development and integration of a wide spectrum of security models in form of code...

    Android RIL introduce

    Android RIL,全称为Radio Interface Layer,是Android操作系统中至关重要的一个组件,它充当着操作系统与无线通信硬件之间的桥梁。RIL负责处理所有与移动网络相关的底层通信,包括蜂窝数据、电话呼叫、短信服务以及...

    team introduce.key

    team introduce.key

    Introduction to Programming Using Python (英文版)

    programming concepts and techniques. “Fundamentals-first” means that students learn fundamental programming concepts like selection statements, loops, and functions, before moving into defining ...

    Brocade Product introduce and Hand on Training.pptx

    Brocade Product introduce and Hand on Training....

Global site tag (gtag.js) - Google Analytics