Android中用Application類實現全域變數

來源:互聯網
上載者:User

在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
 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.