在Android下有類似於session的東西,叫做Application, getApplication()用法

來源:互聯網
上載者:User

標籤:

在Android上有類似於session的東西,叫做Application。
在Android上有類似於session的東西,叫做Application。

1、你可以建立一個類,例如:HelloWordApplication.java 必須extends Application(名字隨你取)
在這個類裡面設定你要全域的資料變數,例如:private String loginName;
然後產生它的get、set方法。
2、在AndroidManifest.xml檔案中配置你的Application類,方法如下:
<application android:icon="@drawable/icon"  android:label="@string/app_name" android:name="HelloWordApplication">
        <activity >
             //略
        </activity>
    </application>
就是在<application>標籤兒中增加android:name="HelloWordApplication"的屬性配置。
3、在使用的時候:
HelloWordApplicationapplication = (HelloWordApplication)getApplication();
application.setLoginName("百度");
則就將"百度"儲存到了Application裡,其他地方要用的時候,application.getLoginName();就可以了。

 

android getApplication()使用

 

Java代碼  
  1. package com.hyzing;  
  2.   
  3. import android.app.Application;  
  4.   
  5. public class MySystemAppcation extends Application{  
  6.     private int curIndex;  
  7.   
  8.     public int getCurIndex() {  
  9.         return curIndex;  
  10.     }  
  11.   
  12.     public void setCurIndex(int curIndex) {  
  13.         this.curIndex = curIndex;  
  14.     }  
  15.   
  16.     @Override  
  17.     public void onCreate() {  
  18.         super.onCreate();  
  19.     }  
  20.   
  21.     @Override  
  22.     public void onTerminate() {  
  23.         super.onTerminate();  
  24.     }  
  25. }  
Java代碼  
  1. package com.hyzing;  
  2. import android.app.Activity;  
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6.   
  7. public class SystemtestActivity extends Activity {  
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         //setContentView(R.layout.main);      
  12.         MySystemAppcation application = (MySystemAppcation) this  
  13.                 .getApplication();  
  14.         System.out.println("++++++++++++++"+application);  
  15.         Log.i("data", "===============" + application.getCurIndex());  
  16.         application.setCurIndex(5);  
  17.         Intent intent = new Intent();  
  18.         Bundle bundle = new Bundle();  
  19.         bundle.putString("checkIn", "0");  
  20.         bundle.putBoolean("managerUser", true);  
  21.         intent.putExtras(bundle);  
  22.         intent.setClass(<span style="color: #ff0000;">SystemtestActivity.this</span>, PrintActivity.class);  
  23.         startActivity(intent);  
  24.           
  25.     }  
  26. }  
Java代碼  
  1. package com.hyzing;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.util.Log;  
  7.   
  8. public class PrintActivity extends Activity {  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         <span style="color: #ff0000;">super.onCreate(savedInstanceState);</span>  
  13.         MySystemAppcation application = (MySystemAppcation) this  
  14.                 .getApplication();  
  15.         Log.i("data", "" + application.getCurIndex());  
  16.         application.setCurIndex(6);  
  17.         Intent intent = new Intent();  
  18.         Bundle bundle = new Bundle();  
  19.         bundle.putString("checkIn", "0");  
  20.         bundle.putBoolean("managerUser", true);  
  21.         intent.putExtras(bundle);  
  22.         intent.setClass(PrintActivity.this, PrintAgainActivity.class);  
  23.         startActivity(intent);  
  24.     }  
  25. }  
 Java代碼  
  1. package com.hyzing;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6.   
  7. public class PrintAgainActivity extends Activity {  
  8.   
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         <span style="color: #ff0000;">super.onCreate(savedInstanceState);</span>  
  12.         final MySystemAppcation application = (MySystemAppcation) this  
  13.                 .getApplication();  
  14.         Log.i("data", "" + application.getCurIndex());  
  15.   
  16.     }  
  17.   
  18. }  
 Java代碼  
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     package="com.hyzing"  
    4.     android:versionCode="1"  
    5.     android:versionName="1.0" >  
    6.   
    7.     <uses-sdk android:minSdkVersion="15" />  
    8.     <application android:name=".MySystemAppcation"  
    9.         android:icon="@drawable/ic_launcher"  
    10.         android:label="@string/app_name" >  
    11.         <activity  
    12.             android:name=".SystemtestActivity"  
    13.             android:label="@string/app_name" >  
    14.             <intent-filter>  
    15.                 <action android:name="android.intent.action.MAIN" />  
    16.                 <category android:name="android.intent.category.LAUNCHER" />  
    17.             </intent-filter>  
    18.         </activity>  
    19.         <activity  
    20.             android:name=".PrintActivity"  
    21.             android:label="@string/app_name" >              
    22.         </activity>  
    23.         <activity  
    24.             android:name=".PrintAgainActivity"  
    25.             android:label="@string/app_name" >              
    26.         </activity>  
    27.           
    28.     </application>  
    29.   
    30. </manifest>  

在Android下有類似於session的東西,叫做Application, getApplication()用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.