Since we are using Sharedpreferences to store user settings information, there will be some default settings for the first time you enter the call Butler, and then update the contents of the settings information file according to the user's settings.
Open the application, first started is Activitygroupdemo, and the default display blacklist interface, at this time, you need to add code in Activitygroupdemo.java, to determine whether the user is the first time to enter the app, if it is the first time, write the default settings information.
This is to determine whether the user is the first time to enter the application, the idea is:
1) because sharedpreferences will generate files under the Shared_prefs/folder in the application directory.
2) Each time you enable the program, determine whether the build file exists, if it does not exist, it means that the user is the first time to enter
3) write the relevant default settings information
Add in Activitygroupdemo:
Private Sharedpreferences SPF;
SPF = this.getsharedpreferences ("setting", activity.mode_private); Sharedpreferences.editor Editor = Spf.edit ();//Check out the current application file path String path = This.getfilesdir (). toString ();//Find Shared_ Prefs/setting.xmlpath = path.substring (0, Path.lastindexof ("/") +1);p ath + = "shared_prefs/setting.xml";//the use of files exists, Determines whether the program is first started, and if it is first started, writes the default value file = New file (path), if (!file.exists ()) {//data is placed in sharedpreferences// Listening Editor.putboolean is enabled by default ("Isstartlisten", false);//default is Whitelist mode Editor.putboolean ("Iswhitelist", false);//default time period is not enabled Editor.putboolean ("Istime", false); Editor.putstring ("StartTime", null); Editor.putstring ("EndTime", null);// Submit Current Settings Data Editor.commit ();}
With these lines of code, you can tell if a user is entering the application for the first time.
Imagine, if you do not save the user's settings information, then the next time the user enters the program, open the settings interface, the display is definitely the default settings defined in the program, not the user last in the app settings, in order to implement the program settings will vary with the user settings, you need to enter the Setup interface each time, Reads the user's last settings information and configures the setup information in the settings interface.
Main ideas:
1. Implement the Read user settings information file in the OnCreate method of setting interface activity
2. Load the Read user settings information file into the settings screen
Add the following code to the Settingactivity.java:
protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_setting); loadsetting (); Read the settings information and load the settings interface into the public void loadsetting () {//Remove settings data, set the corresponding button switch 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 ());} Configure control to display public void Loadsettingimage (View v,boolean isChecked) {switch (V.getid ()) {Case R.id.tb_switch:tb_ according to settings information 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;}}
In this way, the settings information for the app changes as the user sets it.
The previous section has implemented the phone's rule interception function, then when to start interception, and when to stop the interception. In the first item of the setup interface, enable interception, see right here, turn the service on and off.
To start a service, of course you need to use the intent, and you have to set the intent action, in Settingactivity.java's OnCreate function added:
Intent Intent;
Intent = new Intent ("Com.example.callmanager.ListenService");
When listening is enabled, that is, when ToggleButton is selected, start the service and add the following code:
Enable service Getapplicationcontext (). StartService (Intent);
When listening is turned off, that is, when ToggleButton is unchecked, stop the service and add the following code:
Close service Getapplicationcontext (). StopService (Intent);
At this point, the user settings are implemented with load and the listening service is enabled
Android Project Call Butler (7)-----Load user settings and start listening service