android項目 之 來電管家(7),android管家
因為我們用的是SharedPreferences來儲存使用者佈建資訊,那麼在初次進入來電管家時,會有一些預設的設定,後續根據使用者的設定,更新設定資訊檔的內容。
開啟應用程式,首先啟動的是ActivityGroupDemo,並預設顯示黑名單介面,這時,需要在ActivityGroupDemo.java中添加代碼,判斷使用者是否是第一次進入該應用,如果是第一次,則寫入預設的設定資訊。
這裡判斷使用者是否為第一次進入該應用程式,採用的思路為:
1)因為SharedPreferences會在應用程式目錄下的shared_prefs/檔案夾下組建檔案。
2)每次啟用程式時,判斷該組建檔案是否存在,如果不存在,則表示使用者是第一次進入
3) 寫入相關的預設設定資訊
在ActivityGroupDemo裡添加:
private SharedPreferences spf;
spf = this.getSharedPreferences("setting", Activity.MODE_PRIVATE);SharedPreferences.Editor editor = spf.edit();//取出當前應用檔案路徑 String path = this.getFilesDir().toString();//找到shared_prefs/setting.xmlpath = path.substring(0, path.lastIndexOf("/")+1);path += "shared_prefs/setting.xml";//利用檔案是否存在,判斷程式是否為第一次啟動,如果是第一次啟動,則寫入預設值 File file = new File(path);if(!file.exists()){//將資料放入sharedPreferences中 //預設啟用監聽editor.putBoolean("isStartListen", false);//預設是白名單模式 editor.putBoolean("isWhiteList", false);//預設不啟用時間段 editor.putBoolean("isTime", false);editor.putString("startTime", null);editor.putString("endTime", null);//提交當前設定資料editor.commit();}
通過上述幾行代碼,即可判斷使用者是否是第一次進入應用程式。
試想一下,如果不儲存使用者的設定資訊,那麼下次使用者進入程式進,開啟設定介面,顯示的肯定是程式內定義的預設設定,而不是使用者上次在應用中的設定,為了實現程式的設定會隨著使用者佈建的不同而變化,就需要在每次進入設定介面時,讀取使用者的上次的設定資訊,並將設定資訊配置在設定介面中。
主要思路:
1. 在設定介面Activity的oncreate方法裡實現讀取使用者佈建資訊檔
2. 將讀取到的使用者佈建資訊檔載入到設定介面中
在SettingActivity.java中添加如下代碼:
protected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_setting);……loadSetting();}//讀取設定資訊,並載入到設定介面中public void loadSetting(){//取出設定資料,設定相應的按鈕開關 loadSettingImage(tb_switch,sp.getBoolean("isStartListen", false));loadSettingImage(tb_whiteList,sp.getBoolean("isWhiteList", false));loadSettingImage(tb_time,sp.getBoolean("isTime", false));tb_switch.setOnCheckedChangeListener(new ToggleButtonCheckedEvent());tb_whiteList.setOnCheckedChangeListener(new ToggleButtonCheckedEvent());tb_time.setOnCheckedChangeListener(new ToggleButtonCheckedEvent());}//根據設定資訊配置控制項顯示public void loadSettingImage(View v,boolean isChecked){switch(v.getId()){case R.id.tb_switch :tb_switch.setChecked(isChecked);if(isChecked){tb_switch.setBackgroundResource(R.drawable.start_service_on);getApplicationContext().startService(intent);}elsetb_switch.setBackgroundResource(R.drawable.start_service_off);break;case R.id.tb_whitelist :tb_whiteList.setChecked(isChecked);if(isChecked)tb_whiteList.setBackgroundResource(R.drawable.start_service_on);elsetb_whiteList.setBackgroundResource(R.drawable.start_service_off);break; case R.id.tb_time :tb_time.setChecked(isChecked);if(isChecked){tb_time.setBackgroundResource(R.drawable.start_service_on);start_layout.setClickable(true);end_layout.setClickable(true);tv_start_tip.setTextColor(Color.BLACK);tv_start_time.setTextColor(Color.BLACK);tv_end_tip.setTextColor(Color.BLACK);tv_end_time.setTextColor(Color.BLACK);tv_start_time.setText(sp.getString("startTime", ""));tv_end_time.setText(sp.getString("endTime", ""));}else{tb_time.setBackgroundResource(R.drawable.start_service_off);tv_start_tip.setTextColor(Color.GRAY);tv_start_time.setTextColor(Color.GRAY);tv_end_tip.setTextColor(Color.GRAY);tv_end_time.setTextColor(Color.GRAY);start_layout.setClickable(false);end_layout.setClickable(false);String[] time = getCurrentTime();tv_start_time.setText(time[0]);tv_end_time.setText(time[1]);}break;}}
這樣,應用的設定資訊就會隨著使用者的設定而變化。
上一節已實現了電話的按規則攔截功能,那麼什麼時候開始攔截,又什麼時候停止攔截呢。在設定介面的第一項,啟用攔截,看到了吧,就在這裡啟用與關閉服務。
要啟動一個服務,當然要用到了Intent了,並且還得設定Intent的action了,在SettingActivity.java的oncreate函數中添加:
Intent intent;
intent = new Intent("com.example.callmanager.ListenService");
當啟用監聽時,也就是ToggleButton呈選中狀態時,啟動該服務,添加如下代碼:
//啟用服務getApplicationContext().startService(intent);
當關閉監聽時,也就是ToggleButton呈未選中狀態時,停止該服務,添加如下代碼:
//關閉服務getApplicationContext().stopService(intent);
至此,就實現了用載入使用者佈建並啟用監聽服務
android 項目少了 android-support-v7-appcompatjar 的問題
Unable to resolve target 'android-16'。這是因為你的sdk manager裡面沒有下載android-16的,把project.properties改成android-19。
匯入support v7 library:file-import-existing android code into workspace,選擇sdk/extras/support/v7/appcompat,點擊確定。
android項目建立問題
我之前是建立一個項目R檔案都會消失,後面才發現是sdk和adt版本沒有對應,後面下載全這兩個就可以了。