Android's broadcast

Source: Internet
Author: User

Broadcast in Android

The system will generate a lot of events in the process, some events, such as: Power changes, send and receive text messages, call, screen unlock, boot, etc., the system will send a broadcast. As long as the application accepts this broadcast, it knows that the system has a corresponding event and executes the code accordingly. As long as the broadcast receivers that are turned on in our application can receive the broadcast of the system.

IP Dialer

In a small case, the use of the broadcast receiver is also briefly described, and the function is to automatically add a specific number before the number when making a call.

Layout file

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <EditTextAndroid:hint= "Please enter a number prefix"Android:id= "@+id/num"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" />    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Save"Android:onclick= "click"/></LinearLayout>

Activity Code

To save a number prefix to a local XML file

 PackageXidian.dy.com.chujia;Importandroid.content.SharedPreferences;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.View;ImportAndroid.widget.EditText; Public classMainactivityextendsappcompatactivity {@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }     Public voidClick (View v) {EditText et=(EditText) Findviewbyid (r.id.num); //save data to a local ip.xmlSharedpreferences sp = getsharedpreferences ("IP", mode_private); if(Et! =NULL) Sp.edit (). putstring ("Ipnumber", Et.gettext (). toString ()). commit (); }}

Broadcast recipient

In a broadcast recipient, the data is received in the broadcast by Getresultdata, and the information is saved as a string.

Write the new data back through the Setresultdata

 PackageXidian.dy.com.chujia;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;Importandroid.content.SharedPreferences;ImportAndroid.util.Log;/*** Created by Dy on 2016/7/9.*/ Public classCallreceiverextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {String num=Getresultdata (); if(Num.startswith ("1")) {Sharedpreferences SP= context.getsharedpreferences ("IP", context.mode_private); String Number= Sp.getstring ("Ipnumber", "" "); LOG.E ( This. GetClass (). GetName (), number); Num= number +num;        Setresultdata (num); }    }}

Layout file

In the layout file, you need to call in the past, and you need to register the broadcast receiver and specify the type of broadcast to receive (the broadcast that accepts the call app here).

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Xidian.dy.com.chujia">    <uses-permissionAndroid:name= "Android.permission.PROCESS_OUTGOING_CALLS"/>    <ApplicationAndroid:allowbackup= "true"Android:icon= "@mipmap/ic_launcher"Android:label= "@string/app_name"Android:supportsrtl= "true"Android:theme= "@style/apptheme">        <ActivityAndroid:name=". Mainactivity "Android:label= "Main interface">            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>        <receiverAndroid:name=". Callreceiver ">            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.NEW_OUTGOING_CALL"/>            </Intent-filter>        </receiver>    </Application></Manifest>

Attention

When the dialer app dials the number and sends out a broadcast, the system automatically launches our app (which does not start activity here) and then calls our broadcast receivers. When our broadcast receiver finishes processing the data, it will continue to hand over the broadcast message to the calling application.

Our IP dialer is not started

Make a call (set the prefix to 19751)

The system automatically launches our IP dialer application

Android's broadcast

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.