Service实时向Activity传递数据案例
转自
http://www.cnblogs.com/linjiqin/p/3147764.html
演示一个案例,需求如下:
在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 = 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);
//发送广播
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、新建一个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="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>
Service实时向Activity传递数据案例的更多相关文章
- Android Service实时向Activity传递数据
演示一个案例,需求如下:在Service组件中创建一个线程,该线程用来生产数值,每隔1秒数值自动加1,然后把更新后的数值在界面上实时显示. 步骤如下:1.新建一个android项目工程,取名为demo ...
- Android开发:向下一个activity传递数据,返回数据给上一个activity
1.向下一个activity传递数据 activity1 Button button=(Button) findViewById(R.id.button1); button.setOnClickLis ...
- Fragment+Activity传递数据
自己经常使用的知识点,每次到要用的时候都还要再查一次才能懂得使用,终于体会到总结的必要性了. Activity传递数据给Fragment Bundle bundle_fragment=new Bund ...
- 核心基础以及Fragment与Activity传递数据完整示例
MainActivity如下: package cc.testsimplefragment0; import android.os.Bundle; import android.app.Activit ...
- Fragment与Activity传递数据
MainActivity如下: package cc.testsimplefragment0; import android.os.Bundle; import android.app.Activit ...
- Android开发学习之路-回调实现Service向activity传递数据
开启服务的时候,如果我们是通过bindService来绑定服务并且要向服务传递数据,可以直接在Intent中设置bundle来达到效果,但是如果是我们需要从服务中返回一些数据到Activity中的时候 ...
- activity传递数据
这些都是老生常谈了,到处都搜的到,但是因为经常忘记,放着好调用: 传递数据: Intent intent = new Intent(); Bundle bundle = new Bundle(); b ...
- android78 Fragment和Activity 传递数据
Activity: package com.itheima.senddata; import android.os.Bundle; import android.app.Activity; impor ...
- Android Activity传递数据使用getIntent()接收不到,揭秘Intent传递数据与Activity启动模式singleTask的关系。
activity通过intent传递数据的时候,如果activity未启动,那么在这个刚启动的activity里通过getIntent()会获取到这个intent的数据.. 如果要启动的activit ...
随机推荐
- WinForm构造函数的作用
最近练习C#项目:何问起收藏夹(HoverTreeSCJ),实现编辑网址时,遇到这个问题:比如打开窗口后,要自动显示数据.解决方法:那么可以通过窗体的构造函数传递参数. 比如窗体类: public p ...
- MongoDB安装使用
标签: MongoDB 参考链接:Windows上安装MongoDB教程 1. 官方下载 MongoDB提供了可用于32位和64位系统的预编译二进制包,你可以从MongoDB官网下载安装,MongoD ...
- angular-JS模仿Form表单提交
直接上示例代码,有不懂的欢迎留言: $http({ url: "http://localhost:10086/yuanxin/Conference/ImportExcelDataForBus ...
- LINQ to SQL语句(14)之Null语义和DateTime
Null语义 说明:下面第一个例子说明查询ReportsToEmployee为null的雇员.第二个例子使用Nullable<T>.HasValue查询雇员,其结果与第一个例子相同.在第三 ...
- C#开发微信门户及应用(2)--微信消息的处理和应答
微信应用如火如荼,很多公司都希望搭上信息快车,这个是一个商机,也是一个技术的方向,因此,有空研究下.学习下微信的相关开发,也就成为计划的安排事情之一了.本系列文章希望从一个循序渐进的角度上,全面介绍微 ...
- c#中get set 的使用
在早期学习c#的过程中,经常遇到这样的语句: public string StudentName { get{return stuName;} set{stuName=value;} } 当时也不是很 ...
- dubbo连接zookeeper注册中心因为断网导致线程无限等待问题【转】
最近维护的系统切换了网络环境,由联通换成了电信网络,因为某些过滤规则导致系统连不上zookeeper服务器(应用系统机器在深圳,网络为电信线路,zookeeper服务器在北京,网络为联通线路),因为我 ...
- Bootstrap之导航条
基本导航条 <!-- navbar-inverse相反颜色风格 --> <!-- navbar-static-top去除圆角 --> <!-- navbar-fixed- ...
- android基于口令加密快速搞懂(一)
import java.util.Random; import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypt ...
- 【代码笔记】iOS-获得现在的日期
一,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, ...