android中activity向service中传值
和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中传值的更多相关文章
- Android中Activity、Service和线程之间的通信
Activity.Service和线程应该是Android编程中最常见的几种类了,几乎大多数应用程序都会涉及到这几个类的编程,自然而然的,也就会涉及到三者之间的相互通信,本文就试图简单地介绍一下这三者 ...
- Android中Activity和Service的数据通讯
在Android中,我们通常需要Activity跟Service进行通讯,很多人只知道Activity掉用Service,却不知道Service如何将数据返回给Activity.其实Service返回 ...
- android中activity和service是否在同一个进程中
分两种情况,如果是本地线程,肯定是同一个进程中的, 如果是远程服务,那么activity和service将在不同的进程中的 ----- 非远程服务,和Activity属于同一个进程和线程:而远程服务和 ...
- android学习-Activity和Service的生命周期
详细请跳转原网页Activity和Service的生命周期(图) 不解释,不懂算我输 Activity的生命周期(图) Service的声明周期
- Tabhost中Activity绑定Service
在android中,一个Activity绑定一个Service组件我们一般用Context().bindService方法就能够.可是假设这个 Activity属于一个Tabhost的话就不行了,在网 ...
- Android——用Activity和Service实现简单的音乐播放器
一.只用Activity 容易出现问题 xml <?xml version="1.0" encoding="utf-8"?> <LinearL ...
- Android之Activity与Service通信
一.当Acitivity和Service处于同一个Application和进程时,通过继承Binder类来实现. 当一个Activity绑定到一个Service上时,它负责维护Service实例的引用 ...
- android菜鸟学习笔记28----Android中的Service生命周期及本地和远程服务绑定的实现
Service是Android中长期在后台运行的没有界面的组件,使用服务的优势在于:能够提高进程的优先级,系统不容易回收掉进程,即便回收了,内存充足的时候,会把进程重新创建. 1.服务的简单使用示例: ...
- Android开发 ---ContentProvider数据提供者,Activity和Service就是上下文对象,短信监听器,内容观察者
1.activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayou ...
随机推荐
- 关于NHibernate中存在于Session中实例的3种状态的简单分析
在使用NHibernate的时候.在Session中会有3种状态. 1. 瞬时状态 (Transient) 由 new 命令开辟内存空间的对象,也就是平时所熟悉的普通对象. 如: Student st ...
- Linux文件大小排序
ls 文件详情 ls -S / ###从大到小排序ls -Sr / ###从小到大排就加个-r -r 翻转ls -aSr / ...
- Java任务调度框架Quartz
转自:http://blog.csdn.net/yuebinghaoyuan/article/details/9045471 介绍 Quartz is a full-featured, open ...
- HDUOJ-----1098 Ignatius's puzzle
Ignatius's puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDUOJ---Can you solve this equation?
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- python网络编程详解
最近在看<UNIX网络编程 卷1>和<FREEBSD操作系统设计与实现>这两本书,我重点关注了TCP协议相关的内容,结合自己后台开发的经验,写下这篇文章,一方面是为了帮助有需要 ...
- C#将Access数据库导出为JSON
一个Access数据库包含若干首诗歌,每首诗有content.author.title.description四个字段 using System; using System.Data; using S ...
- shell脚本条件判断
http://blog.csdn.net/ws_zll/article/details/7515310
- Android 系统 root 破解原理分析
现在Android系统的root破解基本上成为大家的必备技能!网上也有很多中一键破解的软件,使root破解越来越容易.但是你思考过root破解的 原理吗?root破解的本质是什么呢?难道是利用了Lin ...
- iOS设备的标识---UUID和IDFA
一:1.1 :UDID 简介:UDID的全称是Unique Device Identifier,顾名思义,它就是苹果IOS设备的唯一识别码,它由40个字符的字母和数字组成.在很多需要限制一台设备一个账 ...