首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android中施用享元全局变量

2012-09-28 
Android中应用享元全局变量在Intent中来回的传一些所谓的全局变量实在苦手,上网搜了搜,果然有省事的方案(h

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();

?好东西~~直接省了很多事!!

?

热点排行