1:MainActivity.java

public class MainActivity extends Activity {
private TextView tvInfo = null;
private BroadcastReceiver receiver = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); tvInfo = (TextView)findViewById(R.id.tvInfo); receiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String info = intent.getExtras().getString("data");
tvInfo.setText(info);
}
}; IntentFilter filter = new IntentFilter();
filter.addAction("com.example.timejob.service");
registerReceiver(receiver,filter);
} @Override
protected void onResume() {
super.onResume();
if(!isServiceRunning()){
Intent intent = new Intent(this,MyService.class);
this.startService(intent);
Log.d("onResume:", "服务运行");
}
} @Override
public void onStop() {
super.onStop();
if(isServiceRunning()){
Intent is = new Intent(this,MyService.class);
this.stopService(is);
Log.d("onStop:", "服务关闭");
}
this.unregisterReceiver(receiver);
} /*
* 判断service是否运行
*/
private boolean isServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.example.timejob.MyService".equals(service.service.getClassName())) {
return true;
}
}
return false;
}
}

2:MyService.java

public class MyService extends Service{
public Timer timer = null;
public String info = null; @Override
public IBinder onBind(Intent arg0) {
return null;
} @Override
public void onCreate() {
super.onCreate();
} @Override
public int onStartCommand(Intent intent, int flags, int startId) { doJob(); return super.onStartCommand(intent, flags, startId);
} private void doJob(){
timer = new Timer();
//10s触发一次
timer.schedule(new MyTimerWork(),0, 1000*10);
} private class MyTimerWork extends TimerTask{
@Override
public void run() {
info = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
Intent intent = new Intent();
intent.setAction("com.example.timejob.service");
intent.putExtra("data", info);
sendBroadcast(intent);
Log.d("MyTimerWork:", "定时任务执行:"+info);
}
} @Override
public void onDestroy() {
super.onDestroy();
if(null!=timer){
timer.cancel();
}
}
}

3:AndroidManifest.xml

 <service
android:name="com.example.timejob.MyService"
android:enabled="true"
android:exported="false" />

Android Service 通过 BroadcastReceiver 更新Activity UI的更多相关文章

  1. 浅谈android Service和BroadCastReceiver

    1.题记 Android中的服务和windows中的服务是类似的东西,服务一般没有用户操作界面,它运行于系统中不容易被用户发觉,可以使用它开发如监控之类的程序. 广播接收者(BroadcastRece ...

  2. Android Activity、Service、BroadcastReceiver 的生命周期

    Activity.Service.BroadcastReceiver这三个组建是Android开发中最常使用到的组件,在它们的生命周期的各个阶段我们需要针对性的做些事情,了解这些组件的生命周期有利于我 ...

  3. Android Service 通知Activity更新界面的方法研究

    Android Service 通知Activity更新界面的方法研究   Android的最重要的组件式service和activity,那么在使用的过程中,我们最常遇到的问题是他们之间的通信问题. ...

  4. Android:Service通知Activity更新界面

    Android有四大组件,其中包括service和activity,那么在使用的过程中,我们最常遇到的问题是他们之间的通信问题. 1.首先Activity调用Service 这个是比较基础的,它有两种 ...

  5. Android平台的四大天王:Activity, Service, ContentProvider, BroadcastReceiver

    今天开始要自学android,刚看到百度知道上面这段话,觉得不错(不过已经是2011年8月的回答了): Android系统的手机的每一个你能看到的画面都是一个activity,它像是一个画布,随你在上 ...

  6. (转)Android在子线程中更新Activity中UI的方法

    转:http://blog.sina.com.cn/s/blog_3fe961ae0100mvc5.html 在Android平台下,进行多线程编程时,经常需要在主线程之外的一个单独的线程中进行某些处 ...

  7. Android面试收集录1 Activity+Service

    1.Activity的生命周期 1.1.首先查看一下Activity生命周期经典图片. 在正常情况下,一个Activity从启动到结束会以如下顺序经历整个生命周期: onCreate()->on ...

  8. Android Service实时向Activity传递数据

    演示一个案例,需求如下:在Service组件中创建一个线程,该线程用来生产数值,每隔1秒数值自动加1,然后把更新后的数值在界面上实时显示. 步骤如下:1.新建一个android项目工程,取名为demo ...

  9. Android开发 ---ContentProvider数据提供者,Activity和Service就是上下文对象,短信监听器,内容观察者

    1.activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayou ...

随机推荐

  1. keil 中用函数指针调用函数的参数限制

    NSIC中,通过函数指针调用的函数的参数的个数没有限制,但是KeilC对此有限制,至多3个参数.因为,KeilC编译时,无法通过函数指针找到该函数的局部数据段,也就无法通过局部数据段传递参数,只能通过 ...

  2. 阵列卡,组成的磁盘组就像是一个硬盘,pci-e扩展出sata3.0

    你想提升性能,那么组RAID0,主板上的RAID应该是软RAID,肯定没有阵列卡来得稳定.如果你有闲钱,可以考虑用阵列卡. 不会的.即使不能起到RAID的作用,起码也可以当作直接连接了2个硬盘.不会影 ...

  3. Eclipse代码提示功能设置(Java & Eclipse+CDT C/C++)

    http://developer.51cto.com/art/200907/136242.htm http://blog.chinaunix.net/u/21684/showart_462486.ht ...

  4. Struts2源代码解读之Action调用

    对于Struts2源代码的分析已经有些时日了,虽然网上有很多解读代码,不过自己还是写一个放上来,供大家参考一下. 解读过程: 直接在action类中打断点(包括构造函数和待执行方法)进行debug调试 ...

  5. java链接sqlite资料整理

    0.SQLite三种JDBC驱动的区别 摘自http://blog.sina.com.cn/s/blog_654337ca01016x4n.html 在DBeaver中看到SQLite有三种JDBC驱 ...

  6. Nx32926 命令关机

    一. poweroff关机命令 ~ # poweroff ~ # baud=, quot= w-config: 8bits/char umount: devtmpfs busy - remounted ...

  7. phpcms 缓存

    PHPCMS设置和读取缓存文件 PHPCMS开发中经常用到读取文件缓存,比如常见的当前站点类别,是保存在缓存文件中的,读取的时候 用:$this->categorys = getcache(‘c ...

  8. WPF最基本的4个引用

    Windowsbase Windows基本类库 PresentationCore wpf核心类库 PresentationFramework wpf框架 System.Axml 系统类库

  9. pyqt 动态显示时间方法例子学习

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' import sys,datetime from PyQt4.QtC ...

  10. Spring Tool Suit 在Eclipse上的安装

    登录http://spring.io/tools/sts/all 下载所需的Spring Tool Suit安装包 我用的是springsource-tool-suite-3.6.1.RELEASE- ...