Laidian Butler for android projects (7) and android butler

Source: Internet
Author: User

Laidian Butler for android projects (7) and android butler

Because we use SharedPreferences to store user setting information, some default settings will be made when we first enter the telephone Butler, and the content of the Setting information file will be updated based on the user's settings.

To open the application, start ActivityGroupDemo and display the blacklist page by default. In this case, you must go to ActivityGroupDemo. add code in java to determine whether the user enters the application for the first time. If this is the first time, write the default settings.

Here, we will determine whether the user entered the application for the first time. The idea is as follows:

1) Because SharedPreferences will generate files in the shared_prefs/folder under the application directory.

2) each time you enable the program, you can determine whether the generated file exists. If it does not exist, the user enters

3) Write related default settings

Add the following in ActivityGroupDemo:

private SharedPreferences spf;
Spf = this. getSharedPreferences ("setting", Activity. MODE_PRIVATE); SharedPreferences. editor editor = spf. edit (); // retrieve the path of the current application file String path = this. getFilesDir (). toString (); // find shared_prefs/setting. xmlpath = path. substring (0, path. lastIndexOf ("/") + 1); path + = "shared_prefs/setting. xml "; // determine whether the program is started for the first time by checking whether the File exists. if it is started for the first time, write the default value file = new File (path); if (! File. exists () {// put the data into sharedPreferences // The Listener editor is enabled by default. putBoolean ("isStartListen", false); // The default value is editor in whitelist mode. putBoolean ("isWhiteList", false); // The time period editor is disabled by default. putBoolean ("isTime", false); editor. putString ("startTime", null); editor. putString ("endTime", null); // submit the current data editor. commit ();}

With the code above, you can determine whether the user has entered the application for the first time.

Imagine if you do not save the user's settings, the next time the user enters the program and opens the settings page, the default settings defined in the program are displayed, instead of setting the user in the application last time, in order to implement the program settings will change with the user settings, you need to read the user's previous settings every time you enter the setting interface, configure the settings on the settings page.

Main ideas:

1. Read the user setting information file in the oncreate method of the Activity setting interface.

2. Load the user settings file to the settings page.

Add the following code to SettingActivity. java:

Protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); setContentView (R. layout. activity_setting );...... LoadSetting ();} // read the setting information, load it to the public void loadSetting () {// retrieve the Setting data, and set the corresponding button to switch loadSettingImage (tb_switch, sp. getBoolean ("isStartListen", false); loadSettingImage (tb_whiteList, sp. getBoolean ("isWhiteList", false); loadSettingImage (tb_time, sp. getBoolean ("isTime", false); values (new ToggleButtonCheckedEvent ());} // configure the control to display 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 ;}}

In this way, the application settings will change with the user settings.

In the previous section, we have implemented the rule-based phone Interception Function. When will the interception start, and when will the interception be stopped. Enable interception in the first item on the settings page. Now, enable and disable the service.

To start a service, you must use Intent and set Intent action. Add the following to the oncreate function of SettingActivity. java:

Intent intent;
intent = new Intent("com.example.callmanager.ListenService");

When the listener is enabled, that is, when the ToggleButton is selected, start the service and add the following code:

// Enable the Service getApplicationContext (). startService (intent );

When the listener is closed, that is, when the ToggleButton is unselected, stop the service and add the following code:

// Disable the getApplicationContext (). stopService (intent) service );


At this point, the user settings are loaded and the listener service is enabled.




The android project has less android-support-v7-appcompatjar Problems

Unable to resolve target 'android-16 '. This is because android-16 is not downloaded from your sdk manager. Change project. properties to android-19.
Import support v7 library: file-import-existing android code into workspace, select sdk/extras/support/v7/appcompat, and click OK.

 
Android Project Creation Problems

I used to create a new project, and the R file will disappear. Only later will I find that the sdk and the adt version do not match, and you can download them all later.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.