Android中用Application類實現全局變量

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

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

[java]
package com.tianjf; 
 
import android.app.Application; 
 
public class MyApplication extends Application { 
     
    private boolean mHasPassword; 
 
    public boolean ismHasPassword() { 
        return mHasPassword; 
    } 
 
    public void setmHasPassword(boolean mHasPassword) { 
        this.mHasPassword = mHasPassword; 
    } 
 
    @Override 
    public void onCreate() { 
        mHasPassword = true; 
        super.onCreate(); 
    } 

package com.tianjf;

import android.app.Application;

public class MyApplication extends Application {
 
 private boolean mHasPassword;

 public boolean ismHasPassword() {
  return mHasPassword;
 }

 public void setmHasPassword(boolean mHasPassword) {
  this.mHasPassword = mHasPassword;
 }

 @Override
 public void onCreate() {
  mHasPassword = true;
  super.onCreate();
 }
}
我們定義瞭一個MyApplication繼承自Application,並定義瞭一個全局變量mHasPassword,然後復寫基類的onCreate方法,onCreate負責對所有全局變量賦初期值。

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

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

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

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

ApplicationDemoActivity.java

[java]
package com.tianjf; 
 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
 
public class ApplicationDemoActivity extends Activity implements 
        OnClickListener { 
 
    private static final String TAG = "ApplicationDemoActivity"; 
 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        findViewById(R.id.button).setOnClickListener(this); 
    } 
 
    @Override 
    public void onClick(View v) { 
        switch (v.getId()) { 
        case R.id.button: 
            MyApplication myApplication = (MyApplication) getApplication(); 
            Log.i(TAG, String.valueOf(myApplication.ismHasPassword())); 
            myApplication.setmHasPassword(false); 
 
            Intent intent = new Intent(this, AnotherActivity.class); 
            startActivity(intent); 
            break; 
 
        default: 
            break; 
        } 
    } 

package com.tianjf;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

public class ApplicationDemoActivity extends Activity implements
  OnClickListener {

 private static final String TAG = "ApplicationDemoActivity";

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  findViewById(R.id.button).setOnClickListener(this);
 }

 @Override
 public void onClick(View v) {
  switch (v.getId()) {
  case R.id.button:
   MyApplication myApplication = (MyApplication) getApplication();
   Log.i(TAG, String.valueOf(myApplication.ismHasPassword()));
   myApplication.setmHasPassword(false);

   Intent intent = new Intent(this, AnotherActivity.class);
   startActivity(intent);
   break;

  default:
   break;
  }
 }
}
AnotherActivity.java

[java]
package com.tianjf; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
 
public class AnotherActivity extends Activity { 
 
    private static final String TAG = "AnotherActivity"; 
     
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.another); 
        MyApplication myApplication = (MyApplication) getApplication(); 
        Log.i(TAG, String.valueOf(myApplication.ismHasPassword())); 
    } 

package com.tianjf;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class AnotherActivity extends Activity {

 private static final String TAG = "AnotherActivity";
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.another);
  MyApplication myApplication = (MyApplication) getApplication();
  Log.i(TAG, String.valueOf(myApplication.ismHasPassword()));
 }
}
main.xml[html]
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
    <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello" /> 
 
    <Button 
        android:id="@+id/button" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Start another activity" /> 
 
</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Start another activity" />

</LinearLayout>
another.xml[html] view plaincopyprint?<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
    <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello" /> 
 
</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>
運行一下看看結果。

 

 作者:tianjf0514
 

 

發佈留言