This example describes the way in which the Android service delivers data to the activity in real time. Share to everyone for your reference. Specifically as follows:
Here is a case where the requirements are as follows:
Create a thread in the service component that is used to produce numeric values, 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 project, named Demo.
2, a new service class, used 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 Boolean threaddisable=false;
@Override public void OnCreate () {super.oncreate ();
New Thread (New Runnable () {@Override public void run () {while (!threaddisable) {try {thread.sleep (1000);
catch (Interruptedexception e) {e.printstacktrace ();
} count++;
LOG.V ("Countservice", "Count is" + count);
Send broadcast Intent intent=new Intent ();
Intent.putextra ("Count", count);
Intent.setaction ("Com.ljq.activity.CountService");
Sendbroadcast (Intent);
}}). 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, display 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 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 {@Overr
IDE 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:cursorvisible=" false "
android:editable=" false "
android:id=" @+id/edittext "/>
</LinearLayout>
5. List Documents
<?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:
I hope this article will help you with your Android program.