- Android 基礎
- Android - 首頁
- Android - 概覽
- Android - 環境設定
- Android - 架構
- Android - 應用元件
- Android - Hello World 示例
- Android - 資源
- Android - 活動 (Activity)
- Android - 服務 (Service)
- Android - 廣播接收器 (Broadcast Receiver)
- Android - 內容提供器 (Content Provider)
- Android - 碎片 (Fragment)
- Android - 意圖/過濾器 (Intent/Filter)
- Android - 使用者介面
- Android - UI 佈局
- Android - UI 控制元件
- Android - 事件處理
- Android - 樣式和主題
- Android - 自定義元件
- Android 高階概念
- Android - 拖放
- Android - 通知
- 基於位置的服務
- Android - 傳送郵件
- Android - 傳送簡訊
- Android - 電話呼叫
- 釋出 Android 應用
- Android 實用示例
- Android - 警報對話方塊
- Android - 動畫
- Android - 音訊捕獲
- Android - AudioManager
- Android - 自動完成
- Android - 最佳實踐
- Android - 藍牙
- Android - 相機
- Android - 剪貼簿
- Android - 自定義字型
- Android - 資料備份
- Android - 開發者工具
- Android - 模擬器
- Android - Facebook 整合
- Android - 手勢
- Android - Google 地圖
- Android - 圖片特效
- Android - ImageSwitcher
- Android - 內部儲存
- Android - JetPlayer
- Android - JSON 解析器
- Android - Linkedin 整合
- Android - 載入 Spinner
- Android - 本地化
- Android - 登入介面
- Android - MediaPlayer
- Android - 多點觸控
- Android - 導航
- Android - 網路連線
- Android - NFC 指南
- Android - PHP/MySQL
- Android - 進度圓圈
- Android - 進度條
- Android - 推送通知
- Android - RenderScript
- Android - RSS 閱讀器
- Android - 螢幕錄製
- Android - SDK 管理器
- Android - 感測器
- Android - 會話管理
- Android - 共享首選項
- Android - SIP 協議
- Android - 拼寫檢查器
- Android - SQLite 資料庫
- Android - 支援庫
- Android - 測試
- Android - 文字轉語音
- Android - TextureView
- Android - Twitter 整合
- Android - UI 設計
- Android - UI 模式
- Android - UI 測試
- Android - WebView 佈局
- Android - Wi-Fi
- Android - 小部件
- Android - XML 解析器
- Android 實用資源
- Android - 問答
- Android - 實用資源
- Android - 討論
Android - 會話管理
當您想將使用者資料儲存在應用程式外部時,會話會幫助您,以便下次使用者使用您的應用程式時,您可以輕鬆地獲取他的詳細資訊並相應地執行操作。
這可以透過多種方式完成。但是,最簡單也是最不錯的方法是透過**共享首選項**。
共享首選項
共享首選項允許您以鍵值對的形式儲存和檢索資料。為了使用共享首選項,您必須呼叫一個名為 getSharedPreferences() 的方法,該方法返回一個指向包含首選項值的檔案的 SharedPreference 例項。
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
您可以使用 SharedPreferences.Editor 類將某些內容儲存到共享首選項中。您將呼叫 SharedPreference 例項的 edit 方法,並將其接收在編輯器物件中。其語法如下:
Editor editor = sharedpreferences.edit();
editor.putString("key", "value");
editor.commit();
除了 putString 方法外,編輯器類中還提供了其他方法,允許操作共享首選項中的資料。它們列出如下:
| 序號 | 模式及描述 |
|---|---|
| 1 |
apply() 這是一個抽象方法。它將把您的更改從編輯器提交回您呼叫的 SharedPreference 物件。 |
| 2 |
clear() 它將刪除編輯器中的所有值。 |
| 3 |
remove(String key) 它將刪除鍵作為引數傳遞的值。 |
| 4 |
putLong(String key, long value) 它將在首選項編輯器中儲存一個長整型值。 |
| 5 |
putInt(String key, int value) 它將在首選項編輯器中儲存一個整型值。 |
| 6 |
putFloat(String key, float value) 它將在首選項編輯器中儲存一個浮點型值。 |
透過共享首選項進行會話管理
為了從共享首選項執行會話管理,我們需要在**onResume**方法中檢查儲存在共享首選項中的值或資料。如果我們沒有資料,我們將從頭開始應用程式,因為它新安裝。但如果我們獲得了資料,我們將從使用者離開的地方開始。這在下面的示例中進行了演示:
示例
以下示例演示了會話管理的使用。它建立一個基本的應用程式,允許您首次登入。然後,如果您退出應用程式而沒有登出,則如果再次啟動應用程式,您將被帶回同一位置。但是,如果您從應用程式登出,則您將被帶回主登入螢幕。
要體驗此示例,您需要在實際裝置或模擬器上執行它。
| 步驟 | 描述 |
|---|---|
| 1 | 您將使用 Android Studio IDE 在包 com.example.sairamkrishna.myapplication 下建立一個 Android 應用程式。 |
| 2 | 修改 src/MainActivity.java 檔案以新增進度程式碼以新增會話程式碼。 |
| 3 | 建立新的 Activity 並將其命名為 second.java。編輯此檔案以新增進度程式碼以新增會話程式碼。 |
| 4 | 修改 res/layout/activity_main.xml 檔案以新增相應的 XML 程式碼。 |
| 5 | 修改 res/layout/second_main.xml 檔案以新增相應的 XML 程式碼。 |
| 7 | 執行應用程式並選擇正在執行的 Android 裝置,並將應用程式安裝在其上並驗證結果。 |
以下是**MainActivity.java**的內容。
package com.example.sairamkrishna.myapplication;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText ed1,ed2,ed3;
Button b1;
Intent in;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String Name = "nameKey";
public static final String Phone = "phoneKey";
public static final String Email = "emailKey";
SharedPreferences sharedpreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1=(EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
ed3=(EditText)findViewById(R.id.editText3);
b1=(Button)findViewById(R.id.button);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String n = ed1.getText().toString();
String ph = ed2.getText().toString();
String e = ed3.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Name, n);
editor.putString(Phone, ph);
editor.putString(Email, e);
editor.commit();
in = new Intent(MainActivity.this,second_main.class);
startActivity(in);
}
});
}
}
以下是**second_main.java**的內容。
package com.example.sairamkrishna.myapplication;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class second_main extends Activity {
Button bu=null;
Button bu2=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_main);
bu=(Button)findViewById(R.id.button2);
bu2=(Button)findViewById(R.id.button3);
}
public void logout(View view){
SharedPreferences sharedpreferences = getSharedPreferences(MainActivity.MyPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.clear();
editor.commit();
}
public void close(View view){
finish();
}
}
以下是**activity_main.xml**的內容。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Shared Preference"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="35dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials Point"
android:id="@+id/textView2"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:textSize="35dp"
android:textColor="#ff16ff01" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_below="@+id/textView2"
android:layout_marginTop="67dp"
android:hint="Name"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:layout_below="@+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Pass" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText3"
android:layout_below="@+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Email" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login"
android:id="@+id/button"
android:layout_below="@+id/editText3"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
</RelativeLayout>
以下是**second_main.xml**的內容。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Logout"
android:onClick="logout"
android:id="@+id/button2"
android:layout_gravity="center_horizontal"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="191dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close"
android:onClick="close"
android:id="@+id/button3"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginTop="69dp" />
</RelativeLayout>
以下是**Strings.xml**的內容。
<resources> <string name="app_name">My Application</string> </resources>
以下是**AndroidManifest.xml**的內容。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sairamkrishna.myapplication" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".second"></activity>
</application>
</manifest>
讓我們嘗試執行您的應用程式。我假設您在進行環境設定時建立了您的 AVD。要從 Android Studio 執行應用程式,請開啟您的專案之一的活動檔案,然後單擊工具欄中的執行
圖示。Android Studio 將應用程式安裝到您的 AVD 上並啟動它,如果您的設定和應用程式一切正常,它將顯示以下模擬器視窗:
輸入您的使用者名稱和密碼**(輸入任何您喜歡的內容,但請記住您輸入的內容)**,然後單擊登入按鈕。如下面的圖片所示:
單擊登入按鈕後,您將進入此歡迎螢幕。現在您的登入資訊儲存在共享首選項中。
現在單擊**退出而不登出**按鈕,您將返回主螢幕,並且首選項檔案輸出將如下面的圖片所示
如果您將 myPref.xml 檔案作為記事本檔案開啟,它將如下所示
如果您單擊登出按鈕,它將擦除首選項值。如果您輸入了不同的值作為輸入,它將把這些值作為 XML 中的首選項輸入。
