Android transmits parameters. android transmits parameters.
Android is composed of multiple activities. Each Activity corresponds to different functions and UIS, but each Activity is a separate class, so parameters must be passed. In general, parameters passed in Android are in different activities. Generally, there are five solutions:
1 static
Definition method:
1 public class MainActivity extends TabActivity {2 public static DatabaseHelper mHelper;3 public static SQLiteDatabase db;4 }
Usage:
1 public class QQList extends Activity{2 try{3 db=MainActivity.db;4 5 }catch(Exception e){6 e.printStackTrace();7 }8 }
2 Singleton
Definition method:
Java code
1 public class SingleSocket {2/* 3 * GG SingleSocket version 4 of SingleSocket not only solves the "lazy" multithreading problem, but also solves the waste of resources, it looks like a good choice 4 */5 // Socket member 6 private Socket socket = null; 7 // name referenced by the SingleSocket sSocket itself 8 private static SingleSocket; 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 // constructor private 20 private SingleSocket () {21} 22 // provides a global static Method 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}
Usage:
Java code
1 public class MainActivity extends TabActivity {2 private SingleSocket olsocket=SingleSocket.getSingle();3 private Socket socket=null;4 }
3 passed through Intent
Intent is an Android class,Methods for passing Parameters, Similar to get in jsp, but few parameter types are supported.
Definition method:
Java code
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 }
Usage:
Java code
1 Bundle bundle = getIntent().getExtras();2 Usr = bundle.getString("Usr");3 Psd=bundle.getString("Psd");
Socket transfer
Needless to say, create a Socket and ServerSocket pair for communication.
5. SQLite Database
Database creation method:
Java code
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 @Override35 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 @Override47 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 }
Called:
Java code
1 try {2 3 // create a database (if the database file already exists, it will not be created) 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}
The usage is actually to query the database:
Java code
1 try {2 cursor = db. query (TB_NAME, new String [] {"_ id", "NICK"}, null); 3} catch (Exception ee) {4 ee. printStackTrace (); 5 AlertDialog dlg = new AlertDialog. builder (QQList. this) 6. setMessage ("no table found") 7. setPositiveButton ("OK", null) 8. show (); 9}
How can android pass multiple parameters and objects?
You can pass parameters between activities.
Ntent intent = new Intent ();
Intent. setClass (this, DetailActivity. class );
Intent. putExtra ("human", human );
Intent. putExtra ("method", method );
.....
You can put more parameters here.
StartActivity (intent );
You can obtain the passed parameters in the corresponding activity in this way.
GetIntent (). getExtras (). getSerializable ("human ");
.......
Parameter passing in android
It should be recognizable. the result you define is a member variable. The internal class can access the member variables of the external class, unless your internal class is in a method, the variable to be accessed is the local variable of the method.
In this case, the internal class can only access the final variable in the method, because if the method stack ends, the result variable will definitely disappear, but the internal class
It may also exist in the heap because it may not be recycled so quickly. In this case, it may access a variable result, however, the result of this variable has already been cleared. This is certainly not acceptable. Therefore, either define the result as final.