演示一个案例,需求如下:
在Service组件中创建一个线程,该线程用来生产数值,每隔1秒数值自动加1,然后把更新后的数值在界面上实时显示。

步骤如下:
1、新建一个android项目工程,取名为demo。
2、新建一个Service类,用来实时生产数值,供界面实时显示。

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 = ;
private boolean 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); //发送广播
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=;
threadDisable = true;
Log.v("CountService", "on destroy");
} }

3、新建一个Activity类,显示数据。

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); //启动服务
startService(new Intent(MainActivity.this, CountService.class)); //注册广播接收器
receiver=new MyReceiver();
IntentFilter filter=new IntentFilter();
filter.addAction("com.ljq.activity.CountService");
MainActivity.this.registerReceiver(receiver,filter);
} @Override
protected void onDestroy() {
//结束服务
stopService(new Intent(MainActivity.this, CountService.class));
super.onDestroy();
} /**
* 获取广播数据
*
* @author jiqinlin
*
*/
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle=intent.getExtras();
int count=bundle.getInt("count");
editText.setText(count+"");
}
} }

4、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">
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="false"
android:editable="false"
android:id="@+id/editText"/> </LinearLayout>

5、清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ljq.activity"
android:versionCode=""
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="" /> </manifest>

效果如下:

最新文章

  1. ajax 同步和异步
  2. pythony语法小练习
  3. iBus
  4. 深入理解C#泛型
  5. qq被冻结怎么激活
  6. POJ2151Check the difficulty of problems
  7. 在非gui线程使用QMessageBox
  8. PHP 单列模式实例讲解以及参考网址
  9. fzu 1911 C. Construct a Matrix
  10. [BZOJ4518]征途
  11. 遍历文件路径python版,java版
  12. Debian Nginx 下载 .apk 文件时候报 403 错误 [1]
  13. python3 利用pip命令安装包和模块
  14. node.js vue-axios和vue-resource
  15. idea系列ide给git增加push按钮
  16. Python3学习笔记24-操作文件和目录
  17. js-ES6学习笔记-module(1)
  18. ERROR 1698 (28000): Access denied for user &#39;root&#39;@&#39;localhost&#39; 解决方法
  19. VS增强插件 Supercharger破解教程
  20. 【Unity】使用Git管理项目及其.gitignore写法

热门文章

  1. Lambda(2)
  2. 使用RX方式模拟DoubanFm的登陆
  3. Android开发——AsyncTask详解
  4. sysfs-&gt;sys简单介绍
  5. matlab实现贝塞尔曲线绘图pdf查看
  6. Build Settings
  7. Ubuntu 12.4 下升级 Subversion 1.7
  8. 存储过程——在LINQ中使用(六)
  9. Jquery 固定悬浮层以及固定表头
  10. Java---算法---插入排序