Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android項目 之 來電管家(4) ----- 添加攔截時間段

android項目 之 來電管家(4) ----- 添加攔截時間段

編輯:關於Android編程

在大多數的騷擾攔截類的軟件中都會有定時攔截這個實用的的功能,其實,也不難實現。

看圖:

\\\

在未選中啟用時間段時,下面的兩個開始時間和結束時間呈灰色狀態,並且單擊無響應,啟用時間段後,下面則變成了可以單擊的狀態,通過單擊可以彈出選擇日期與時間的對話框,用於選擇開始時間和結束時間。

主要思路:<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+ICAgICAgICAgICAgICAgIDEuICDG9NPDyrG85LbOo6y9q8/Cw+a1xL/YvP6x5LPJv8m78bXDvbm149e0zKw8L3A+CjxwPiAgICAgICAgICAgICAgICAyLiAgzO2807Wlu/fKwrz+o6y1r7P2yNXG2sqxvOTRodTxttS7sL/yPC9wPgo8cD4gICAgICAgICAgICAgICAgMy4gINGh1PHKsbzko6yyos/Uyr7U2s/g06a1xM671sM8L3A+CjxwPiAgICAgICAgICAgICAgICA0LiAgzai5/VNoYXJlZFByZWZyZXNlbmNlvavP4LnYyejWw7GjtOajrNLUsePPwrTOxvS2r7PM0PLKscq508M8L3A+CjxwPiA8L3A+CjxwPiAgICAgICAgICDJ6NbDvefD5rXEsry+1s7EvP7S0dTatdrSu73auPiz9sHLo6zPwsPmyrXP1r7NyrXP1r7fzOXM7bzTyrG85LXEuabE3KGjPC9wPgo8cD4gICAgICAgICAgIMrXz8jKx7/YvP61xMn5w/ejrNXiwO/Tw7W9wctUb2dnbGVCdXR0b27AtLHtyr7BvdbW17TMrKOstbHIu8HL0rK/ydPDU3dpdGNoo6y1q8rHMi4zsrvWp7PWoaM8L3A+CjxwcmUgY2xhc3M9"brush:java;"> private ToggleButton tb_switch,tb_whiteList,tb_time; private RelativeLayout start_layout; private RelativeLayout end_layout; private TextView tv_start_time,tv_start_tip; private TextView tv_end_time,tv_end_tip; private SharedPreferences sp; SharedPreferences.Editor editor;

接著,就是從布局文件中找到相應的控件,並綁定監聽器

		sp = this.getSharedPreferences("setting",Activity.MODE_PRIVATE);
		editor = sp.edit();
		
		
		start_layout = (RelativeLayout)findViewById(R.id.start_layout);
		start_layout.setOnClickListener(new ClickEvent());
		start_layout.setClickable(false);
		end_layout = (RelativeLayout)findViewById(R.id.end_layout);
		end_layout.setOnClickListener(new ClickEvent());
		end_layout.setClickable(false);
		
		tv_start_time =(TextView)findViewById(R.id.tv_start_time);
		tv_end_time = (TextView)findViewById(R.id.tv_end_time);
		
		tv_start_tip = (TextView)findViewById(R.id.tv_start_tip);
		tv_end_tip = (TextView)findViewById(R.id.tv_end_tip);
		
		tb_switch = (ToggleButton)findViewById(R.id.tb_switch);
		tb_whiteList = (ToggleButton)findViewById(R.id.tb_whitelist);
		tb_time = (ToggleButton)findViewById(R.id.tb_time);
		tb_switch.setOnCheckedChangeListener(new ToggleButtonCheckedEvent());
   tb_whiteList.setOnCheckedChangeListener(new ToggleButtonCheckedEvent());
   tb_time.setOnCheckedChangeListener(new ToggleButtonCheckedEvent());

其中,sp = this.getSharedPreferences("setting",Activity.MODE_PRIVATE);這個可以將簡單的設置信息保存到文件,注意這裡是對兩個relativeLayout添加單擊事件,這樣,就可以通過點擊設置界面中的兩個時間段選擇行,就可以彈出相應的日期時間選擇對話框。

附: SharedPreferences的用法:

1. 得到SharedPreferences對象:SharedPreferences sp = this.getSharedPreferences("setting",Activity.MODE_PRIVATE);後面的參數,表示模式,表示共享的范圍,可以查看具體文檔。

2. 存入數據:SharedPreferences.Editor editor = sp.edit();

editor.putBoolean("isStartListen",true);

editor.commit();//提交數據,這樣數據就保存到setting.xml文件中了

3. 取出數據:boolean data = sp.getBoolean("isStartListen", false),其中第二個參數false是默認值,也就是如果文件中不存在該key,就返回false。

