在Application关闭后,Service仍然会运行。

package com.example.servdemo;

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.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build; public class MainActivity extends Activity implements OnClickListener { private Button btnStartService, btnStopService;
private Intent serviceIntent; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); serviceIntent = new Intent(this, EchoService.class); btnStartService = (Button) findViewById(R.id.btnStartService);
btnStopService = (Button) findViewById(R.id.btnStopService); btnStartService.setOnClickListener(this);
btnStopService.setOnClickListener(this);
} @Override
public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public void onClick(View v) { switch (v.getId()) {
case R.id.btnStartService:
startService(serviceIntent);
break; case R.id.btnStopService:
stopService(serviceIntent);
break;
}
} }
package com.example.servdemo;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder; public class EchoService extends Service { @Override
public IBinder onBind(Intent intent) {
return null;
} @Override
public void onCreate() {
// TODO Auto-generated method stub
System.out.println("Service Start!");
super.onCreate();
} @Override
public void onDestroy() {
// TODO Auto-generated method stub
System.out.println("Service Stop!");
super.onDestroy();
} }

02_启动和销毁Service的更多相关文章

  1. Android开机启动Activity或者Service方法

    本文出自 “Bill_Hoo专栏” 博客,请务必保留此出处http://billhoo.blog.51cto.com/2337751/761230 这段时间在做Android的基础开发,现在有一需求是 ...

  2. Android开机启动Activity或者Service方法(转载)

    这段时间在做Android的基础开发,现在有一需求是开机启动,按照网上某些博文教程做了下,始终不成功,一开机总是提示所启动的应用程序意外终止,于是参考了Android SDK doc,终于解决问题,下 ...

  3. 启动与销毁Activity

    启动与销毁Activity 编写:kesenhoo - 原文:http://developer.android.com/training/basics/activity-lifecycle/start ...

  4. android中如何在系统启动的时候启动自己的service

    自定义一个broadcastreciver在去接受系统启动消息,然后在处理的时候启动自己的service即可

  5. 无法启动 Diagnostic Policy Service(服务错误 1079)的解决方案

    问题 在services.msc中手动启动 Diagnostic Policy Service 时,弹出以下提示: ---------------------------服务------------- ...

  6. 6) 十分钟学会android--Activity的生命周期之启动与销毁

    当用户导航.退出和返回您的应用时,应用中的 Activity 实例将在其生命周期中转换不同状态. 例如,当您的Activity初次开始时,它将出现在系统前台并接收用户焦点. 在这个过程中,Androi ...

  7. vmware无法安装vmware authorization&windows无法启动VMware Authorization Service服务

    在vmware安装过程中或更新时,时常遇到vmware无法安装vmware authorization&windows无法启动VMware Authorization Service服务的情况 ...

  8. 启动、关闭Service

    //获取程序界面中的start.stop两个按钮 start = (Button) findViewById(R.id.start); stop = (Button) findViewById(R.i ...

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

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

随机推荐

  1. [Luogu P2278] [HNOI2003]操作系统

    题面 传送门:https://www.luogu.org/problemnew/show/P2278 Solutiton 挺简单的一道模拟题,拿堆模拟一下题目意思就好 堆中有两个关键字,分别是优先级和 ...

  2. frida打印与参数构造

    title: frida打印与参数构造 categories: 逆向与协议分析 toc: true mathjax: true tags: frida HOOK 逆向 widgets: type: t ...

  3. 利用GitHub和Hexo打造免费的个人博客

    每个程序猿都需要一个个人博客,目前广泛出现在大家视野里的有CSDN.博客园.简书,但是他们却没有给用户一个专属的站点.一个好记的域名.你需要一个https://xxx.xxx.xxx/格式的网址,一个 ...

  4. vi&vim 基本使用方法

    vi/&vim 基本使用方法 本文介绍了vi (vim)的基本使用方法,但对于普通用户来说基本上够了!i/vim的区别简单点来说,它们都是多模式编辑器,不同的是vim 是vi的升级版本,它不仅 ...

  5. Redis下载

    Windows版下载地址 https://github.com/tporadowski/redis/releases Linux版下载地址 https://redis.io/download

  6. 基于selenium微博个人主页视频下载

    # -*- coding: utf-8 -*- import selenium from selenium import webdriver import time import urllib.req ...

  7. Spring源码之注解的原理

    https://blog.csdn.net/qq_28802119/article/details/83573950 https://www.zhihu.com/question/318439660/ ...

  8. Spring源码之AbstractApplicationContext中refresh方法注释

    https://blog.csdn.net/three_stand/article/details/80680004 refresh() prepareRefresh(beanFactory) 容器状 ...

  9. Python_爬虫_BeautifulSoup网页解析库

    BeautifulSoup网页解析库 from bs4 import BeautifulSoup 0.BeautifulSoup网页解析库包含 的 几个解析器 Python标准库[主要,系统自带;] ...

  10. webug第十六关:明天双十一

    ---恢复内容开始--- 第十六关:明天双十一 不说了...只能看着源码做出来 ---恢复内容结束---