Android参数传递总结

Android由众多Activity组成,每个Activity对应不同的功能和UI,但是每个Activity都是一个单独的类,所以需要传递参数。一般来讲,Android中传递参数是在不同的Activity中。大致上有5类方案:
1 静态 static
定义方法:
public class MainActivity extends TabActivity {
        public static DatabaseHelper mHelper;
	public static  SQLiteDatabase db;
}


使用方法:
public	class QQList extends Activity{
        try{
         	db=MainActivity.db;
 			
 	}catch(Exception e){
 		e.printStackTrace();
 	}
}



2 单例 Singleton
定义方法:
public class SingleSocket {
/*
 * GG单例模式的第四个版本,既解决了“懒汉式的”多线程问题,又解决了资源浪费的现象,看上去是一种不错的选择
*/
	    //Socket成员
		private Socket socket=null;
	    //对单例本身引用的名称
	    private static SingleSocket sSocket;
	    public Socket getSocket() {
	        return socket;
	    }
	    public void initSocket(String ip,int port ) {
	        try{
	        		socket=new Socket(ip,port);
	        }catch(Exception e){
	        	e.printStackTrace();
	        }
	    }
	    //构造函数私有化
	    private SingleSocket() {
	    } 
	    //提供一个全局的静态方法
	    public static SingleSocket getSingle() {
	        if(sSocket == null) {
	            synchronized (SingleSocket.class) {
	                if(sSocket == null) {
	                    sSocket = new SingleSocket();
	                }
	            }
	        }
	        return sSocket;
	    }
}


使用方法:
public class MainActivity extends TabActivity {
        private SingleSocket olsocket=SingleSocket.getSingle();
        private  Socket socket=null;
}


3 通过Intent传递
Intent是Android的类,含有传递参数的方法,类似于jsp中的get,但可传的参数类型支持很少。
定义方法:
public class ClientActivity extends Activity {  		  
	Intent intent = new Intent();		
	Bundle bundle = new Bundle();  
	bundle.putString("Usr", Usr); 
	bundle.putString("Psd", Psd); 
	intent.putExtras(bundle); 
	intent.setClass(ClientActivity.this, MainActivity.class);
}

使用方法:
Bundle bundle = getIntent().getExtras();
Usr = bundle.getString("Usr");
Psd=bundle.getString("Psd");


4 Socket传递
这个无需多说,创建Socket和ServerSocket对,进行通信。

5 SQLite数据库
创建数据库方法:
package com.android.client;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public	class	DatabaseHelper	extends	SQLiteOpenHelper{
	/*public static final String TB_XINLANGWEIBO="XINLANGWEIBO";
	public static final String TB_QQGROUP="QQGROUP";
	public static final String TB_WEB="WEB";
	public static final String TB_QQLIST="QQLIST";
	public static final String TB_CLIENTPUB="CLIENTPUB";

	public static final String XINLANGWEIBO_ID="_id";
	public static final String QQGROUP_ID="_id";
	public static final String WEB_ID="_id";
	public static final String CLIENTPUB_ID="_id";

	public static final String QQLIST_ID="_id";
	public static final String QQLIST_NICK="NICK";*/
	public static final String TB_BUSINESS="BUSINESS";
	
	public static final String ID="_id"; 
	public static final String CLASS="CLASS";
	public static final String CONTENT="CONTENT";
	public static final String FROM="FRM";
	public static final String WHO="WHO";
	
	
	public DatabaseHelper(Context context,String name,CursorFactory factory,int version){
		super(context,name,factory,version);
	}
	
	@Override
	public void onCreate(SQLiteDatabase arg0){
		//arg0.execSQL("CREATE TABLE IF NOT EXISTS "+TB_XINLANGWEIBO+"(_id INTEGER PRIMARY KEY AUTOINCREMENT,CONTENT TEXT);");
		//arg0.execSQL("CREATE TABLE IF NOT EXISTS "+TB_QQGROUP+"(_id INTEGER PRIMARY KEY AUTOINCREMENT,CONTENT TEXT);");
		//arg0.execSQL("CREATE TABLE IF NOT EXISTS "+TB_WEB+"(_id INTEGER PRIMARY KEY AUTOINCREMENT,CONTENT TEXT);");
		
		//arg0.execSQL("CREATE TABLE IF NOT EXISTS "+TB_QQLIST+"(_id TEXT PRIMARY KEY,NICK TEXT);");
		//arg0.execSQL("CREATE TABLE IF NOT EXISTS "+TB_CLIENTPUB+"(_id INTEGER PRIMARY KEY AUTOINCREMENT,CONTENT TEXT);");
		arg0.execSQL("CREATE TABLE IF NOT EXISTS "+TB_BUSINESS+"(_id INTEGER PRIMARY KEY AUTOINCREMENT,CLASS TEXT,CONTENT TEXT,FRM TEXT,WHO TEXT);");

		
	}
	@Override
	public void onUpgrade(SQLiteDatabase db,int oldVertion,int newVersion){
		//db.execSQL("DROP TABLE IF EXISTS"+TB_XINLANGWEIBO);
		//db.execSQL("DROP TABLE IF EXISTS"+TB_QQGROUP);
		//db.execSQL("DROP TABLE IF EXISTS"+TB_WEB);
		//db.execSQL("DROP TABLE IF EXISTS"+TB_QQLIST);
		onCreate(db);
		
	}
}


调用之:
      try{
        
        //创建数据库(如果数据库文件已经存在就不会再创建)
        mHelper = new DatabaseHelper(this,DB_NAME,null,VERSION);
	db=mHelper.getWritableDatabase();
       }catch (Exception ee){
       	AlertDialog dlg1=new AlertDialog.Builder(this)  
            .setMessage(ee.getMessage())  
            .setPositiveButton("Ok", null)  
            .show();
        }

使用方法,其实就是查询数据库:
try{
    cursor=db.query(TB_NAME, new String []{"_id","NICK"},null, null, null, null,null);
}catch(Exception ee){
    ee.printStackTrace();
    AlertDialog dlg=new AlertDialog.Builder(QQList.this)  
                .setMessage("没找到表")  
                .setPositiveButton("Ok", null)  
                .show();
}

当然类似于数据库的传递方案还有很多,最常见的就是使用xml文件,但是思路一样,以上便是针对Android参数传递的一点总结。

你可能感兴趣的:(java,android)