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を取得して設定することができます.
谁かの役に立つことを望みます!!!