Android Service 启动和停止服务
activity_main.xml
定义两个Button控件,start_service和stop_service。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context=".MainActivity"> <Button
android:id="@+id/start_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start Service"/> <Button
android:id="@+id/stop_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Stop Service"/>
</LinearLayout>
MainActivity.java
同样定义两个Button控件start_service和stop_service,并设置监听器,然后用Intent对象进行通信。
package jiangbin.servicetest; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class MainActivity extends Activity implements View.OnClickListener{ private Button startService;
private Button stopService; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService = (Button) findViewById(R.id.start_service);
stopService = (Button) findViewById(R.id.stop_service);
startService.setOnClickListener(this);
stopService.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_service:
Intent startIntent = new Intent(this, MyService.class);
startService(startIntent);
break;
case R.id.stop_service:
Intent stopIntent = new Intent(this, MyService.class);
stopService(stopIntent);
default:
break;
}
}
}
MyService.java
定义Myservice类
onCreate()在服务创建时候调用;
onStartCommand()在服务创建的时候调用;
onDestory()在服务销毁的时候调用。
package jiangbin.servicetest; import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log; public class MyService extends Service {
@Override
public IBinder onBind(Intent intent){
return null;
} @Override
public void onCreate() {
super.onCreate();
Log.d("Myservice", "onCreate");
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("Myservice", "onStart");
return super.onStartCommand(intent, flags, startId); } @Override
public void onDestroy() {
super.onDestroy();
Log.d("Myservice", "onDestory");
}
}



Android Service 启动和停止服务的更多相关文章
- 转Android service 启动篇之 startForegroundService
本文转自:https://blog.csdn.net/shift_wwx/article/details/82496447 前言: 在官方文档 Android 8.0 行为变更 中有这样一段话: An ...
- CentOS启动和停止服务详解
服务简介Linux 系统服务是在Linux启 动时自动加载,并在Linux退出时自动停止的系统任务.在Linux 启动过程中,我们可以看得很多“starting … ”提示信息,该信息表示正在启动系统 ...
- Android Service学习之本地服务
Service是在一段不定的时间运行在后台,不和用户交互应用组件.每个Service必须在manifest中 通过来声明.可以通过contect.startservice和contect.bindse ...
- Android Service AIDL 远程调用服务 【简单音乐播放实例】
Android Service是分为两种: 本地服务(Local Service): 同一个apk内被调用 远程服务(Remote Service):被另一个apk调用 远程服务需要借助AIDL来完成 ...
- Android - Service启动机制
以下资料摘录整理自老罗的Android之旅博客,是对老罗的博客关于Android底层原理的一个抽象的知识概括总结(如有错误欢迎指出)(侵删):http://blog.csdn.net/luoshe ...
- CentOS7 从查看、启动、停止服务说起systemctl
执行命令“systemctl status 服务名.service”可查看服务的运行状态,其中服务名后的.service 可以省略,这是CenOS7以后采用systemd作为初始化进程后产生的变化. ...
- MySQL5.7使用Notifier启动、停止服务时出现的问题
1.选择右击右下角 MySQL Notifier ,选择 Actions -> Manage Monitored Items 2.选择当前的服务 MySQL57 并进行删除 3.然后点击 a ...
- android开机启动应用和服务
注冊广播监听开机状态.启动应用和服务等: 监听开机的广播接收器: public class BootCompletedReceiver extends BroadcastReceiver{ @Over ...
- Tomcat源码分析——启动与停止服务
前言 熟悉Tomcat的工程师们,肯定都知道Tomcat是如何启动与停止的.对于startup.sh.startup.bat.shutdown.sh.shutdown.bat等脚本或者批处理命令,大家 ...
随机推荐
- Jenkins 八: 构建Git项目
1. 安装git. http://git-scm.com/download/win 下载之后一步步安装即可. 2. 安装插件. 打开"系统管理" –> "管理插 ...
- angularJS constant和value
angularJS可以通过constant(name,value)和value(name,value)对于创建服务也是很重要的. 相同点是:都可以接受两个参数,name和value. 区别: 1.co ...
- 加快maven中jar包的下载速度
maven下载jar包的默认仓库是http://my.repository.com/repo/path速度较慢,通过配置国内镜像提高下载速度 1.打开eclipse--->Window---&g ...
- ECLIPSE TOMCAT CONFIG JSTL
{LJ?Dragon}[标题]Eclipse 配置 JSTL标签库 {LJ?Dragon}[Diary]2017年,愉快的开始:当和他们离别时,感觉失去了整个世界......... 1.JSTL简 ...
- centos下的mysql安装
卸载mysql yum remove mysql mysql-server mysql-libs compat-mysql51 rm -rf /var/lib/mysql rm /etc/my.cnf ...
- Linux网络编程echo多线程服务器
echo_server服务器多线程版本 #include <unistd.h> #include <stdlib.h> #include <stdio.h> #in ...
- adb logcat命令查看并过滤android输出log
cmd命令行中使用adb logcat命令查看android系统和应用的log,dos窗口按ctrl+c中断输出log记录. logcat日志中的优先级/tag标记: android输出的每一条日志都 ...
- maven建module子模块
在父工程中,点击new ->other ->maven -> maven module, 按照提示直到完成. module 可以是普通的工程也可以是web工程. 遇到的问题: 新 ...
- Reso | liunx下longeneQQ和搜狗拼音
sogoupinyin_2.0.0.0078_amd64.deb: http://pan.baidu.com/s/1eSDLvEU WineQQ7.8-20151109-Longene .deb: ...
- Git 版本控制工具使用介绍------Windows系统下使用
Git 是用于 Linux内核开发的版本控制工具.与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不必服务器端软件支持(wingeddevil注:这得分是用什 ...