在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. PHP博客

    创建数据库 用户表 blog_user userid int 用户id username varchar(50) 用户名 password varchar(30) 密码 type tinyint(2) ...

  2. leetcode75:search-a-2d-matrix

    题目描述 请写出一个高效的在m*n矩阵中判断目标值是否存在的算法,矩阵具有如下特征: 每一行的数字都从左到右排序 每一行的第一个数字都比上一行最后一个数字大 例如: 对于下面的矩阵: [ [1, 3, ...

  3. SQL:获取每个key下最新创建的记录

    今天遇到了一个好玩的问题 问题: 有一个含有key和createdTime字段的表,表里存在很多不同的key值,每个key值下有很多记录. 我想要查出每个key下面cratedTime最大的记录,即每 ...

  4. mysql between and 是[a,b]闭区间

    mysql between and 是[a,b]闭区间 mysql between and 是[a,b]闭区间

  5. http 结构初始化

    简要而说:accept 到连接后 根据fd 构建一个connection  由于是 http : 重新封装为http-connection:同时设置fd的读回调: 回调函数根据是否是https/htt ...

  6. 适用初学者的5种Python数据输入技术

    摘要:数据是数据科学家的基础,因此了解许多加载数据进行分析的方法至关重要.在这里,我们将介绍五种Python数据输入技术,并提供代码示例供您参考. 数据是数据科学家的基础,因此了解许多加载数据进行分析 ...

  7. cetos6.5 gcc4.8 安装

    1.准备源 #安装仓库 wget http://people.centos.org/tru/devtools-2/devtools-2.repo mv devtools-2.repo /etc/yum ...

  8. MFC详解

    MFC的消息响应机制详解: 1.MFC是Windows下程序设计的最流行的一个类库,但是该类库比较庞杂,尤其是它的消息映射机制,更是涉及到很多低层的东西,接下来详细讲解. 2.在讲解MFC的消息响应之 ...

  9. CTDB使用radosobject作为lockfile

    前言 服务器的服务做HA有很多种方式,其中有一种就是是用CTDB,之前这个是独立的软件来做HA的,现在已经跟着SAMBA主线里面了,也就是跟着samba发行包一起发行 之前CTDB的模式是需要有一个共 ...

  10. appium-appium的等待时间

    #三种appium设置等待时间的方法 #第一种 sleep(): 设置固定休眠时间. python 的 time 包提供了休眠方法 sleep() , 导入 time包后就可以使用 sleep()进行 ...