android服务之启动方式
服务有两种启动方式
- 通过startService方法来启动
- 通过bindService来开启服务
布局文件
在布局文件中我们定义了四个按键来测试这两种方式来开启服务的不同
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="开始服务"
android:onClick="start"/>
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="停止服务"
android:onClick="stop"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="绑定服务"
android:onClick="bind"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="解绑"
android:onClick="unbind"/>
</LinearLayout>
Activity
在Activity中我们使用了这两种方式来开启服务
package xidian.dy.com.chujia; import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast; public class MainActivity extends AppCompatActivity {
Intent service;
MyConnection mc;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
service = new Intent(this, MyService.class);
mc = new MyConnection();
} public void start(View v){
Toast.makeText(this,"开启服务",Toast.LENGTH_SHORT).show();
startService(service);
} public void stop(View v){
Toast.makeText(this,"关闭服务",Toast.LENGTH_SHORT).show();
stopService(service);
} public void bind(View v){
bindService(service,mc,BIND_AUTO_CREATE);
} public void unbind(View v){
unbindService(mc);
} class MyConnection implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) { } @Override
public void onServiceDisconnected(ComponentName name) { }
}
}
注意在绑定服务的时候需要传入回调函数(第二个参数),当绑定成功会调用onServiceConnected方法,当解除绑定会调用onServiceDisconnected方法。
Service
在服务代码中我们将各个方法复写了一边,其实代码中各个方法也表示了服务的生命周期。
package xidian.dy.com.chujia; import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log; /**
* Created by dy on 2016/7/12.
*/
public class MyService extends Service {
@Override
public void onCreate() {
super.onCreate();
Log.i(this.getClass().getName(), "****start方法****");
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(this.getClass().getName(), "*****startCommand方法*****");
return super.onStartCommand(intent, flags, startId);
} @Override
public void onDestroy() {
Log.i(this.getClass().getName(), "***Destroy方法***");
super.onDestroy();
} @Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i(this.getClass().getName(), "****Bind方法被调用****");
return null;
} @Override
public boolean onUnbind(Intent intent) {
Log.i(this.getClass().getName(),"****unBind方法被调用****");
return super.onUnbind(intent);
}
}
两者区别

启动服务
- 点击开始服务

- 点击返回按键销毁Activity,我们发现服务仍在运行

- 点击停止服务

绑定服务
- 点击绑定服务

- 点击返回按键销毁Activity,则会抛出异常并且有两方法被调用


通过对比我们发现startService开启的服务是独立运行的,而通过bindService开启的服务则依赖于Activity,但Activity被销毁时绑定的服务也被销毁了
android服务之启动方式的更多相关文章
- android activity的启动方式
1.Standard正常启动,默认的启动方式,没什么说头 2.SingleTop 意思就是在栈顶只能存在一个相同的activity 不能叠加,如果再A上继续启动A的话,只会调用A的onNewInten ...
- android service两种启动方式
android service的启动方式有以下两种: 1.Context.startService()方式启动,生命周期如下所示,启动时,startService->onCreate()-> ...
- Android service介绍和启动方式
1.Android service的作用: service通常是用来处理一些耗时操作,或后台执行不提供用户交互界面的操作,例如:下载.播放音乐. 2.Android service的生命周期: ser ...
- Android服务之PackageManagerService启动源码分析
了解了Android系统的启动过程的读者应该知道,Android的所有Java服务都是通过SystemServer进程启动的,并且驻留在SystemServer进程中.SystemServer进程在启 ...
- Android界面组件的四种启动方式
Android界面组件启动有四种方式 standard,singleTop,singleTask,singleInstance. standard:每次调用都会都会产生新的组件. singletop: ...
- Android Init进程命令的执行和服务的启动
这里开始分析init进程中配置文件的解析,在配置文件中的命令的执行和服务的启动. 首先init是一个可执行文件,它的对应的Makfile是init/Android.mk. Android.mk定义了i ...
- Android系统Surface机制的SurfaceFlinger服务的启动过程分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8022957 在前面一篇文章中,我们简要介绍了A ...
- Android Activity 的四种启动模式 lunchMode 和 Intent.setFlags();singleTask的两种启动方式。
原文:Android Activity 的四种启动模式 lunchMode 和 Intent.setFlags();singleTask的两种启动方式. Android Activity 的四种启动模 ...
- Hive 学习笔记(启动方式,内置服务)
一.Hive介绍 Hive是基于Hadoop的一个数据仓库,Hive能够将SQL语句转化为MapReduce任务进行运行. Hive架构图分为以下四部分. 1.用户接口 Hive有三个用户接口: 命令 ...
随机推荐
- Linux的文件时间
在windows下,一个文件有:创建时间.修改时间.访问时间.而在Linux下,一个文件也有三种时间,分别是:访问时间.修改时间.状态改动时间. 1.访问时间,读一次这个文件的内容,这个时间就会更新. ...
- sql in按照指定顺序排序
如下 Select * FROM table1 ,,,,) order by field (3,5,1,4,2)
- 磁盘配额-----quota
为什么要使用磁盘配额:为了限制普通用户使用普通磁盘的空间与创建文件的个数等. 不至于个别人的浪费影响所有人的使用. 需要安装quota的软件包. mount -o usrquota,grpquota ...
- 【2016-10-13】【坚持学习】【Day4】【virtual 虚函数】
定义一个基类,有一个虚函数 定义三个子类,分别继承,重写,New,这个虚函数 abstract class Test { public virtual void Prinf() { Console ...
- 《Python核心编程》部分错误纠正(勘误表)(持续更新)
Chapter 3: 例3-1 makeTextFile.py #!/usr/bin/env python 'makeTextFile.py' import os ls = os.linesep #g ...
- POJ3621Sightseeing Cows[01分数规划 spfa(dfs)负环 ]
Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9703 Accepted: 3299 ...
- 微软云Azure Website 远程调试
微软云Azure Website 远程调试 是可以的 但是只有48小时,要在后台开启,所以还是很麻烦的啊! 但是安全性提高了,不得不承认哦
- 穷举、迭代、以及while代替for循环的使用
for循环的穷举: 就是所有情况走一遍,使用if筛选出符合的情况. while循环分为2个格式 (1)先判断再做while(){}(2)不管对错,先做了在判断do{}whlie() 百鸡百钱的whil ...
- Mysql的二进制日志binlog的模式说明
binlog模式总共可分为以下三种:row,statement,mixed 1.Row日志中会记录成每一行数据被修改的形式,然后在slave端再对相同的数据进行修改,只记录要修改的数据,只有value ...
- 在SecureCRT中使用rz和sz传输文件
首先检查Centos中有没有安装 lrzsz sudo yum install lrzsz 使用yum install的时候碰到一个问题, 不知道是否和虚拟机环境有关 Existing lock /v ...