Android有四大组件,其中包括service和activity,那么在使用的过程中,我们最常遇到的问题是他们之间的通信问题。

1.首先Activity调用Service

这个是比较基础的,它有两种常见的方法;

1. 通过Intent

可以指定package name和class name的方式来调用,Intent.setClassName这个成员即可。通过putString来装载数据,startService(intent)即可例子如下:

Intent regIntent = new Intent(“com.service”);
regIntent.putExtra(“data”, "helloData");
startService(regIntent);

2.通过IPC

IPC有点复杂,想要了解的可以去查有关IBinder的一些资料。

2.Service将状态告诉Activity

方法有两种

1.service 通过广播的形式发送broadcast

我们写一个broadcastReceiver即可,通常的情况,将broadcastReceiver写成Activity的内部类,这个onReceiver可以直接调用activity的方法来更新界面。但是内部类只能采用代码注册的方法registerReceiver(),不能再AndroidManifest.xml文件中进行静态的声明,因为内部类要依赖于外部类而存在的。如果你一定要用AndroidManifest来注册receiver,那么只能把broadcastReceiver写成单独的文件的public类。这时候,你想更新界面就比较麻烦了,你只能自己把你要更新的activity运行起来,然后再向这个activity的内部类发广播的消息来更新界面

2.service直接向activity发intent

在service里面进行startActivity是属于在Activity外startActivity即在task外启动activity,因此,必须在intent加入一个参数如下:

Intent intentSend = new Intent(Constants.ACTION_STATUS);

intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

intent1.putExtra(“statues“,“end“);

context.startActivity(intent1);

但是此时会引发的一个问题是,多次startActivity会导致很多的activity实现被运行,这肯定不是我们要的,我只要一个Activity就可,此时,我们要在androidManifest里面对这个activity的launchMode设置为singleInstance

<activity android:name="com.demo.Activity"
android:label="@string/online" android:launchMode="singleInstance">

记住啦,有人设置为singleTask,也可以,但他们有一点区别。

同时记住要更新intent,这样getInstent才可以得到每次的新实例

@Override

protected void onNewIntent (Intent intent){

    setIntent(intent);

}

Android:Service通知Activity更新界面的更多相关文章

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

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

  2. Android—Service与Activity的交互

    service-Android的四大组件之一.人称"后台服务"指其本身的运行并不依赖于用户可视的UI界面 实际开发中我们经常需要service和activity之间可以相互传递数据 ...

  3. Android Service与Activity之间通信的几种方式

    在Android中,Activity主要负责前台页面的展示,Service主要负责需要长期运行的任务,所以在我们实际开发中,就会常常遇到Activity与Service之间的通信,我们一般在Activ ...

  4. Android Service与Activity之间通信

    主要分为: 通过Binder对象 通过broadcast(广播)的形式 Activity调用bindService (Intent service, ServiceConnection conn, i ...

  5. Android Service 通过 BroadcastReceiver 更新Activity UI

    1:MainActivity.java public class MainActivity extends Activity { private TextView tvInfo = null; pri ...

  6. Android Service与Activity的交互

    Android中有时候需要在Service中改变Activity的UI,或者在Activity中修改Service中的数值.首先必须使用与Activity绑定的Service,有三种方式可以实现.第一 ...

  7. android service和activity的通讯

    我们须要用下面方式来启动service: </pre><pre name="code" class="java"><span st ...

  8. eclipse Android 开发基础 Activity 窗体 界面

    eclipse Android 开发基础 新建工程 新建布局layout,new Android Activity就相当于窗体Form. 新建Activity自动生成src下同名的java代码. pu ...

  9. Service 启动Activity

    1, 在BroadcastReceiver中启动Activity的问题  *  * 如果在BroadcastReceiver的onReceive()方法中如下启动一个Activity  * Inten ...

随机推荐

  1. yolo-开源数据集coco kitti voc

    1.kitti数据集(参考博客:https://blog.csdn.net/jesse_mx/article/details/65634482  https://blog.csdn.net/baoli ...

  2. Batch Normalization层

    Batch Normalization的加速作用体现在两个方面:一是归一化了每层和每维度的scale,所以可以整体使用一个较高的学习率,而不必像以前那样迁就小scale的维度:二是归一化后使得更多的权 ...

  3. POJ2274(后缀数组应用)

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 25272   Accepted: 10 ...

  4. JavaScript-Tool:validate.js-un

    ylbtech-JavaScript-Tool:validate.js 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 0. http://validatejs. ...

  5. 配置哨兵监控Redis运行情况

    Redis的主从架构,如果master发现故障了,还得手动将slave切换成master继续服务,手动的方式容易造成失误,导致数据丢失,那Redis有没有一种机制可以在master和slave进行监控 ...

  6. window.open全屏

    window.open全屏   1. window.open(url,'资金计划项超支提醒','width='+(window.screen.availWidth-10)+',height='+(wi ...

  7. socket学习目录

    深入探析c# Socket http://www.cnblogs.com/tianzhiliang/archive/2010/09/08/1821623.html Http和Socket连接区别 ht ...

  8. Asset Catalog Help (四)---Adding an iOS App Icon Set or Launch Image Set

    Adding an iOS App Icon Set or Launch Image Set Organize different resolutions of your app icons and ...

  9. spring使用过程中遇到的问题

    1.出现这样的错误:The type org.springframework.core.NestedRuntimeException cannot be resolved. It is indirec ...

  10. python 之元类

    定义类的两种方法: 1.class定义 2.type(类名,类的基类们,类的名称空间) # 定义类的三要素:类名.基类.名称空间 class_name = 'Chinese' class_bases ...