| 1 基于消息的通信機(jī)制 Intent--------boudle,extra 用這種簡(jiǎn)單的形式,一般而言傳遞一些簡(jiǎn)單的類型是比較容易的,如int、string等 詳細(xì)介紹下Intent機(jī)制 Intent包含兩部分: 1 目的【action】-------要去到哪里去 2 內(nèi)容【category、data】----------路上帶些什么,區(qū)分性數(shù)據(jù)和內(nèi)容性數(shù)據(jù) 簡(jiǎn)單數(shù)據(jù)傳遞: 復(fù)制代碼 1 2 3 4 5 6 7 8 9 10 Intent intent = new Intent(LoginActivity.this, MainActivity.class); intent.putExtra("flag", flag); startActivity(intent); ///////////////////////////////////////////////////////// String flag = "   "; Intent intent1 = this.getIntent();   flag = intent1.getStringExtra("flag"); ///////////////////////////////////////////////////////// 數(shù)據(jù)類型有限,遇到不可序列化的數(shù)據(jù)Bitmap,Inputstream,或者是LinkList鏈表等數(shù)據(jù)類型就不太好用了 2 利用static靜態(tài)數(shù)據(jù),public static成員變量 我們千萬(wàn)不要以為Davlik虛擬機(jī)的垃圾回收器會(huì)幫助我們回收不需要的內(nèi)存垃圾。事實(shí)上,回收器并不可靠, 尤其是手機(jī)上,是更加的不可靠。 因此,除非我們要使自己的程序變得越來(lái)越糟糕,否則盡量遠(yuǎn)離static。 注:如果經(jīng)常使用static的Bitmap、Drawable等變量。可能就會(huì)拋出一個(gè)在Android系統(tǒng)中非常著名的異常( 以前budget這個(gè)單詞一直記不住什么意思,自從經(jīng)常拋出這個(gè)異常后,這個(gè)單詞終于爛熟于心了,) ERROR/AndroidRuntime(4958): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget 注:如果經(jīng)常使用static的Bitmap、Drawable等變量??赡芫蜁?huì)拋出一個(gè)在Android系統(tǒng)中非常著名的異常( 以前budget這個(gè)單詞一直記不住什么意思,自從經(jīng)常拋出這個(gè)異常后,這個(gè)單詞終于爛熟于心了,) 3 基于外部存儲(chǔ)的傳輸 ,F(xiàn)ile/Preference/Sqlite,如果要針對(duì)第三方應(yīng)用需要Content provider 作為一個(gè)完成的應(yīng)用程序,數(shù)據(jù)存儲(chǔ)操作是必不可少的。因此,Android系統(tǒng)一共提供了四種數(shù)據(jù)存儲(chǔ)方式。 分別是:SharePreference、SQLite、Content Provider和File。由于Android系統(tǒng)中,數(shù)據(jù)基本都是私有的的 ,都是存放于“data/data/程序包名”目錄下,所以要實(shí)現(xiàn)數(shù)據(jù)共享,正確方式是使用Content Provider。 SQLite: SQLite是一個(gè)輕量級(jí)的數(shù)據(jù)庫(kù),支持基本SQL語(yǔ)法,是常被采用的一種數(shù)據(jù)存儲(chǔ)方式。Android 為此數(shù)據(jù)庫(kù)提供了一個(gè)名為SQLiteDatabase的類,封裝了一些操作數(shù)據(jù)庫(kù)的API。 SharedPreference: 除SQLite數(shù)據(jù)庫(kù)外,另一種常用的數(shù)據(jù)存儲(chǔ)方式,其本質(zhì)就是一個(gè)xml文件,常用于 存儲(chǔ)較簡(jiǎn)單的參數(shù)設(shè)置。 File: 即常說(shuō)的文件(I/O)存儲(chǔ)方法,常用語(yǔ)存儲(chǔ)大數(shù)量的數(shù)據(jù),但是缺點(diǎn)是更新數(shù)據(jù)將是一件困難的 事情。 ContentProvider: ContentProvider是安卓平臺(tái)中,在不同應(yīng)用程序之間實(shí)現(xiàn)數(shù)據(jù)共享的一種機(jī)制。一個(gè) 應(yīng)用程序如果需要讓別的程序可以操作自己的數(shù)據(jù),即可采用這種機(jī)制。并且此種方式忽略了底層的數(shù)據(jù)存儲(chǔ) 實(shí)現(xiàn),ContentProvider提供了一種統(tǒng)一的通過(guò)Uri實(shí)現(xiàn)數(shù)據(jù)操作的方式。 詳細(xì)介紹使用過(guò)程 File 通過(guò)文件內(nèi)容的讀取傳遞數(shù)據(jù) Preference: SharedPreferences也是一種輕型的數(shù)據(jù)存儲(chǔ)方式,它的本質(zhì)是基于XML文件存儲(chǔ)key-value鍵值 對(duì)數(shù)據(jù),通常用來(lái)存儲(chǔ)一些簡(jiǎn)單的配置信息 SharedPreferences對(duì)象本身只能獲取數(shù)據(jù)而不支持存儲(chǔ)和修改,存儲(chǔ)修改是通過(guò)Editor對(duì)象實(shí)現(xiàn)。實(shí)現(xiàn) SharedPreferences存儲(chǔ)的步驟如下: 一、根據(jù)Context獲取SharedPreferences對(duì)象 二、利用edit()方法獲取Editor對(duì)象。 三、通過(guò)Editor對(duì)象存儲(chǔ)key-value鍵值對(duì)數(shù)據(jù)。 四、通過(guò)commit()方法提交數(shù)據(jù)。 復(fù)制代碼 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16    SharedPreferences sp=getSharedPreferences("login",0);//login存儲(chǔ)文件名    SharedPreferences.Editor se=sp.edit();;    se.putString("server", logEdit.getText().toString());    se.putString("port", portEdit.getText().toString());    se.commit(); /////////////////////////////////////////////////////////////   SharedPreferences ps=getSharedPreferences("login",0);//login是存儲(chǔ)文件     server=ps.getString("server", "");   port=ps.getString("port", "");   logEdit.setText(server);   portEdit.setText(port); ///////////////////////////////////////////////////////////// ContentProvider 其步驟為: 1. 在當(dāng)前應(yīng)用程序中定義一個(gè)ContentProvider。 2. 在當(dāng)前應(yīng)用程序的AndroidManifest.xml中注冊(cè)此ContentProvider 3. 其他應(yīng)用程序通過(guò)ContentResolver和Uri來(lái)獲取此ContentProvider的數(shù)據(jù)。 在程序A中,繼承ContProvider類,并重寫其中的方法 復(fù)制代碼 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 public class MyProvider extends ContentProvider{      @Override      public int delete(Uri uri, String selection, String[] selectionArgs) {          // TODO Auto-generated method stub          return 0;      }      @Override      public String getType(Uri uri) {          // TODO Auto-generated method stub          return null;      }      @Override      public Uri insert(Uri uri, ContentValues values) {          return null;      }      //在Create中初始化一個(gè)數(shù)據(jù)庫(kù)      @Override      public boolean onCreate() {          SQLiteDatabase db = this.getContext().openOrCreateDatabase("test_db.db3",  Context.MODE_PRIVATE, null);          db.execSQL("create table tab(_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT  NULL)");          ContentValues values = new ContentValues();          values.put("name", "test");          db.insert("tab", "_id", values);          db.close();          return true;      }      //實(shí)現(xiàn)query方法      @Override      public Cursor query(Uri uri, String[] projection, String selection,              String[] selectionArgs, String sortOrder) {          SQLiteDatabase db = this.getContext().openOrCreateDatabase("test_db.db3",  Context.MODE_PRIVATE, null);          Cursor c = db.query("tab", null, null, null, null, null,null);          return c;      }      @Override      public int update(Uri uri, ContentValues values, String selection,              String[] selectionArgs) {          // TODO Auto-generated method stub          return 0;      }  } 在其AndroidManifest.xml中聲明此ContentProvider,其中authorities屬性定義了此ContentProvider的Uri 標(biāo)識(shí)。 <provider android:name=".MyProvider" android:authorities="com.test.MyProvider"/> 在應(yīng)用程序B中,通過(guò)ContentResolver獲取程序A的ContentProvider中的數(shù)據(jù)。 復(fù)制代碼 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public class MainActivity extends Activity {      @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);          //獲取上下文          Context ctx = MainActivity.this;          //獲取ContentResolver對(duì)象          ContentResolver resolver = ctx.getContentResolver();          //獲取Uri對(duì)象          Uri uri = Uri.parse("content://com.test.MyProvider");          //獲取數(shù)據(jù)          Cursor c = resolver.query(uri, null, null, null, null);          c.moveToFirst();          for(int i=0; i<c.getCount(); i++){              int index = c.getColumnIndexOrThrow("name");              String src = c.getString(index);              Log.d("", src);              c.moveToNext();          }      }  } 再觀察兩個(gè)應(yīng)用程序的結(jié)構(gòu),A的程序結(jié)構(gòu),可以清楚看到其有一個(gè)名為“test_db.db3”的數(shù)據(jù)庫(kù),B的程序結(jié)構(gòu),其并沒有任何數(shù)據(jù)庫(kù)用于存儲(chǔ)數(shù)據(jù)。由此圖,可以確定應(yīng)用程序B中查詢出來(lái)的數(shù)據(jù)結(jié)果是來(lái)自于應(yīng)用程序A。 以上就是ContentProvider的使用方式,這種存儲(chǔ)方式相比SQLite和SharedPreferences,其復(fù)雜性是顯而易見的,但是在處處可見“云”的今天,程序間的數(shù)據(jù)交互需求令ContentProvider存儲(chǔ)機(jī)制變成必不可少的一部分。 4 基于Ipc的通信機(jī)制 context與service之間的傳輸,如Activity與Service之間的通信 5 基于Application Context 在一個(gè)activity初始化一個(gè)ArrayList<HashMap<Sting,Map>>對(duì)象,然后經(jīng)過(guò)一個(gè)tableactivity,在傳遞到另 外一個(gè)activity,一開始直接考慮用putExtra,測(cè)試發(fā)現(xiàn)數(shù)據(jù)只能傳遞一次,就考慮用Application傳遞 Java里面通常是用一個(gè)static的變量(例如singleton之類的)來(lái)同步activity之間(程序里面類之間)的狀態(tài)。在android里面比較靠譜的做法是用application context來(lái)關(guān)聯(lián)這些狀態(tài)。 每個(gè)activity都是context,里面包含了運(yùn)行時(shí)的狀態(tài)。同樣application也有一個(gè)context,android會(huì)保證這個(gè)context是唯一的實(shí)例。 復(fù)制代碼 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 package net.blogjava.mobile1; import android.app.Application; import android.graphics.Bitmap; public class MyApp extends Application {     private Bitmap mBitmap;     public Bitmap getBitmap()     {         return mBitmap;     }     public void setBitmap(Bitmap bitmap)     {         this.mBitmap = bitmap;     } } <application android:name=".MyApp" android:icon="@drawable/icon" android:label="@string/app_name"> </application> 獲得Bitmap對(duì)象的代碼: 復(fù)制代碼 1 2 3 4 5 6 7     ImageView imageview = (ImageView)findViewById(R.id.ivImageView);     MyApp myApp = (MyApp)getApplication();     imageview.setImageBitmap(myApp.getBitmap()); 上面兩段代碼可以在任何的Service、Activity中使用。全局的 | 
|  | 
來(lái)自: 520jefferson > 《java/android》