Broadcast using and displaying messages in the notification bar

Source: Internet
Author: User

This example has only 1 interface (Activity), the interface includes a edittext and a button. When the button is pressed, get the contents of the EditText and broadcast. This example also creates a broadcastreceiver that receives a broadcast when the button is pressed and displays the broadcast message content in the notification bar. When you click the broadcast message in the notification bar, another activity will be opened. The following results are displayed when finished:

Programming implementation

1. Create a new Android project (procedure omitted) and add the required files. Once added, the project catalog is as follows:

2, edit Activity_main.xml, add a edittext and a button, complete after the content is as follows:

<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"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.broadcastreceiver.broadcastreceiverproject.mainactivity$placeholderfragment" >    <EditTextAndroid:id= "@+id/edittext1"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:layout_alignparentleft= "true"Android:layout_alignparenttop= "true"Android:ems= "Ten"Android:hint= "@string/textedit1_text"Android:inputtype= "text" >    </EditText>    <ButtonAndroid:id= "@+id/button1"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:layout_alignleft= "@+id/edittext1"Android:layout_below= "@+id/edittext1"Android:layout_margintop= "14DP"Android:text= "@string/button1_text" /></Relativelayout>

3. Edit the Mainactivity.java file and define the class mainactivity in the Mainactivity.java. Mainactivity inherits from activity, with Activity_main.xml defined interface as the display interface. When the button on the activity_main.xml is pressed, get the contents of the EditText and send the broadcast. Mainactivity.java content is as follows:

 PackageCom.broadcastreceiver.broadcastreceiverproject;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast;ImportAndroid.content.Context;Importandroid.content.Intent; Public classMainactivityextendsActivity {PrivateContext Mcontext; PrivateButton Btnsendbroadcast; PrivateEditText etbroadcastcontent; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); Mcontext= This; Btnsendbroadcast=(Button) Findviewbyid (R.id.button1); Btnsendbroadcast.setonclicklistener (NewBtnsendbroadcast_listener ()); Etbroadcastcontent=(EditText) Findviewbyid (R.ID.EDITTEXT1); }        Private classBtnsendbroadcast_listenerImplementsView.onclicklistener {@Override Public voidOnClick (View v) {String content=Etbroadcastcontent.gettext (). toString (). Trim (); if(Content.length () < 1) {Toast.maketext (Mcontext, Etbroadcastcontent.gethint (), Toast.length_short). Show (); return; } Intent Intent=NewIntent (); Intent.setaction ("Com.eoeandroid.action.BroadcastReceiverTest"); Intent.putextra ("Msg_content", content);        Sendbroadcast (Intent); }    }}

4. Edit the Hellobroadcastreceiver.java file, which defines the broadcast receiver Broadcastreceiver, which is used to receive broadcasts sent in Mainactivity. When the broadcast is received, the broadcast content is displayed with the notification bar. The contents of the Hellobroadcastreceiver.java file are as follows:

 PackageCom.broadcastreceiver.broadcastreceiverproject;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.app.NotificationManager;Importandroid.app.Notification;Importandroid.app.PendingIntent; Public classHellobroadcastreceiverextendsbroadcastreceiver{Privatecontext Context; @Override Public voidOnReceive (Context context, Intent Intent) { This. Context =context;    Shownotification (Intent); }        //Notification bar Display message    Private voidshownotification (Intent Intent) {Notificationmanager Notificationmanager=(Notificationmanager) context. Getsystemservice (Context.notification_service); //a set of intent that will be executedIntent aintent[] =NewIntent[1]; aintent[0] =NewIntent (context, mainactivity.class); Pendingintent pendingintent= pendingintent.getactivities (context, 0, aintent, 0); String title= Intent.getextras (). getString ("Msg_content"); Notification Noti=NewNotification.builder (context). Setcontenttitle (Title.subsequence (0, Title.length ())) . Setcontenttext (Title.subsequence (0, Title.length ())) . Setsmallicon (R.drawable.ic_launcher). Setwhen (System.currenttimemillis ()). setcontentintent (pendingintent)//The intent collection is executed after the call when the notification bar message is clicked. Build ();    Notificationmanager.notify (R.layout.activity_main, Noti); }}

5, the final need to register a broadcast receiver, this example is registered in a static manner. Add the following to the <application> in Androidmanifest.xml:

        <receiverAndroid:name=". Hellobroadcastreceiver "android:exported= "false">            <Intent-filter>                <ActionAndroid:name= "Com.eoeandroid.action.BroadcastReceiverTest"/>            </Intent-filter>        </receiver>

Broadcast using and displaying messages in the notification bar

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.