和activity中互相传值类似

在activity中

Intent regIntent = new Intent(this, ChatService.class);
  regIntent.putExtra("student_id", student_id);
  startService(regIntent);

然后再service中的onStart函数中获取该值

student_id=intent.getStringExtra("student_id");
  System.out.println("sevice_student_id---------------"+student_id);

当然写到这里还是不能传的,不然会报错!!!

我们需要在Mainfeist文件中注册这个service

销毁Service写在activity的onDestroy方法里:

@Override
 protected void onDestroy() {
   ChatActivity.this.stopService(new
   Intent(ChatActivity.this,
   ChatService.class));
  super.onDestroy();
 }

补充:

也可以从public int onStartCommand(Intent intent, int flags, int startId)中取出从activity中传过来的值。intent.getExtra()获得bundle对象,可从中取值。
也可以用bindService(intent, conn,BIND_AUTO_CREATE);传值,把要传的值绑定在intent里,在service的public IBinder onBind(Intent intent) 方法里取得intent。
可以在service里面注册一个广播,在activity里sendbroadcast(intent)传值。

android中activity向service中传值的更多相关文章

  1. Android中Activity、Service和线程之间的通信

    Activity.Service和线程应该是Android编程中最常见的几种类了,几乎大多数应用程序都会涉及到这几个类的编程,自然而然的,也就会涉及到三者之间的相互通信,本文就试图简单地介绍一下这三者 ...

  2. Android中Activity和Service的数据通讯

    在Android中,我们通常需要Activity跟Service进行通讯,很多人只知道Activity掉用Service,却不知道Service如何将数据返回给Activity.其实Service返回 ...

  3. android中activity和service是否在同一个进程中

    分两种情况,如果是本地线程,肯定是同一个进程中的, 如果是远程服务,那么activity和service将在不同的进程中的 ----- 非远程服务,和Activity属于同一个进程和线程:而远程服务和 ...

  4. android学习-Activity和Service的生命周期

    详细请跳转原网页Activity和Service的生命周期(图) 不解释,不懂算我输 Activity的生命周期(图) Service的声明周期

  5. Tabhost中Activity绑定Service

    在android中,一个Activity绑定一个Service组件我们一般用Context().bindService方法就能够.可是假设这个 Activity属于一个Tabhost的话就不行了,在网 ...

  6. Android——用Activity和Service实现简单的音乐播放器

    一.只用Activity 容易出现问题 xml <?xml version="1.0" encoding="utf-8"?> <LinearL ...

  7. Android之Activity与Service通信

    一.当Acitivity和Service处于同一个Application和进程时,通过继承Binder类来实现. 当一个Activity绑定到一个Service上时,它负责维护Service实例的引用 ...

  8. android菜鸟学习笔记28----Android中的Service生命周期及本地和远程服务绑定的实现

    Service是Android中长期在后台运行的没有界面的组件,使用服务的优势在于:能够提高进程的优先级,系统不容易回收掉进程,即便回收了,内存充足的时候,会把进程重新创建. 1.服务的简单使用示例: ...

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

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

随机推荐

  1. spring aop的两种写法aspect和advisor

    本文转自:https://www.cnblogs.com/leiOOlei/p/3709607.html 首先看个例子,如下 接口代码: package com.lei.demo.aop.schema ...

  2. java 多态的好处

    /* 对象的多态性. class 动物{} class 猫 extends 动物{} class 狗 extends 动物{} 猫 x = new 猫(); 动物 x = new 猫();//一个对象 ...

  3. SlackWare安装

     Keep It Simple Stupid 01.下载 slackware: http://www.slackware.com/ 中科大:    http://mirrors.ustc.edu.cn ...

  4. 原创:微信小程序页面跳转展示缓冲提示

    1.在JS文件中,直接加上下面代码即可: //缓冲提醒 wx.showToast({ title: '加载中', icon: 'loading', duration: 400 })

  5. zabbix 触发器描述列表count

    触发器描述列表: count: count(600): 最近10分钟的值的个数 count(600,12): 最近10分钟,值等于12的个数 count(600,12,"gt"): ...

  6. 架构-LAMP特级学习(网站服务器监控)

    1.服务监控(SNMP配合CACTI监控) Apache Web服务监控 MySQL数据库监控 磁盘空间监控 2.流量监控(SNMP配合MRTG监控) 网站流量监控 3.使用SNMP可以获取被监控服务 ...

  7. webview使用遇到 It is possible that this object was over-released, or is in the process of deallocation错误的解决办法

    使用wekwebview时,push后,再pop返回,报错了: Cannot form weak reference to instance (xxxx) of class xxxx. It is p ...

  8. eclipse新建maven工程的各种坑

    尽量按照最后强烈推荐的那篇创建maven工程.  1.jsp文件头报错 2.xml配置文件头红叉 3.Archive for required library...blabla 4.pom依赖出错 5 ...

  9. MQTT Stresser

    go环境请参考https://www.cnblogs.com/saryli/p/9833253.html Load testing tool to stress MQTT message broker ...

  10. sqlserver中将varchar类型转换为int型再进行排序的方法

    sql中把varchar类型转换为int型然后进行排序,如果我们数据库的ID设置为varchar型的 在查询的时候order by id的话 如果我们数据库的ID设置为varchar型的 在查询的时候 ...