Android Service 的生命周期
来源:互联网 发布:征途 巨人网络 编辑:程序博客网 时间:2024/06/06 00:18
服务的启动方式
###startService
* 通过startService启动的服务,该服务所在进程会变成服务进程
* 服务与启动它的Activity,不再有一毛钱关系
###bindService
* 通过bindService启动的服务,进程优先级不变
* 绑定的服务与启动它的Activity是同生共死的,Activity销毁了,服务也要销毁,不过服务销毁了,Activity不会销毁
startService
* 开始服务,会使进程变成为服务进程
* 启动服务的activity和服务不再有一毛钱关系
* bindService
* 绑定服务不会使进程变成服务进程
* 绑定服务,是activity与服务建立连接,如果activity销毁了,服务也会被解绑并销毁,但是如果服务被销毁,activity不会被销毁
* 绑定服务和解绑服务的生命周期方法:onCreate->onBind->onUnbind->onDestroy
一、采用start的方式开启服务
onStart()过时了
开启服务: onCreate()--> onStartCommand() ---> onDestory();
如果服务已经开启,不会重复的执行onCreate(), 而是会调用onStart()和 onStartCommand();
服务停止的时候 onDestory().
服务只会被停止一次
import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.widget.Toast;public class MyService extends Service {@Overridepublic IBinder onBind(Intent arg0) {return null;}@Overridepublic void onCreate() {System.out.println("oncreate");super.onCreate();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {System.out.println("onstartcommand");return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {System.out.println("ondestory");super.onDestroy();}/** * 这是服务里面的一个方法 */public void methodInService() {Toast.makeText(this, "哈哈,我是服务里面的方法", 0).show();}}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="start" android:text="开启服务" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="stop" android:text="停止服务" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="call" android:text="调用服务里面的方法" /></LinearLayout>
public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void start(View view) {Intent intent = new Intent(this, MyService.class);// 通知框架开启服务。startService(intent);}public void stop(View view) {Intent intent = new Intent(this, MyService.class);stopService(intent);}@Overrideprotected void onDestroy() {System.out.println("啊啊啊,我是activity,我挂了");super.onDestroy();}// 调用服务里面的方法。不可以自己new服务,调用的服务的方法,必须通过框架得到服务的引用。public void call(View view) {// new MyService().methodInService();}}
二、服务还有一种开启方式,绑定的方式开启服务。
注意:bindService是异步操作
onCreate() --->onBind();--->onunbind()-->onDestory();
绑定服务不会调用onstart或者onstartcommand方法;
public class MyService extends Service {//2.实现服务成功绑定的代码 ,返回一个中间人。@Overridepublic IBinder onBind(Intent arg0) {System.out.println("服务被成功绑定了。。。。");return new MiddlePerson();}@Overridepublic boolean onUnbind(Intent intent) {System.out.println("onunbind");return super.onUnbind(intent);}@Overridepublic void onCreate() {System.out.println("oncreate");super.onCreate();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {System.out.println("onstartcommand");return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {System.out.println("ondestory");super.onDestroy();}}
public class MainActivity extends Activity {private MyConn conn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}// 绑定服务public void bind(View view) {// 3.activity采用绑定的方式去开启服务。Intent intent = new Intent(this, MyService.class);conn = new MyConn();bindService(intent, conn, BIND_AUTO_CREATE);}// 解除绑定服务public void unbind(View view) {unbindService(conn);}@Overrideprotected void onDestroy() {System.out.println("啊啊啊,我是activity,我挂了");super.onDestroy();}private class MyConn implements ServiceConnection {// 4. 当服务被连接的时候调用 服务别成功 绑定的时候调用@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {}// 当服务失去连接的时候调用(一般进程挂了,服务被异常杀死)@Overridepublic void onServiceDisconnected(ComponentName name) {}}}
三、两种开启服务方法的区别。
start方式开启服务。 一旦服务开启跟调用者(开启者)就没有任何关系了。
开启者退出了,开启者挂了,服务还在后台长期的运行。
开启者没有办法去调用服务里面的方法。(美国的司法独立)
bind的方式开启服务,绑定服务,调用者挂了,服务也会跟着挂掉。不求同时生,但求同时死。
开启者可以调用服务里面的方法。
参考:
绑定本地Service和远程Service并调用其中方法
混合调用Service(Service既能后台长期运行,又能调用Service中方法)
0 0
- Android Service的生命周期
- Android 的Service生命周期
- Android Service 的生命周期
- Android Service的生命周期
- Android的Service生命周期
- Android Service的生命周期
- Android Service的生命周期
- Android Service的生命周期
- Android Service的生命周期
- Android Service的生命周期
- Android-Service的生命周期
- Android Service的生命周期
- Android Service的生命周期
- Android Service 的生命周期
- Android Service的生命周期
- Android Service的生命周期
- Android Service的生命周期
- Android Service的生命周期
- 【POJ 1988 Cube Stacking】+ 并查集
- PHP基础(1)---认识PHP
- POJ3480John博弈
- 蓝桥杯 基础练习 查找整数 JAVA
- 请求重定向和请求转发的区别
- Android Service 的生命周期
- H5项目常见问题汇总及解决方案
- 树莓派开机自启动
- L
- PostgreSQL on Linux 最佳部署手册
- 《C程序设计语言》读书笔记(六)——结构
- C语言实现推箱子游戏
- Android精简apk【转】
- 面向接口编程