android项目 之 来电管家(7) ----- 加载用户设置并启监听用服务

        因为我们用的是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.xml
		path = 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 stub
		super.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);
			}
			else
				tb_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);
			else
				tb_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);


          至此,就实现了用加载用户设置并启用监听服务

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值