演示一个案例,需求如下:
在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>

效果如下:

Android Service实时向Activity传递数据的更多相关文章

  1. Service实时向Activity传递数据案例

    转自 http://www.cnblogs.com/linjiqin/p/3147764.html 演示一个案例,需求如下:在Service组件中创建一个线程,该线程用来生产数值,每隔1秒数值自动加1 ...

  2. Android开发:向下一个activity传递数据,返回数据给上一个activity

    1.向下一个activity传递数据 activity1 Button button=(Button) findViewById(R.id.button1); button.setOnClickLis ...

  3. 核心基础以及Fragment与Activity传递数据完整示例

    MainActivity如下: package cc.testsimplefragment0; import android.os.Bundle; import android.app.Activit ...

  4. Fragment与Activity传递数据

    MainActivity如下: package cc.testsimplefragment0; import android.os.Bundle; import android.app.Activit ...

  5. Fragment+Activity传递数据

    自己经常使用的知识点,每次到要用的时候都还要再查一次才能懂得使用,终于体会到总结的必要性了. Activity传递数据给Fragment Bundle bundle_fragment=new Bund ...

  6. Android开发学习之路-回调实现Service向activity传递数据

    开启服务的时候,如果我们是通过bindService来绑定服务并且要向服务传递数据,可以直接在Intent中设置bundle来达到效果,但是如果是我们需要从服务中返回一些数据到Activity中的时候 ...

  7. Android Activity传递数据使用getIntent()接收不到,揭秘Intent传递数据与Activity启动模式singleTask的关系。

    activity通过intent传递数据的时候,如果activity未启动,那么在这个刚启动的activity里通过getIntent()会获取到这个intent的数据.. 如果要启动的activit ...

  8. Android Activity 传递数据

    activity中数据的传递方式有2中,一种是使用putExtra()传递,另外一种是传递Bundle对象,使用putExtras()方法. 方法一 发送数据 putExtra()传送的是键值对,第一 ...

  9. Android 开发中使用Intent传递数据的方法

    Activity之间通过Intent传递值,支持基本数据类型和String对象及 它们的数组对象byte.byte[].char.char[].boolean.boolean[].short.shor ...

随机推荐

  1. objdump的使用方法和 symbol table的每列的含义

    一.objdump的用法 objdump命令的man手册 objdump     [-a] [-b bfname|     --target=bfdname] [-C] [--debugging]   ...

  2. [原创]PostgreSQL Plus Advince Server在 HA环境中一对多的Stream Replication配置(二)

    三.配置主机与备机的ssh无密码登录1.主机s1到备机s3的无密码登录a.创建ssh目录[root@s1 ~]# mkdir /opt/PostgresPlus/9.2AS/.sshb.修改ssh目录 ...

  3. NGUI3.5系列教程之 一些小功能的实现

    (一)可拖动窗体的实现: 1:添加一个Sprite为鼠标点击区域,改名为:DragSprite 2:给DragSprite添加Collider 3:给DragSprite添加Drag Object , ...

  4. constructor(构造器)

    当我们创建一个类的时候,如果不自定义构造器,则系统会自动创建一个默认的构造器,也是一个无参构造器用于初始化. 当我们在类的里面创建了自己的构造器,则系统将不会创建默认的构造器,由于需求条件不同,构造器 ...

  5. css/js online online code editor/formator/debuger

    http://cssdeck.com/labs http://jsfiddle.net/ http://fiddle.jshell.net/  support console http://plnkr ...

  6. cocos2dx中的坐标体系

    1.UI坐标系和GL坐标系 2.本地坐标与世界坐标 本地坐标是一个相对坐标,是相对于父节点或者你指明的某个节点的相对位置来说的,本地坐标的原点在参考节点的左下角 世界坐标是一个绝对的坐标,是以屏幕的左 ...

  7. iOS 10 因苹果健康导致闪退 crash-b

    如果在app中调用了苹果健康,iOS10中会出现闪退.控制台报出的原因是: Terminating app due to uncaught exception 'NSInvalidArgumentEx ...

  8. HTTP 错误 404.3 - Not Found由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射

    出现此错误可能是在安装IIS服务器的时候没有勾选“应用程序开发”, 解决方法如下: 就像安装IIS一样的步骤. 选择web服务器(IIS)后,再选择“应用程序开发”  选择asp.net  以及 服务 ...

  9. C# XML - XmlDocument

    http://msdn.microsoft.com/zh-cn/library/System.Xml.XmlDocument(v=vs.80).aspx 1.member http://msdn.mi ...

  10. UVA 11858 Frosh Week 逆序对统计

    题目链接: http://acm.hust.edu.cn/vjudge/contest/122094#problem/H Frosh Week Time Limit:8000MSMemory Limi ...