[Android instance] demonstration of service, broadcast, and broadcastreceiver in Android)

Source: Internet
Author: User

From: http://www.eoeandroid.com/forum.php? MoD = viewthread & tid = 152136.

Main. Java

Package COM. webabcd. service; import android. app. activity; import android. content. broadcastreceiver; import android. content. componentname; import android. content. context; import android. content. intent; import android. content. intentfilter; import android. content. serviceconnection; import android. OS. bundle; import android. OS. ibinder; import android. view. view; import android. view. view. onclicklistener; import Andro Id. widget. textview;/** differences between startservice () and bindservice () * startservice ()-normal understanding * bindservice ()-make the current context object (activity in this example) A serviceconnection object is bound to the specified service. In this way, if the context object is destroyed, the corresponding service will also be destroyed */public class main extends activity implements onclicklistener {private textview txtmsg; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); settitle ("service of Android"); this. findviewbyid (R. id. btnstart ). setonclicklistener (this); this. findviewbyid (R. id. btnstop ). setonclicklistener (this); This. findviewbyid (R. id. btnbind ). setonclicklistener (this); this. findviewbyid (R. id. btnunbind ). setonclicklistener (this); txtmsg = javasmsg); // instantiate the custom broadcastreceiver receiver ER = new updatereceiver (); intentfilter filter = new intentfilter (); // specify the action for broadcastreceiver, it is used to receive the broadcast filter of the same action. addaction ("com. webabcd. service. MSG "); // register broadcastreceive programmatically R. For the example of registering broadcastreceiver in configuration mode, see androidmanifest. XML file // This is generally registered at onstart and canceled at onstop. registerreceiver (receiver, filter); // This. unregisterreceiver (guest ER) ;}@ override public void onclick (view v) {intent = new intent (main. this, myservice. class); Switch (v. GETID () {case R. id. btnstart: This. startservice (intent); break; case R. id. btnstop: This. stopservice (intent); break; case R. id. btnbind: This. bindservice (intent, Conn, context. bind_auto_create); break; case R. id. btnunbind: This. unbindservice (conn); break ;}// serviceconnection object private serviceconnection conn = new serviceconnection () {@ override public void onserviceconnected (componentname classname, ibinder Service) required by bindservice) {}@ override public void onservicedisconnected (componentname classname) {}}; private string MSG = ""; private updatereceiver receiver; // implements a broadcastreceiver, used to receive the specified broadcast public class updatereceiver extends broadcastreceiver {@ override public void onreceive (context, intent) {MSG = intent. getstringextra ("MSG"); txtmsg. append (MSG + "\ n ");}}}

Myservice. Java

Package COM. webabcd. service; import android. app. service; import android. content. intent; import android. OS. ibinder; import android. util. log; // shows the service lifecycle. Run the program according to the specific information and view the public class myservice extends Service {@ override public ibinder onbind (intent) {log in logcat. D ("mydebug", "onbind"); sendmsg ("onbind"); // todo auto-generated method stub return NULL;} @ override public void oncreate () {// todo auto-generated method stub super. oncreate (); log. D ("mydebug", "oncreate"); sendmsg ("oncreate") ;}@ override public void ondestroy () {// todo auto-generated method stub super. ondestroy (); log. D ("mydebug", "ondestroy"); sendmsg ("ondestroy") ;}@ override public void onrebind (intent) {// todo auto-generated method stub super. onrebind (intent); log. D ("mydebug", "onrebind"); sendmsg ("onrebind") ;}@ override public void onstart (intent, int startid) {super. onstart (intent, startid); log. D ("mydebug", "onstart"); sendmsg ("onstart") ;}@ override public Boolean onunbind (intent) {log. D ("mydebug", "onunbind"); sendmsg ("onunbind"); // todo auto-generated method stub return Super. onunbind (intent);} // send the broadcast information private void sendmsg (string MSG) {// specify the action of the broadcast target (Note: The receiver who specifies this action will receive this broadcast) intent intent = new intent ("com. webabcd. service. MSG "); // The intent parameter to be passed. putextra ("MSG", MSG); // send broadcast this. sendbroadcast (intent );}}

Mybootreceiver. Java

Package COM. webabcd. service; import android. content. broadcastreceiver; import android. content. context; import android. content. intent; import android. util. log; public class mybootreceiver extends broadcastreceiver {// used to receive broadcast instances that meet the conditions. (For details about the registration information of broadcast instances, see androidmanifest. XML, this broadcast receiver will be called after the system is started) @ override public void onreceive (context arg0, intent arg1) {log. D ("mydebug", "onreceive"); // start the intent service = new intent (arg0, myservice. class); arg0.startservice (service );}}

Androidmanifest. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "com. webabcd. service "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <application Android: icon =" @ drawable/icon "Android: label = "@ string/app_name"> <activity Android: Name = ". main "Android: Label =" @ string/app_name "> <intent-filter> <action Android: Name =" android. intent. action. main "/> <category Android: Name =" android. intent. c Ategory. launcher "/> </intent-filter> </activity> <! -- If you want to use a service, make the corresponding configuration here --> <service android: Name = ". myservice"> </service> <! -- Register a broadcastreceiver whose intent-filter is Android. intent. action. boot_completed (used to receive broadcast after system startup) --> <Cycler Android: Name = ". mybootreceiver "> <intent-filter> <action Android: Name =" android. intent. action. boot_completed "/> </intent-filter> </receiver> </Application> <! -- Accept the broadcast permission after the system starts --> <uses-Permission Android: Name = "android. permission. receive_boot_completed "/> <uses-SDK Android: minsdkversion =" 3 "/> </manifest>

Main. xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"        android:orientation="vertical" android:layout_width="fill_parent"        android:layout_height="fill_parent">                <Button android:text="startService" android:id="@+id/btnStart"                android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>        <Button android:text="stopService" android:id="@+id/btnStop"                android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>        <Button android:text="bindService" android:id="@+id/btnBind"                android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>        <Button android:text="unbindService" android:id="@+id/btnUnbind"                android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>        <TextView android:id="@+id/txtMsg" android:layout_width="fill_parent"                android:layout_height="wrap_content" />                </LinearLayout>

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.