歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android中用Application類實現全局變量

Android中用Application類實現全局變量

日期:2017/3/1 10:18:39   编辑:Linux編程

在Java中如果要使用全局變量,一般定義public static類型的變量。但是這種方法不符合Android的框架架構,Android中要使用Application context。

Application是一個基類,這個基類的作用是獲取整個App的狀態,我們需要自己定義一個類來繼承這個基類。代碼如下:

  1. package com.tianjf;
  2. import android.app.Application;
  3. public class MyApplication extends Application {
  4. private boolean mHasPassword;
  5. public boolean ismHasPassword() {
  6. return mHasPassword;
  7. }
  8. public void setmHasPassword(boolean mHasPassword) {
  9. this.mHasPassword = mHasPassword;
  10. }
  11. @Override
  12. public void onCreate() {
  13. mHasPassword = true;
  14. super.onCreate();
  15. }
  16. }
我們定義了一個MyApplication繼承自Application,並定義了一個全局變量mHasPassword,然後復寫基類的onCreate方法,onCreate負責對所有全局變量賦初期值。

我們還需要把自定義的Application類添加到AndroidManifest.xml裡面,代碼如下:

  1. <application
  2. android:name="MyApplication"
  3. 。。。。。。。。。。。。。。。。。。。。。。。。。。。
  4. 。。。。。。。。。。。。。。。。。。。。。。。。。。。
  5. </application>
這樣做的目的:App的進程被創建的時候,這個類就會被實例化,onCreate方法就會被執行,給所有全局變量賦初期值。

這樣,所有的Activity就共同擁有這個類裡面的變量了。

下面用兩個Activity來測試一下,當一個Activity改變了全局變量的值之後,看看另一個Activity能不能取到改變後的值。

ApplicationDemoActivity.java

  1. package com.tianjf;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. public class ApplicationDemoActivity extends Activity implements
  9. OnClickListener {
  10. private static final String TAG = "ApplicationDemoActivity";
  11. @Override
  12. public void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.main);
  15. findViewById(R.id.button).setOnClickListener(this);
  16. }
  17. @Override
  18. public void onClick(View v) {
  19. switch (v.getId()) {
  20. case R.id.button:
  21. MyApplication myApplication = (MyApplication) getApplication();
  22. Log.i(TAG, String.valueOf(myApplication.ismHasPassword()));
  23. myApplication.setmHasPassword(false);
  24. Intent intent = new Intent(this, AnotherActivity.class);
  25. startActivity(intent);
  26. break;
  27. default:
  28. break;
  29. }
  30. }
  31. }
AnotherActivity.java
  1. package com.tianjf;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. public class AnotherActivity extends Activity {
  6. private static final String TAG = "AnotherActivity";
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.another);
  11. MyApplication myApplication = (MyApplication) getApplication();
  12. Log.i(TAG, String.valueOf(myApplication.ismHasPassword()));
  13. }
  14. }
main.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <TextView
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="@string/hello" />
  10. <Button
  11. android:id="@+id/button"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. android:text="Start another activity" />
  15. </LinearLayout>
another.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <TextView
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="@string/hello" />
  10. </LinearLayout>
運行一下看看結果。
Copyright © Linux教程網 All Rights Reserved