Android from ignorance to knowing--no.5

Source: Internet
Author: User

Let's do it today. IP dialing is implemented using broadcasts.

The main use of this piece of knowledge is the Android four components of one of the broadcast receiver (broadcast receiver). Then it receives what east, is our unnecessary one of the events, for example, there are outgoing calls, low battery tips. To text messages and so on.

What is the IP dialing, you may have this impression, in the long-distance call when the number is preceded by several numbers, this is called the IP number, so the call is cheaper than direct dialing. In fact, the principle of IP dialing is very easy, it is to use the front of the broadcast receiver to hear the number we dialed, and then extract it out. Add the IP number in front of it, and finally change to the so-called IP phone number, OK, directly on the code ...  

<textview        android:layout_width= "fill_parent"        android:layout_height= "wrap_content"        android:text= "@string/input"/><edittext        android:inputtype= "Phone"        android:id= "@+id/et_ipnumber"        android: Layout_width= "Fill_parent"        android:layout_height= "wrap_content"         /><button        android:onclick= " Click "        android:layout_width=" fill_parent "        android:layout_height=" wrap_content "        android:text=" @ String/sure "/>


This must be very familiar to everyone, first build a small interface to set the IP number. Specifies that it can only enter the number of the phone type. No difficulty. Directly on:

The next step is the more critical steps. Another piece:

public class Mainactivity extends Activity {private EditText et_ipnumber; Private sharedpreferences sp; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); et_ipnumber= (EditText) Findviewbyid (R.id.et_ipnumber ); Sp=getsharedpreferences ("config", mode_private), Et_ipnumber.settext (sp.getstring ("Ipnumber", ""));} @SuppressLint ("Showtoast") public void Click (View view) {String ipnumber=et_ipnumber.gettext (). toString (). Trim (); Editor Editor=sp.edit (); Editor.putstring ("Ipnumber", Ipnumber); Editor.commit (); Toast.maketext (This, "set complete", 0). Show ();}} 


First we find the set IP number by ID, assign it to ipnumber, we need to save it after we get the number. So the next time you go in, this number will also show that the use of sharedpreferences. It is a lightweight, lightweight storage solution that is especially suitable for saving software configuration parameters. The getsharedpreferences has two parameters, and the first parameter is the name of the store. The second parameter is the way the file is opened, and then the SP calls its method to get an editor, save the data to Sharedpreferences, do not forget to commit with the commit () method, get this data and return it to the Et_ipnumber text, Such a base IP dial-up is complete.

The layout is complete. The next thing to do is to monitor the outgoing calls and change them into the form of an IP number.

We need to build a Java class that inherits broadcast receiver, on the code:

public class Outcall extends Broadcastreceiver {@Overridepublic void OnReceive (context context, Intent Intent) {System.ou T.println ("found an external call"); String Number=getresultdata (); System.out.println ("This is the number of outgoing calls");//Get Dial-out number//replacement number Sharedpreferences sp =context.getsharedpreferences ("config", Context. Mode_private); String ipnumber=sp.getstring ("Ipnumber", ""); String newnumber=ipnumber+number;//Gets the new outgoing number setresultdata (Newnumber);}      }


when there is an outgoing call, it calls the OnReceive method (in addition to the next article on this method and related knowledge), we can also display the phone number that is heard, and then add it and set the IP number together to get the IP phone number finally.

On the aircraft:

I call myself, the front plus a set of IP number 1234. OK, this is basically the case, the next article on the example of some of the problems appearing in some other necessary incidental knowledge, first of all.

Android from ignorance to knowing--no.5

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.