activity_main

<RelativeLayout 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" > <Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/stop"
android:layout_centerHorizontal="true"
android:layout_marginBottom="44dp"
android:text="开启服务" /> <Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/start"
android:layout_alignParentBottom="true"
android:layout_marginBottom="163dp"
android:text="关闭服务" /> </RelativeLayout>

MainActivity

package com.example.android_service;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build; public class MainActivity extends Activity { private Button stop,start;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); start=(Button)this.findViewById(R.id.start);
stop=(Button)this.findViewById(R.id.stop);
final Intent intent=new Intent();
intent.setAction("dashu.test.service");
start.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startService(intent);
}
});
stop.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
stopService(intent);
}
});
}
}

MyService

package com.example.android_service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log; public class MyService extends Service { private final static String TAG = "dashu"; @Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
} // Service创建时候回调
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.d(TAG, "-->服务创建");
} //服务启动时候回调
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.d(TAG, "-->服务启动");
return super.onStartCommand(intent, flags, startId);
} //服务关闭时候回调
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d(TAG, "-->服务关闭");
} }

 

使用context.startService()
启动Service是会会经历:
context.startService()  ->onCreate()- >onStart()->Service
running
context.stopService() | ->onDestroy() ->Service stop 
 
假设Service还没有执行,则android先调用onCreate()然后调用onStart();假设Service已经执行,则仅仅调用onStart()。所以一个Service的onStart方法可能会反复调用多次。 
 
stopService的时候直接onDestroy,假设是调用者自己直接退出而没有调用stopService的话,Service会一直在后台执行。该Service的调用者再启动起来后能够通过stopService关闭Service。
 
所以调用startService的生命周期为:onCreate --> onStart(可多次调用) -->
onDestroy

启动和停止Service的更多相关文章

  1. Android四大组件之Service --- 如何启动和停止Service?

    启动和停止方法主要是通过Intent来实现 以上一篇中的ServiceTest项目为例来启动和停止MyService这个服务 首先修改activity_main.xml中的代码,如下所示:<Li ...

  2. Service的启动与停止、绑定与解绑

    ---恢复内容开始--- Service的意义就在于当软件停止之后还可以在背景中进行运行,换句话也就是说,比如一个音乐播放器,当我们退出音乐播放器的时候,还是希望它在背景中运行,也就是一直播放着音乐, ...

  3. 如何把apache和nginx 加入到系统服务,用service 命令来控制启动、停止

    1 把apache 加入到系统服务,即用service 命令来控制Apache 启动.停止  如果Linux服务器上默认安装了httpd的话(用rpm -qa|grep httpd查看),那你就可以用 ...

  4. Windows Service 学习系列(二):C# windows服务:安装、卸载、启动和停止Windows Service几种方式

    一.通过InstallUtil.exe安装.卸载.启动.停止Windows Service 方法一 1.以管理员身份运行cmd 2.安装windows服务 切换cd C:\Windows\Micros ...

  5. 初学Android,创建,启动,停止Service(五十八)

    Service跟Windows系统里的服务概念差不多,都在后台执行,它跟Activity的最大区别就是,它是无界面的 开发Service与开发Activity的步骤类似 1.定义一个继承Service ...

  6. C#操作注册服务卸载服务启动服务停止服务.. .

    using Microsoft.Win32; using System; using System.Collections; using System.Collections.Generic; usi ...

  7. linux 下 apache相关;启动、停止、重启命令;配置文件位置等等

    linux 下 apache启动.停止.重启命 基本的操作方法: 本文假设你的apahce安装目录为/usr/local/apache2,这些方法适合任何情况 apahce启动命令: 推荐/usr/l ...

  8. MySQL 安装和启动服务,“本地计算机 上的 MySQL 服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止。”

    MySQL 安装和启动服务,以及遇到的问题 MySQL版本: mysql-5.7.13-winx64.zip (免安装,解压放到程序文件夹即可,比如 C:\Program Files\mysql-5. ...

  9. Linux上服务的启动,停止和重启

    (1)查看所有的服务 [berry@berry:practice] service Usage: service < option > | --status-all | [ service ...

随机推荐

  1. (数据结构整理)NJUPT1054

    这一篇博客以一些OJ上的题目为载体,整理一下数据结构.会陆续的更新. .. 我们都知道,数据结构的灵活应用有时能让简化一些题目的解答. 一.栈的应用 1.NJUPT OJ 1054(回文串的推断) 回 ...

  2. 曲根英语万词---二、evoke

    曲根英语万词---二.evoke 一.总结 一句话总结:evoke v.唤起,引起 词根:-voc-, -vok- [词根含义]:声音,叫喊 1.consecrate? v,供奉,奉为神圣 -ate, ...

  3. POJ 3173 模拟

    按照题意模拟就好-- //By SiriusRen #include <cstdio> #include <algorithm> using namespace std; in ...

  4. java9新特性-8-语法改进:钻石操作符(Diamond Operator)使用升级

    1.使用说明 我们将能够与匿名实现类共同使用钻石操作符(diamond operator) 在java8中如下的操作是会报错的:   编译报错信息:'<>' cannot be used ...

  5. error C4996: 'setmode': The POSIX name for this item is deprecated解决方案

    在使用VS2012编译zlib库官方提供的案例程序 zpipe.c 中代码时报错: 信息如下: 错误 1 error C4996: 'setmode': The POSIX name for this ...

  6. TabLayout禁止选择

    项目中有个页面上面是TabLayout下面是Listview,选择TabLayout的选项卡更新下面Listview里面的数据,在请求的时候想禁用TabLayout选项卡来避免用户频繁点击选项卡造成L ...

  7. codeforces 527 C Glass Carving

    Glass Carving time limit per test 2 seconds Leonid wants to become a glass carver (the person who cr ...

  8. WIFI 概览

      概览 Android 提供默认 Android 框架实现,其中包括对各种 WLAN 协议和模式的支持,这些协议和模式包括: WLAN 基础架构 (STA) 网络共享模式或仅限本地模式下的 WLAN ...

  9. 我的PHP学习之路

    由于工作中,做微信小程序需要我自己写一些后台代码.并且公司后台用的是php.所以我决定在周末和下班后抽空学习php.一开始,我想找一些入门视频来学,然后发现好像效率不是很好.不如看书来得痛快.(主要是 ...

  10. 入门python:《Python编程从入门到实践》中文PDF+英文PDF+代码学习

    入门python推荐学习久负盛名的python入门书籍<Python编程从入门到实践>. 书中涵盖的内容是比较精简的,没有艰深晦涩的概念,最重要的是每个小结都附带有"动手试一试& ...