服务有两种启动方式

  • 通过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服务之启动方式的更多相关文章

  1. android activity的启动方式

    1.Standard正常启动,默认的启动方式,没什么说头 2.SingleTop 意思就是在栈顶只能存在一个相同的activity 不能叠加,如果再A上继续启动A的话,只会调用A的onNewInten ...

  2. android service两种启动方式

    android service的启动方式有以下两种: 1.Context.startService()方式启动,生命周期如下所示,启动时,startService->onCreate()-> ...

  3. Android service介绍和启动方式

    1.Android service的作用: service通常是用来处理一些耗时操作,或后台执行不提供用户交互界面的操作,例如:下载.播放音乐. 2.Android service的生命周期: ser ...

  4. Android服务之PackageManagerService启动源码分析

    了解了Android系统的启动过程的读者应该知道,Android的所有Java服务都是通过SystemServer进程启动的,并且驻留在SystemServer进程中.SystemServer进程在启 ...

  5. Android界面组件的四种启动方式

    Android界面组件启动有四种方式 standard,singleTop,singleTask,singleInstance. standard:每次调用都会都会产生新的组件. singletop: ...

  6. Android Init进程命令的执行和服务的启动

    这里开始分析init进程中配置文件的解析,在配置文件中的命令的执行和服务的启动. 首先init是一个可执行文件,它的对应的Makfile是init/Android.mk. Android.mk定义了i ...

  7. Android系统Surface机制的SurfaceFlinger服务的启动过程分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8022957 在前面一篇文章中,我们简要介绍了A ...

  8. Android Activity 的四种启动模式 lunchMode 和 Intent.setFlags();singleTask的两种启动方式。

    原文:Android Activity 的四种启动模式 lunchMode 和 Intent.setFlags();singleTask的两种启动方式. Android Activity 的四种启动模 ...

  9. Hive 学习笔记(启动方式,内置服务)

    一.Hive介绍 Hive是基于Hadoop的一个数据仓库,Hive能够将SQL语句转化为MapReduce任务进行运行. Hive架构图分为以下四部分. 1.用户接口 Hive有三个用户接口: 命令 ...

随机推荐

  1. Linux运行等级,根目录,文件类型etc

    Linux的Runlevel Linux默认有6个Terminal(Ctrl+Alt+F1~F6)和1个X window(Ctrl+Alt+F7,图形化界面)让用户登录 Linux的runlevel一 ...

  2. @RestController注解下返回到jsp视图页面

    spring4.1中添加了@RestController注解很方便,集成了@ResponseBody注解,无需再在每个方法前添加了..但是却发现个问题..之前用@Controller注解的时候经常会如 ...

  3. django用户登录和注销

    django版本:1.4.21. 一.准备工作 1.新建项目和app [root@yl-web-test srv]# django-admin.py startproject lxysite [roo ...

  4. WinCE项目应用之RM905a+活度计远程检定方法研究

    前文<RM905a+医用放射性核素活度计>中已经提到,基于WinCE5.0系统的RM905a+可以很方便的实现远程界面显示和控制.所以远程检定的主要工作在于服务器端的业务部分.基于< ...

  5. ural One-two, One-two 2

     One-two, One-two 2 Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  6. 微软云Azure Website 远程调试

    微软云Azure Website 远程调试 是可以的 但是只有48小时,要在后台开启,所以还是很麻烦的啊! 但是安全性提高了,不得不承认哦

  7. 在ios android设备上使用 Protobuf (使用dll方式)

    http://game.ceeger.com/forum/read.php?tid=13479 如果你的工程可以以.Net 2.0 subset模式运行,请看这个帖子中的方法. 地址:http://g ...

  8. java 27 - 3 反射之 通过反射获取构造方法并使用

    类 Constructor<T>:提供关于类的单个构造方法的信息以及对它的访问权限. 通过反射的方法获取构造方法并使用  ps:先忽略泛型 A.1:获取构造方法的数组: public Co ...

  9. windows下使用 linux命令好办法

    1. 安装下载 CygwinPortable一键安装包.7z 2. 把安装路径下/  [D:\cygwinportable\CygwinPortable\App\Cygwin\bin] 加到 Path ...

  10. android 调用系统的音乐和视频播放器

    package com.eboy.testsystemaudiovideo; import android.app.Activity;import android.content.Intent;imp ...