02_启动和销毁Service

在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();
    }

}
时间: 2024-08-26 18:09:00

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

自我分析-Spring IOC在Web应用的启动和销毁

Spring IOC容器通过ServletContextListener对servlet容器的生命周期监听,从而实现了IOC的启动和销毁. 注意: 1.分析框架代码时,要常使用类继承.调用关系等快捷键,可以更高效的学习,快捷键可以设置成你习惯的按键: 2.本文重在怎么自我分析框架代码,所以对其中解析需自己实际跟踪代码实践方可: 3.spring源代码版本 spring-framework-3.2.1.RELEASE. 预览 javax.servlet.ServletContext,Servlet

windows无法启动VMware Authorization Service服务

VMware也用了一年了,一直好好的,今天突然出现启动不了VMware里的linux系统了, Authorization  Service问题,在windows系统里查看服务,找到VMware Authorization Service,点启动,弹出无法启动VMware Authorization  Service  1075服务不存在. 上网找到答案:这是装了VMware虚拟机软件之后,又用了系统优化工作之后造成的 万恶的金山毒霸,果断卸载了 windows无法启动VMware Authori

Spring容器启动后注入service到Servlet并自动执行

通常做法是定义一个Servlet,并在web.xml中配置Servlet的启动顺序<load-on-startup>的值在DispatcherServlet之后.但这样做的缺点是在Servlet中无法使用Spring的依赖注入功能,只能使用WebApplicationContext的getBean()方法获取bean. 找到的解决办法如下: 1.自定义一个用于代理启动Servlet的类DelegatingServletProxy: package cn.edu.swu.oa.common.ut

intent 启动activity、service的方法

1.通过intent启动service. 通过传递一个Intent对象至Context.startService()将启动一个服务(或给予正在运行的服务以一个新的指令).Android调用服务的onStart()方法并将Intent对象传递给它. Intent stpServiceIn = new Intent(context, StpService.class); context.startService(stpServiceIn);

Linux禁止开机启动防火墙firewall.service

每次重启测试环境会发现外网都无法访问80端口,用systemctl status firewalld.service检查防火墙,是开启的状态 要使firewall不开机启动,使用命令systemctl disable firewalld.service 重启虚机后,再次检查firewall的状态 已经没有启动了. 原文地址:http://blog.51cto.com/12482328/2087882

adb启动activity、service、发送broadcast

一.adb启动activity: $ adb shell$ am start -n {包(package)名}/{包名}.{活动(activity)名称} 如:启动浏览器 # am start -n com.android.browser/com.android.browser.BrowserActivity 二.adb启动service: $ adb shell$ am startservice -n {包(package)名}/{包名}.{服务(service)名称} 如:启动自己应用中一个

问题汇总记录----mysql不能启动,提示service could not be started

文章目录 mysql安装配置 错误1 service could not be started  失败原因+解决方法(端口被占用) 错误2 ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061) +解决方法(在计算机管理中启动mysql8) 一 安装 配置 下载zip文件后,配置mysql 1 cmd管理员打开,输入 mysqld --initialize --console 生成临时密码: 2 mysql

centOS 7下无法启动网络(service network start)错误解决办法(应该是最全的了。。。)

今天在centOS 7下更改完静态ip后发现network服务重启不了,翻遍了网络,尝试了各种方法,终于解决了. 现把各种解决方法归纳整理,希望能让后面的同学少走点歪路... 首先看问题:执行service network restart命令后出现下面的错误: 1 Restarting network (via systemctl): Job for network.service failed because the control process exited with error code

安卓 service 后台运行,activity 启动和停止service

安卓activity界面,上面有两个按钮,一个是开始服务,一个是取消服务. 界面布局: 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layo