Android (Java) Learning Note 178: Customizing broadcasts

Source: Internet
Author: User

Broadcast using:
Radio: Send signals externally. Broadcast Radio--------- Radio (customizable)
Radio: A signal to receive a radio station. ----- Broadcast Recipients

Here, we explain the custom stations and build our own radio broadcasts :

1. First we write our own radio Android program, build the radio, as follows:

(1) The first is the layout file, Activity_main.xml file:

<Relativelayoutxmlns: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"Tools:context=". Mainactivity " >    <ButtonAndroid:onclick= "click"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_centerhorizontal= "true"android:layout_centervertical= "true"Android:text= "Send Broadcast" /></Relativelayout>

(2) Define a method for the button above click, here we directly implement it, click the button to send the broadcast:

The Mainactivity.java code is as follows:

 PackageCom.itheima.sender;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.view.View; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }        //Send broadcast     Public voidClick (View view) {
Intent Intent=NewIntent (); Intent.setaction ("Com.itheima.sender.CCTV"); Intent.setdata (Uri.parse ("cctv://the whole country up and down a piece of red")); Sendbroadcast (Intent); }}
using intent to send data , the action action here is: the string "Com.itheima.sender.CCTV", which is custom.
Here is the setting data:intent.setdata (Uri.parse ("cctv://National Red")); Here the custom settings data is the Uri type, the data at the beginning of the keyword is:CCTV

You don't have to set any permissions here.

2. It is necessary to set up a radio receiver so that we can hear the broadcast, and then we will write a broadcast receiver Android program that listens to the custom broadcast above:

(1) The first step is to buy a radio, write mybroadcastreceiver inherit from Broadcastreceiver:

 PackageCom.itheima.receiver;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.widget.Toast; Public classMybroadcastreceiverextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {toast.maketext (context,"I am a civil servant, I received the broadcast of the Organization" +intent.getdata (), 0). Show (); }}

(2) The second and third steps are: install the battery and adjust the channel :

Add Receiver node content under the application node of the Androidmanifest.xml configuration file:

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.itheima.receiver"Android:versioncode= "1"Android:versionname= "1.0" >    <USES-SDKandroid:minsdkversion= "8"android:targetsdkversion= "+" />    <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" >        <ActivityAndroid:name= "Com.itheima.receiver.MainActivity"Android:label= "@string/app_name" >            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>

<receiverAndroid:name= "Com.itheima.receiver.MyBroadcastReceiver"> <Intent-filter> <ActionAndroid:name= "com.itheima.sender.CCTV"/> <DataAndroid:scheme= "CCTV" /> </Intent-filter> </receiver >

</Application></Manifest>

The effect on the phone is as follows :

3. The above has set up the custom radio station and also prepared the corresponding broadcast receiver :

Let's look at the effect of the run, click on the button below:

The effect is as follows when you press the button:

This is the logical process of a complete custom station

Android (Java) Learning Note 178: Customizing broadcasts

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.