Android由众多Activity组成,每个Activity对应不同的功能和UI,但是每个Activity都是一个单独的类,所以需要传递参数。一般来讲,Android中传递参数是在不同的Activity中。大致上有5类方案: 

1 静态 static 

定义方法: 

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 
定义方法:

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 }

使用方法: 

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,但可传的参数类型支持很少。 
定义方法: 

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数据库 
创建数据库方法: 

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 }

调用之: 

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 }
 
posted on 2014-09-28 11:17  xuan轩  阅读(829)  评论(0编辑  收藏  举报