Android Service delivers data to activity in real time

Source: Internet
Author: User

Demonstrate a case with the following requirements:
Create a thread in the service component that is used to produce numeric values that automatically add 1 every 1 seconds and then display the updated values in real time on the interface.

The steps are as follows:
1, a new Android project, named as demo.
2, create a new service class, for real-time production of numerical values for the interface real-time display.

Package com.ljq.activity; import Android.app.service;import android.content.intent;import android.os.IBinder; Import Android.util.Log;  Public classCountservice extends Service {Private intCount =0; PrivateBoolean threaddisable=false; @Override Public voidonCreate () {super.oncreate (); NewThread (NewRunnable () {@Override Public voidrun () { while(!threaddisable) {     Try{Thread.Sleep ( +); } Catch(interruptedexception e) {e.printstacktrace (); } Count++; LOG.V ("Countservice","Count is"+count); //Send broadcastIntent intent=NewIntent (); Intent.putextra ("Count", Count); Intent.setaction ("Com.ljq.activity.CountService");    Sendbroadcast (Intent);  }}). Start (); } @Override Publicibinder onbind (Intent Intent) {return NULL; } @Override Public voidOnDestroy () {Super.ondestroy (); Count=0; Threaddisable=true; LOG.V ("Countservice","On destroy"); }  }

3. Create a new activity class to display the data.

Package com.ljq.activity; import Android.app.activity;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.content.intentfilter;import Android.os.bundle;import Android.view.view;import Android.widget.button;import Android.widget.EditText;  Public classMainactivity extends Activity {PrivateEditText edittext=NULL; PrivateMyreceiver receiver=NULL; @Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                 Setcontentview (R.layout.main); EditText=(EditText) Findviewbyid (R.id.edittext); //Start the serviceStartService (NewIntent (mainactivity. This, Countservice.class)); //registering a broadcast receiverReceiver=NewMyreceiver (); Intentfilter Filter=NewIntentfilter (); Filter.addaction ("Com.ljq.activity.CountService"); Mainactivity. This. Registerreceiver (Receiver,filter); } @Overrideprotected voidOnDestroy () {//End ServiceStopService (NewIntent (mainactivity. This, Countservice.class));   Super.ondestroy (); }         /** * Get Broadcast data * * @author Jiqinlin **/     Public classMyreceiver extends Broadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {Bundle bundle=Intent.getextras (); intCount=bundle.getint ("Count"); Edittext.settext (Count+""); }    }             }

4. Main.xml Layout file

<?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"> <edittext android:layout_width="fill_parent"Android:layout_height="wrap_content"android:cursorvisible="false"android:editable="false"Android:id="@+id/edittext"/> </LinearLayout>

5. manifest file

<?xml version="1.0"encoding="Utf-8"? ><manifest xmlns:android="http://schemas.android.com/apk/res/android" Package="com.ljq.activity"Android:versioncode="1"Android:versionname="1.0"> <application android:icon="@drawable/icon"Android:label="@string/app_name"> <activity android:name=". Mainactivity"Android:label="@string/app_name"> <intent-filter> <action android:name="Android.intent.action.MAIN"/> <category android:name="Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <service android:name =". Countservice"/> </application> <uses-sdk android:minsdkversion="7"/> </manifest>

The effect is as follows:

Android Service delivers data to activity in real time

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.