在監聽器中實現想要的功能了,如下:

	//ToggleButton開關監聽器
	class ToggleButtonCheckedEvent implements OnCheckedChangeListener{
		
		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			if(buttonView.getId() == R.id.tb_switch){
				tb_switch.setChecked(isChecked);
				
				if(isChecked){
					editor.putBoolean("isStartListen",true);
					editor.commit();
					tb_switch.setBackgroundResource(R.drawable.start_service_on);
					Toast.makeText(SettingActivity.this, "開啟攔截服務", 1000	).show();
				}
				else{
					editor.putBoolean("isStartListen",false);
					editor.commit();
					tb_switch.setBackgroundResource(R.drawable.start_service_off);
					Toast.makeText(SettingActivity.this, "關閉攔截服務", 1000	).show();
				}
				
			}
			else if(buttonView.getId() == R.id.tb_whitelist){
				tb_whiteList.setChecked(isChecked);
				if(isChecked){
					editor.putBoolean("isWhiteList",true);
					tb_whiteList.setBackgroundResource(R.drawable.start_service_on);
					Toast.makeText(SettingActivity.this, "白名單模式", 1000	).show();
				}
				else{
					editor.putBoolean("isWhiteList", false);
					tb_whiteList.setBackgroundResource(R.drawable.start_service_off);
					Toast.makeText(SettingActivity.this, "黑名單模式", 1000	).show();
				}
				editor.commit();
				
			}
			else if(buttonView.getId() == 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);
					editor.putBoolean("isTime", true);
					Toast.makeText(SettingActivity.this, "啟用定時攔截,請設置開始時間和結束時間", 1000	).show();
					String[] time = getCurrentTime();
					tv_start_time.setText(time[0]);
					tv_end_time.setText(time[1]);
					editor.putString("startTime", time[0]);
					editor.putString("endTime", time[1]);
				}
				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);
					editor.putBoolean("isTime", false);
					Toast.makeText(SettingActivity.this, "關閉定時攔截", 1000).show();
					String[] time = getCurrentTime();
					tv_start_time.setText(time[0]);
					tv_end_time.setText(time[1]);
				}
				editor.commit();
			}
			
		}
	}

可以看出,監聽器中還有其他兩個ToggleButton需要實現的功能,當然了,因為目前還沒有通過service實現電話攔截的功能,所以,在之後實現攔截功能時,會在相應的地方添加代碼,這裡只實現將設置的信息保存。

下面就實現選擇日期與時間功能,並將其保存。

依然是監聽器:

	class ClickEvent implements OnClickListener{

		@Override
		public void onClick(View v) {
			Context mContext = SettingActivity.this;
			AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
			View view = View.inflate(mContext, R.layout.data_time_dialog, null);
			final DatePicker datePicker  = (DatePicker)view.findViewById(R.id.data_picker);
			final TimePicker timePicker = (TimePicker)view.findViewById(R.id.time_picker);
			builder.setView(view);
			
			//獲取當前日期 
			Calendar cal = Calendar.getInstance();
			cal.setTimeInMillis(System.currentTimeMillis());
			//將日期選擇器設置為當前日期
			datePicker.init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH),null);
			
			//將時間選擇器設置為當前時間
			timePicker.setIs24HourView(true);
			timePicker.setCurrentHour(cal.get(Calendar.HOUR_OF_DAY));
			timePicker.setCurrentMinute(cal.get(Calendar.MINUTE));
			if(v.getId() == start_layout.getId()){
				builder.setTitle("選擇起始監聽時間");
				builder.setPositiveButton("確定 ", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						StringBuffer sb = new StringBuffer();
						//取出選擇的時間,並格式化
						sb.append(String.format("%02d-%02d", datePicker.getMonth()+1,datePicker.getDayOfMonth()));
						//將日期與時間用空格分開 
						sb.append(" ");
						sb.append(String.format("%02d:%02d", timePicker.getCurrentHour(),timePicker.getCurrentMinute()));
						
						
						//將選擇好的日期日間顯示
						tv_start_time.setText(sb);
						//將時間保存到sharedPreference
						editor.putString("startTime", sb.toString());
						editor.commit();
						showTimeErrorTip();
						dialog.cancel();
					}
				});
			}
			else if(v.getId() == end_layout.getId()){
				builder.setTitle("選擇結束監聽時間");
				builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
						StringBuffer sb = new StringBuffer();
						//取出選擇的時間,並格式化
						sb.append(String.format("%02d-%02d", datePicker.getMonth()+1,datePicker.getDayOfMonth()));
						//將日期與時間用空格分開 
						sb.append(" ");
						sb.append(String.format("%02d:%02d", timePicker.getCurrentHour(),timePicker.getCurrentMinute()));
						
						//將選擇好的日期日間顯示
						tv_end_time.setText(sb);
						editor.putString("endTime", sb.toString());
						editor.commit();
						showTimeErrorTip();
						dialog.cancel();
					}
				});
			}
			
			//創建對話框,並顯示 
			Dialog dialog = builder.create();
			dialog.show();
			
		}
	}

	//刷新存儲時間,判斷開始時間與結束時間大小
	public void showTimeErrorTip(){
		//如果時間合法,則不提示,否則提示
		if(sp.getString("startTime", "").compareTo(sp.getString("endTime", "")) > 0){
			Toast.makeText(SettingActivity.this, "結束時間必須大於開始時間", 1000).show();
		}
	}

彈出的日期時間選擇界面的布局文件如下:

data_time_dialog.xml



    
    
    
    
        
    


至此,已實現了選擇日期時間,並將相關的設置信息寫入文件的功能。





  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved