Service delivers data cases 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 class Countservice extends Service { private int count = 0; private Boolea n threaddisable=false;   @Override  public void OnCreate () {  super.oncreate ();      new Thread (New Runnable () {    @Override    public Void Run () {     while (!threaddisable) {     try {       Thread.Sleep (+);      } catch (Interruptedexception e) {       e.printstacktrace ();     }     count++;      LOG.V ("Countservice", "Count is" + count);            //Send broadcast      intent intent=new Intent ();   &Nbsp;  intent.putextra ("Count", count);      intent.setaction (" Com.ljq.activity.CountService ");      sendbroadcast (intent);     }    } &NBSP,}). Start ();  }   @Override  public ibinder onBind (Intent Intent) {  return null; }   @Override  public void OnDestroy () {   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 class Mainactivity extends Activity { private EditText edittext=null; private myreceiver receiver=null ;       @Override     public void OnCreate (Bundle Savedinstancestate) {        super.oncreate (savedInstanceState);         setcontentview (R.layout.main);                  edittext= (EditText) findViewById ( R.id.edittext);                  //Start Service      &Nbsp;  startservice (New Intent (Mainactivity.this, countservice.class));            //Registered broadcast receiver   receiver=new Myreceiver ();   intentfilter Filter=new Intentfilter ();   filter.addaction ("Com.ljq.activity.CountService");   MainActivity.this.registerReceiver (Receiver,filter);    }           @Override  protected void OnDestroy () {     //End service          stopservice (New Intent (Mainactivity.this, countservice.class));   super.ondestroy ();   }         /**      * get Broadcast data      *      * @author jiqinlin      *     */    public class MyReceiver extends Broadcastreceiver {&NBsp;     @Override      public void onreceive (context context, Intent Intent) {      bundle Bundle=intent.getextras ();       int Count=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:cursorV Isible= "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 AndroidOid:name= "Android.intent.category.LAUNCHER"/>             </intent-filter>        </activity>   <service android:name = ". Countservice "/>     </application>    <uses-sdk android:minsdkversion= "7"/> </manifest>

The effect is as follows:

Transferred from: http://www.cnblogs.com/linjiqin/p/3147764.html

Service delivers data cases 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.