Android Simple Combat Tutorial--the 20th session "by the broadcast recipient, to dial the phone plus the IP number"

Source: Internet
Author: User

Not sleep, get up more article haha! First of all congratulate Lee Chong Wei defeated me Dan, although I am supporting my Dan's, but he is not easy haha, worthy of respect of the people! Cut to the chase: This article describes a custom broadcast recipient.

Usually when we call outside, we generally use internet telephony. If the phone number is a type of phone, it is automatically preceded by the IP number. Thus saving money. Today, this article simulates IP dialing. This function is implemented by the broadcast receiver. Customize the broadcast receiver, "Listen" for outgoing calls when the system broadcasts (the system shouts to our registered broadcast: "Dear, I'm coming!" "), after receiving the message, give the outgoing phone a prefix price IP number.

Let's start with an activity layout:

< LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/ Tools "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:paddingbottom=" @dimen/act Ivity_vertical_margin "android:paddingleft=" @dimen/activity_horizontal_margin "android:paddingright=" @dimen/ Activity_horizontal_margin "android:paddingtop=" @dimen/activity_vertical_margin "tools:context=". Mainactivity "android:orientation=" vertical "> <edittext android:id=" @+id/et "android:layout     _width= "Match_parent" android:layout_height= "wrap_content" android:hint= "Please enter IP line"/><button Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Save" android:onClick= "clic" K "/></LINEARLAYOUT> 
The edittext is used to enter the IP number we want to add, and the button saves the number.

To register a custom broadcast (profile registration):

        <receiver android:name= ". Callreceiver ">            <intent-filter >                      <!--Intent Filter, the intention of calling outside is this stuff android.intent.action.NEW_ Outgoing_call, we register so we can receive--                <action android:name= "Android.intent.action.NEW_OUTGOING_CALL"/>            </intent-filter>        </receiver>


Package Com.itydl.ipdialer;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.content.sharedpreferences;public class Callreceiver extends BroadcastReceiver {//Receive the broadcast will call @overridepublic void OnReceive (context context, Intent Intent) {//Add IP Line//In the telephone broadcast, will bring the dialed number, Get to string number = Getresultdata () by the following code, if (Number.startswith ("0")) {Sharedpreferences SP = Context.getsharedpreferences ("IP", context.mode_private); String Ipnumber = sp.getstring ("Ipnumber", "");//Add the IP line number to the front of the user dialing number = Ipnumber + number;//put the new number back into the broadcast, At this point the dialed number is preceded by our defined IP number Setresultdata, Abortbroadcast ();}}

On the top of the broadcast receiver written out, first extract our saved IP number, and then add a prefix to the number, and then the last number to dial outside the display.

Take a look at the code on the main activity:

Package Com.itydl.ipdialer;import Android.os.bundle;import Android.app.activity;import Android.content.sharedpreferences;import Android.view.menu;import Android.view.view;import Android.widget.edittext;public class Mainactivity extends Activity {    @Override    protected void OnCreate (Bundle Savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    }    public void Click (View v) {    EditText et = (EditText) Findviewbyid (r.id.et);    Sharedpreferences sp = getsharedpreferences ("IP", mode_private);    Sp.edit (). putstring ("Ipnumber", Et.gettext (). toString ()). commit ();    }    }

Remember to have a permission to join: <uses-permission android:name= "Android.permission.PROCESS_OUTGOING_CALLS"/>

Run to see the effect:


Take a look at the outgoing calls again:


Android Simple Combat Tutorial--the 20th session "by the broadcast recipient, to dial the phone plus the IP number"

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.