leyteris 发表于 2013-1-14 18:27:33

Android中应用享元全局变量

在Intent中来回的传一些所谓的全局变量实在苦手,上网搜了搜,果然有省事的方案(http://stackoverflow.com/questions/708012/android-how-to-declare-global-variables)可以用继承Application来享元~~直接贴代码~
 
 
public class ShareContext extends Application {private PHPRPC_Client client;public PHPRPC_Client getClient() {return client;}public void setClient(PHPRPC_Client client) {this.client = client;}} 
 然后在AndroidManifest.xml中设置name属性
 
<application android:name=".ShareContext" android:icon="@drawable/icon" android:label="@string/app_name">设置享元:
 
ShareContext shareContext = ((ShareContext)getApplicationContext());shareContext.setClient(client); 
 读取享元:
 
ShareContext shareContext = ((ShareContext)getApplicationContext());PHPRPC_Client client = shareContext.getClient(); 好东西~~直接省了很多事!!
 
页: [1]
查看完整版本: Android中应用享元全局变量