android傳遞參數

 Android由衆多Activity組成,每一個Activity對應不一樣的功能和UI,可是每一個Activity都是一個單獨的類,因此須要傳遞參數。通常來說,Android中傳遞參數是在不一樣的Activity中。大體上有5類方案: android

1 靜態 static sql

定義方法: 數據庫

1 public class MainActivity  extends TabActivity {
2         public static DatabaseHelper mHelper;
3     public static  SQLiteDatabase db;
4 }

使用方法:多線程

1 public    class QQList extends Activity{
2         try{
3              db=MainActivity.db;
4              
5      }catch(Exception e){
6          e.printStackTrace();
7      }
8 }

2 單例 Singleton 
定義方法:socket

Java代碼
 1 public class SingleSocket {
 2 /*
 3  * GG單例模式的第四個版本,既解決了「懶漢式的」多線程問題,又解決了資源浪費的現象,看上去是一種不錯的選擇
 4 */
 5         //Socket成員
 6         private Socket socket=null;
 7         //對單例自己引用的名稱
 8         private static SingleSocket sSocket;
 9         public Socket getSocket() {
10             return socket;
11         }
12         public void initSocket(String ip,int port ) {
13             try{
14                     socket=new Socket(ip,port);
15             }catch(Exception e){
16                 e.printStackTrace();
17             }
18         }
19         //構造函數私有化
20         private SingleSocket() {
21         } 
22         //提供一個全局的靜態方法
23         public static SingleSocket getSingle() {
24             if(sSocket == null) {
25                 synchronized (SingleSocket.class) {
26                     if(sSocket == null) {
27                         sSocket = new SingleSocket();
28                     }
29                 }
30             }
31             return sSocket;
32         }
33 }

使用方法: jsp

Java代碼
1 public class MainActivity extends TabActivity {
2         private SingleSocket olsocket=SingleSocket.getSingle();
3         private  Socket socket=null;
4 }

3 經過Intent傳遞 
Intent是Android的類,含有傳遞參數的方法,相似於jsp中的get,但可傳的參數類型支持不多。 
定義方法: ide

Java代碼
1 public class ClientActivity extends Activity {            
2     Intent intent = new Intent(); 3 Bundle bundle = new Bundle(); 4 bundle.putString("Usr", Usr); 5 bundle.putString("Psd", Psd); 6  intent.putExtras(bundle); 7 intent.setClass(ClientActivity.this, MainActivity.class); 8 }

使用方法: 函數

Java代碼
1 Bundle bundle = getIntent().getExtras();
2 Usr = bundle.getString("Usr"); 3 Psd=bundle.getString("Psd");

 Socket傳遞 
這個無需多說,建立Socket和ServerSocket對,進行通訊。 

5 SQLite數據庫 
建立數據庫方法: ui

Java代碼 
 1 package com.android.client;
 2 import android.content.Context; 3 import android.database.sqlite.SQLiteDatabase; 4 import android.database.sqlite.SQLiteOpenHelper; 5 import android.database.sqlite.SQLiteDatabase.CursorFactory; 6 7 public class DatabaseHelper extends SQLiteOpenHelper{ 8 /*public static final String TB_XINLANGWEIBO="XINLANGWEIBO"; 9 public static final String TB_QQGROUP="QQGROUP"; 10 public static final String TB_WEB="WEB"; 11 public static final String TB_QQLIST="QQLIST"; 12 public static final String TB_CLIENTPUB="CLIENTPUB"; 13 14 public static final String XINLANGWEIBO_ID="_id"; 15 public static final String QQGROUP_ID="_id"; 16 public static final String WEB_ID="_id"; 17 public static final String CLIENTPUB_ID="_id"; 18 19 public static final String QQLIST_ID="_id"; 20 public static final String QQLIST_NICK="NICK";*/ 21 public static final String TB_BUSINESS="BUSINESS"; 22 23 public static final String ID="_id"; 24 public static final String class="CLASS"; 25 public static final String CONTENT="CONTENT"; 26 public static final String FROM="FRM"; 27 public static final String WHO="WHO"; 28 29 30 public DatabaseHelper(Context context,String name,CursorFactory factory,int version){ 31 super(context,name,factory,version); 32  } 33 34  @Override 35 public void onCreate(SQLiteDatabase arg0){ 36 //arg0.execSQL("CREATE TABLE IF NOT EXISTS "+TB_XINLANGWEIBO+"(_id INTEGER PRIMARY KEY AUTOINCREMENT,CONTENT TEXT);"); 37 //arg0.execSQL("CREATE TABLE IF NOT EXISTS "+TB_QQGROUP+"(_id INTEGER PRIMARY KEY AUTOINCREMENT,CONTENT TEXT);"); 38 //arg0.execSQL("CREATE TABLE IF NOT EXISTS "+TB_WEB+"(_id INTEGER PRIMARY KEY AUTOINCREMENT,CONTENT TEXT);"); 39 40 //arg0.execSQL("CREATE TABLE IF NOT EXISTS "+TB_QQLIST+"(_id TEXT PRIMARY KEY,NICK TEXT);"); 41 //arg0.execSQL("CREATE TABLE IF NOT EXISTS "+TB_CLIENTPUB+"(_id INTEGER PRIMARY KEY AUTOINCREMENT,CONTENT TEXT);"); 42 arg0.execSQL("CREATE TABLE IF NOT EXISTS "+TB_BUSINESS+"(_id INTEGER PRIMARY KEY AUTOINCREMENT,CLASS TEXT,CONTENT TEXT,FRM TEXT,WHO TEXT);"); 43 44 45  } 46  @Override 47 public void onUpgrade(SQLiteDatabase db,int oldVertion,int newVersion){ 48 //db.execSQL("DROP TABLE IF EXISTS"+TB_XINLANGWEIBO); 49 //db.execSQL("DROP TABLE IF EXISTS"+TB_QQGROUP); 50 //db.execSQL("DROP TABLE IF EXISTS"+TB_WEB); 51 //db.execSQL("DROP TABLE IF EXISTS"+TB_QQLIST); 52  onCreate(db); 53 54  } 55 }

調用之: this

Java代碼
 1       try{
 2         
 3         //建立數據庫(若是數據庫文件已經存在就不會再建立)
 4         mHelper = new DatabaseHelper(this,DB_NAME,null,VERSION); 5 db=mHelper.getWritableDatabase(); 6 }catch (Exception ee){ 7 AlertDialog dlg1=new AlertDialog.Builder(this) 8  .setMessage(ee.getMessage()) 9 .setPositiveButton("Ok", null) 10  .show(); 11 }

使用方法,其實就是查詢數據庫: 

Java代碼
1 try{
2     cursor=db.query(TB_NAME, new String []{"_id","NICK"},null, null, null, null,null); 3 }catch(Exception ee){ 4  ee.printStackTrace(); 5 AlertDialog dlg=new AlertDialog.Builder(QQList.this) 6 .setMessage("沒找到表") 7 .setPositiveButton("Ok", null) 8  .show(); 9 }
相關文章
相關標籤/搜索