【邊做項目邊學Android】手機安全衛士10-設定嚮導之綁定SIM卡,android10-
上回主要做了設定嚮導介面的介面設計,主要涉及到介面的布局和一些控制項的使用。這次要做設定嚮導介面的功能具體實現。
首先,4個介面分別是(重複度很大,這裡就不再貼到本文中了)
Activity之間的切換動畫效果
- public void overridePendingTransition(int enterAnim, int exitAnim)
兩個參數:
- enterAnim:進入新Activity的動畫效果
- exitAnim:退出當前Activity的動畫效果
建立動畫效果:
- /mobilesafe/res/anim/alpha_in.xml
<?xml version="1.0" encoding="utf-8"?><alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:fromAlpha="0.0" android:toAlpha="1.0" ></alpha>
- /mobilesafe/res/anim/alpha_out.xml
為“下一步”按鈕添加點擊事件:
- /mobilesafe/src/com/liuhao/mobilesafe/ui/SetupWizard1Activity.java
package com.liuhao.mobilesafe.ui;import com.liuhao.mobilesafe.R;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class SetupWizard1Activity extends Activity implements OnClickListener { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.setup_wizard1); button = (Button) this.findViewById(R.id.bt_next); button.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_next: finish();// 使用者點擊“後退”時不會再看到這個介面 Intent intent = new Intent(this, SetupWizard2Activity.class); startActivity(intent); // 設定Activity切換時的動畫效果 overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out); break; } }}
設定界嚮導面2
需求功能: 1. 綁定SIM卡 2. CheckBox狀態的處理 3. 上一步、下一步 點擊功能的實現
在使用者點擊“綁定SIM卡”時,觸發相應的處理邏輯,擷取當前SIM卡的串號,並將串號存取到SharePreference中。
要擷取手機SIM卡串號,需要添加許可權:android.permission.READ_PHONE_STATE
/** * 綁定SIM串號 * */private void setSimInfo() { TelephonyManager manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); String simSerialNumber = manager.getSimSerialNumber(); Editor edit = sp.edit(); edit.putString("sim", simSerialNumber); edit.commit(); Toast.makeText(getApplicationContext(), "SIM卡已綁定", Toast.LENGTH_SHORT).show();}
// 首先初始化chexkbox的狀態String sim = sp.getString("sim", null);if(sim != null){ cb_bind.setText("已綁定SIM卡"); cb_bind.setChecked(true);}else{ cb_bind.setText("未綁定SIM卡"); cb_bind.setChecked(false);}cb_bind.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ setSimInfo(); cb_bind.setText("已綁定SIM卡"); }else{ cb_bind.setText("未綁定SIM卡"); } }});
異常處理
弄好,運行代碼,綁定手機SIM卡串號,沒有問題。
再次開啟,進入嚮導2介面時,出錯,程式崩潰。
錯誤記錄檔(摘取了主要部分)E/AndroidRuntime(26463): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.liuhao.mobilesafe/com.liuhao.mobilesafe.ui.SetupWizard2Activity}: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
...
E/AndroidRuntime(26463): at com.liuhao.mobilesafe.ui.SetupWizard2Activity.onCreate(SetupWizard2Activity.java:42)原因:
由於之前判斷SharePreference中是否存在SIM資訊是根據下面的邏輯:
// 首先初始化chexkbox的狀態if(sp.getBoolean("sim", false)){ cb_bind.setText("已綁定SIM卡"); cb_bind.setChecked(true);}else{ cb_bind.setText("未綁定SIM卡"); cb_bind.setChecked(false);}
而boolean android.content.SharedPreferences.getBoolean(String key, boolean defValue)方法,
Retrieve a boolean value from the preferences.
Parameters: key The name of the preference to retrieve. defValue Value to return if this preference does not exist. Returns: Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a boolean. Throws: ClassCastException
可以發現,若已存在值,而這個值不是Boolean類型時將會拋出ClassCastException。
修改
// 首先初始化chexkbox的狀態String sim = sp.getString("sim", null);if(sim != null){ cb_bind.setText("已綁定SIM卡"); cb_bind.setChecked(true);}else{ cb_bind.setText("未綁定SIM卡"); cb_bind.setChecked(false);}
運行效果
完整代碼:
- /mobilesafe/src/com/liuhao/mobilesafe/ui/SetupWizard2Activity.java