`

Android中共享全局变量

阅读更多
MyApp.java:
public class MyApp extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		MyApplications appState = ((MyApplications) getApplicationContext());
		System.out.println("appState.getState()________" + appState.getState());
	}
}


MyApplications.java:
import android.app.Application;

public class MyApplications extends Application {
	private String myState = "hello from";

	public String getState() {
		return myState;
	}

	public void setState(String s) {
		myState = s;
	}
}


配置文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="com.example.aa" android:versionCode="1" android:versionName="1.0">
	<application android:name=".MyApplications" android:icon="@drawable/icon"
		android:label="@string/app_name">
		<activity android:name=".MyApp" android:label="@string/app_name">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
	</application>
</manifest>

我就不做过多解释了,这样一些全部变量我们就不需要用Bundle传来传去了,直接获取Context对象,然后get,set就可以获取和设置.
希望对某些人有帮助!!!:)))))
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics