Android Project Call Butler (4)-----Add intercept time period

Source: Internet
Author: User

In most of the harassment interception software will have timed interception of this practical function, in fact, it is not difficult to achieve.

Look at the picture:

When the enable time period is unchecked, the following two start and end times are grayed out, and when you click No response, when the time period is enabled, the following becomes a status that you can click, and you can click to pop up a dialog box that selects the date and time to select the start time and end time.

Main ideas:

1. Enable the time period to turn the following control into a focusable state

2. Add Click event, pop-up Date Time selection dialog box

3. Select the time and display it in the appropriate location

4. Save the relevant settings through sharedprefresence so that you can use the next time you start the program

Setting the layout of the interface file is given in the first section, the following implementation of the implementation of the specific time to add the function.

The first is the declaration of the control, where ToggleButton is used to represent the two states, of course, switch is also available, but 2.3 is not supported.

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;

Next, locate the appropriate control from the layout file and bind the listener

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 ()); <br abp= "830"/> tb_ Whitelist.setoncheckedchangelistener (New Togglebuttoncheckedevent ()); <br abp= "831"/> Tb_ Time.setoncheckedchangelistener (New Togglebuttoncheckedevent ());

where sp = this.getsharedpreferences ("setting", activity.mode_private); This can save simple setup information to a file, Note that this is to add a click event to the two relativelayout, so that you can select the row by clicking on the two time periods in the settings screen to pop up the appropriate DateTime selection dialog box.

Attached: Usage of sharedpreferences:

1. Get Sharedpreferences object: sharedpreferences sp = this.getsharedpreferences ("setting", Activity.mode_private), subsequent parameters, Represents a pattern that represents a shared scope and can view specific documents.

2. Deposit Data: Sharedpreferences.editor Editor = Sp.edit ();

Editor.putboolean ("Isstartlisten", true);

Editor.commit ();//Submit the data so that the data is saved to the Setting.xml file

3. Remove the data: Boolean data = Sp.getboolean ("Isstartlisten", false), where the second parameter false is the default value, that is, if the key does not exist in the file, it returns false.

The desired functionality is implemented in the listener, as follows:

ToggleButton switch Listener class Togglebuttoncheckedevent implements oncheckedchangelistener{@Overridepublic 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, "unblock service", +). Show (); Else{editor.putboolean ("Isstartlisten", false); Editor.commit (); Tb_switch.setbackgroundresource (R.drawable.start _service_off); Toast.maketext (Settingactivity.this, "close interception service", +). 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, "White list mode", +). Show (); Else{editor.putboolean ("Iswhitelist", false); Tb_whitelist.setbackgroundresource (R.drawable.start_service_off); Toast.makeText (Settingactivity.this, "blacklist mode", +). 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, "Enable timed intercept, set start time and end time,"). 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, "Turn off timed intercept", +). Show (); String[] Time = GetCurrentTime (); Tv_start_time.settext (time[0]); Tv_end_time.settext (time[1]);} Editor.commit ();}}}

As you can see, there are two other features in the listener that need to be implemented, of course, because there is no service to implement the phone interception function, so after the interception function, the corresponding place to add code, here only to save the information set.

Here's how to select the Date and Time feature and save it.

Still a listener:

Class Clickevent implements onclicklistener{@Overridepublic 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);//Gets the current date Calendar cal = Calendar.getinstance (); Cal.settimeinmillis (System.currenttimemillis ( );//Set the date selector to the current date Datepicker.init (Cal.get (calendar.year), Cal.get (Calendar.month), Cal.get (calendar.day_of_month ), NULL),//sets the time selector to the current time 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 ( "Select Start listening Time"); Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick ( Dialoginterface dialog, intwhich) {StringBuffer sb = new StringBuffer ();//Remove the selected time and format sb.append (String.Format ("%02d-%02d", Datepicker.getmonth ( +1,datepicker.getdayofmonth ()));//The date and time are separated by a space Sb.append (""); Sb.append (String.Format ("%02d:%02d", Timepicker.getcurrenthour (), Timepicker.getcurrentminute ()));//Will select a good date for daytime display tv_start_time.settext (SB);// Save time to Sharedpreferenceeditor.putstring ("StartTime", sb.tostring ()); Editor.commit (); Showtimeerrortip (); Dialog.cancel ();}});} else if (v.getid () = = End_layout.getid ()) {Builder.settitle ("Select End Listening Time") Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {StringBuffer SB = New StringBuffer ();//Remove the selected time and format the Sb.append (String.Format ("%02d-%02d", Datepicker.getmonth () +1, Datepicker.getdayofmonth ()));//The date and time are separated by a space Sb.append (""); Sb.append (String.Format ("%02d:%02d", Timepicker.getcurrenthour (), Timepicker.getcurrentminute ()));//Will select a good date for daytime display of Tv_end_time.settext (SB); Editor.putstring ("EndTime", sb.tostring ()); editor.cOmmit (); Showtimeerrortip ();d ialog.cancel ();}});} Creates a dialog box and displays Dialog Dialog = builder.create ();d ialog.show ();}}

Refresh storage time, determine the start time and end time size public void Showtimeerrortip () {///If the time is valid, do not prompt, otherwise prompt if (sp.getstring ("StartTime", "" "). CompareTo (Sp.getstring ("EndTime", "")) > 0) {toast.maketext (Settingactivity.this, "end time must be greater than start time", "+"). Show ();}}

The layout file for the popup date-time selection screen is as follows:

Data_time_dialog.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " > <textview android:layout_width= "match_parent" android:layout_height= "Wrap_content" Androi D:singleline= "true" android:text= "Select Date" android:textsize= "15sp"/> <datepicker Andro Id:id= "@+id/data_picker" android:layout_width= "wrap_content" android:layout_height= "Wrap_content"/&G    T <textview android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:layout     _margintop= "10DP" android:singleline= "true" android:text= "Select Time" android:textsize= "15sp"/> <timepicker android:id= "@+id/time_picker" android:layout_width= "Wrap_content" Android:layout_h eight= "Wrap_content"/> </LinearLayout> 

At this point, the ability to select a datetime and write related settings information to the file has been implemented.





Android Project Call Butler (4)-----Add intercept time period

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.