Android Entry-service Real-time data transfer to activity via Broadcastreceiver

Source: Internet
Author: User

Citation:

Http://www.cnblogs.com/linjiqin/p/3147764.html

<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"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=". EX0315"> <TextView android:layout_width="100DP"Android:layout_height="wrap_content"Android:id="@+id/ex0315textview"Android:layout_marginleft="20DP"Android:layout_margintop="0DP"Android:text="@string/hello_world"/> <button android:id="@+id/ex0315startbutton"Android:layout_width="100DP"Android:layout_height="wrap_content"Android:layout_marginleft="20DP"Android:layout_margintop="40DP"Android:text="@string/ex0315startbutton"/> <button android:id="@+id/ex0315sendbutton"Android:layout_width="100DP"Android:layout_height="wrap_content"Android:layout_marginleft="20DP"Android:layout_margintop="110DP"Android:text="@string/ex0315sendbutton"/></relativelayout>
Package Com.iruisi.service;import android.app.service;import android.content.intent;import Android.os.Handler; Import Android.os.ibinder;import Android.util.Log; Public classEx0315service extends Service {Private StaticFinal String TAG ="MyService"; PrivateHandler mhandler=NewHandler (); Private intMcount=0; PrivateRunnable mrunnable =NewRunnable () {@Override Public voidrun () {MCount++; Sendmsg2activity ("start Send to service"); LOG.I ("Hippo","Calculator"+integer.tostring (MCount)); Mhandler.postdelayed (mrunnable, +);    }    }; @Override Public voidonCreate () {super.oncreate (); LOG.E (TAG,"Start oncreate~~~"); //turn on service delay 1sMhandler.postdelayed (Mrunnable, +); }    Private voidsendmsg2activity (String msg) {//Send broadcastIntent intent=NewIntent (); Intent.putextra ("Count", MCount); Intent.setaction ("Com.iruisi.service.Ex0315Service");    Sendbroadcast (Intent); } @Override Publicibinder onbind (Intent Intent) {return NULL; } @Override Public voidOnDestroy () {mhandler.removecallbacks (mrunnable);    Super.ondestroy (); } @Override Publicboolean onunbind (Intent Intent) {//TODO auto-generated Method Stub        returnSuper.onunbind (Intent); } @Override Public voidOnrebind (Intent Intent) {//TODO auto-generated Method StubSuper.onrebind (Intent); } @Override Public intOnstartcommand (Intent Intent,intFlagsintStartid) {        //TODO auto-generated Method Stub    returnSuper.onstartcommand (Intent, flags, Startid);}}
Package Com.example.hellowrold;import Com.example.hellowrold.r.id;import com.iruisi.service.Ex0315Service; Import Android. R.string; import Android.os.bundle;import android.app.activity;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.content.intentfilter;import Android.view.menu;import android.view.view;import android.widget.button;import Android.widget.TextView; Public classEX0315 extends Activity {PrivateTextView Mtextview; PrivateButton Startbutton; PrivateButton Sendbubutton; PrivateEx0315servicereceiver receiver=NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_ex0315); Mtextview=(TextView) Findviewbyid (ID.        Ex0315textview); Startbutton=(Button) Findviewbyid (ID.        Ex0315startbutton); Startbutton.setonclicklistener (NewButton.onclicklistener () {@Override Public voidOnClick (View v) {Intent sintent=NewIntent (EX0315. This, Ex0315service.class); //sintent.setflags (intent.flag_activity_new_task);StartService (sintent); Receiver=NewEx0315servicereceiver (); Intentfilter Mintentfilter=NewIntentfilter (); Mintentfilter.addaction ("Com.iruisi.service.Ex0315Service"); EX0315. This. Registerreceiver (receiver, mintentfilter);                }        }); Sendbubutton=(Button) Findviewbyid (ID.        Ex0315sendbutton); Sendbubutton.setonclicklistener (NewButton.onclicklistener () {@Override Public voidOnClick (View v) {Intent sintent=NewIntent (EX0315. This, Ex0315service.class);            StopService (sintent);                        }        }); Mtextview.settext ("Hahh"); } @Override Publicboolean Oncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (r.menu.ex0315, menu); return true; }     Public classEx0315servicereceiver extends broadcastreceiver{@Override Public voidOnReceive (Context context, Intent Intent) {Bundle Mbundle=Intent.getextras (); intCount=mbundle.getint ("Count"); Mtextview=(TextView) Findviewbyid (ID.        Ex0315textview);        Mtextview.settext (String.valueof (count)); }    }    }
add        in manifest <service            android:name="com.iruisi.service.Ex0315Service"             android:exported="false" >        </service>

This service is in a different bag.

Click the Send message to the server button, in fact, "stop service" named error, click will stop.

Note that the TextView object is to be retrieved again on the broadcast. To display.

Android Entry-service Real-time data transfer to activity via Broadcastreceiver

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.