Broadcastreceiver broadcast receiver (4) -- localbroadcastmanager for local broadcast and dialog in onreceive ()

Source: Internet
Author: User
Mainactivity is as follows:
Package CC. CV; import android. OS. bundle; import android. support. v4.content. localbroadcastmanager; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. app. activity; import android. content. context; import android. content. intent; import android. content. intentfilter;/*** demo Description: * 1. Use the complete example of local broadcast localbroadcast. * 2. Use the onreceive () method to display the dialog ** features of local broadcast: * 1 local broadcast can only be transmitted in this app, and the broadcast receiver can only receive broadcasts from this app. * In this case, other applications may be prevented from obtaining sensitive data in the broadcast to ensure data security. ** 2 local broadcast can only be dynamically registered in code. * static registration is mainly used to enable the program to receive broadcasts even when it is not started. when sending a local broadcast *, our app must have been started, so static registration is not required at all. otherwise, this local * broadcast has no special features. * ** Note: * in the onreceive () method of the broadcast, the dialog * 1 is displayed to set the type * Dialog for the dialog. getwindow (). settype (windowmanager. layoutparams. type_system_alert); * 2 pay attention to permissions * <uses-Permission Android: Name = "android. permission. sy Stem_alert_window "/> ** reference: * http://blog.csdn.net/guolin_blog * Thank you very much */public class mainactivity extends activity {private button mbutton; private intent mintent; private context mcontext; private intentfilter mintentfilter; private localbroadcastmanager mlocalbroadcastmanager; private localbroadcastreceiver mlocalbroadcastreceiver; @ overrideprotected void oncreate (bundle savedinst Ancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Init ();} private void Init () {mcontext = This; mbutton = (button) findviewbyid (R. id. button); mbutton. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view) {registerlocalbroadcastreceiver (); sendlocalbroadcast ();}});} /*** register the local broadcast receiver */private void registerlocalbroadcastreceiver () {mintentfilter = new intent Filter ("test_action"); mlocalbroadcastreceiver = new localbroadcastreceiver (); mlocalbroadcastmanager = localbroadcastmanager. getinstance (mcontext); mlocalbroadcastmanager. registerreceiver (mlocalbroadcastreceiver, mintentfilter);}/*** send local broadcast */private void sendlocalbroadcast () {mintent = new intent ("test_action"); mintent. putextra ("Number", "9527"); mlocalbroadcastmanager. sendbroadcast (mintent); // cancel the local broadcast after the broadcast is sent. Register unregisterlocalbroadcastreceiver ();}/*** cancel registration of local broadcast */private void unregisterlocalbroadcastreceiver () {If (mlocalbroadcastmanager! = NULL) {If (mlocalbroadcastreceiver! = NULL) {mlocalbroadcastmanager. unregisterreceiver (mlocalbroadcastreceiver) ;}}@ overrideprotected void ondestroy () {super. ondestroy (); merge ();}}


Localbroadcastreceiver is as follows:

Package CC. CV; import android. app. alertdialog. builder; import android. app. dialog; import android. content. broadcastreceiver; import android. content. context; import android. content. dialoginterface; import android. content. intent; import android. view. windowmanager; public class localbroadcastreceiver extends broadcastreceiver {@ overridepublic void onreceive (context, intent) {string message = intent. getstringextra ("Number"); showdialoginbroadcastreceiver (context, message);}/*** in the onreceive () method of broadcast, dialog * 1 is displayed to set type * Dialog for dialog. getwindow (). settype (windowmanager. layoutparams. type_system_alert); * 2 pay attention to permissions * <uses-Permission Android: Name = "android. permission. system_alert_window "/> */private void showdialoginbroadcastreceiver (context, string message) {Builder = new Builder (context); builder. seticon (R. drawable. ic_launcher); builder. settitle ("title"); builder. setmessage (Message); builder. setnegativebutton ("OK", new dialoginterface. onclicklistener () {@ override public void onclick (dialoginterface dialog, int arg1) {dialog. dismiss () ;}}); dialog = builder. create (); dialog. getwindow (). settype (windowmanager. layoutparams. type_system_alert); dialog. show ();}}

Main. XML is as follows:
<Relativelayout 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"> <button Android: id = "@ + ID/button" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_centerinparent = "true" Android: TEXT = "Send local broadcast"/> </relativelayout>


Broadcastreceiver broadcast receiver (4) -- localbroadcastmanager for local broadcast and dialog in onreceive ()

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.