public class MyService extends Service {
public MyService() {
} @Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
//throw new UnsupportedOperationException("Not yet implemented");
return new Binder(); //这里必须要返回一个Binder实例
} // 服务启动
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
} // 服务销毁
@Override
public void onDestroy() {
super.onDestroy();
} // 服务开始的时候 执行的方法
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.print("服务正在执行.....");
new Thread() {
@Override
public void run() {
super.run(); while (true) {
System.out.print("服务正在执行....");
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
return super.onStartCommand(intent, flags, startId);
}
}
public class MainActivity extends AppCompatActivity implements ServiceConnection {

    private TextView tv;

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main); // 创建视图
//setContentView(R.layout.my_layout); this.setContentView(R.layout.my_layout); // 启动服务
findViewById(R.id.btnStartServcie).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { System.out.print("服务开始执行......");
Intent i = new Intent(MainActivity.this, MyService.class);
startService(i);
}
});
// 停止服务
this.findViewById(R.id.btnStopServcie).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, MyService.class);
stopService(i);
}
}); //btnBindServcie 绑定service
this.findViewById(R.id.btnBindServcie).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, MyService.class);
// 这里的MainActivity必须要实现ServiceConnection 重写onServiceConnected 和 onServiceDisconnected 方法
bindService(i, MainActivity.this, Context.BIND_AUTO_CREATE);
}
}); //btnBindServcie 解除绑定service
this.findViewById(R.id.btnUnBindServcie).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, MyService.class);
unbindService(MainActivity.this);
}
}); System.out.println("onCreate");
} @Override
protected void onStart() {
super.onStart();
System.out.println("onStart");
} @Override
protected void onResume() {
super.onResume();
System.out.println("onResume");
} @Override
protected void onPause() {
super.onPause();
System.out.println("onPause");
} @Override
protected void onStop() {
super.onStop();
System.out.println("onStop");
} @Override
protected void onDestroy() {
super.onDestroy();
System.out.println("onDestroy");
} @Override
protected void onRestart() {
super.onRestart();
System.out.println("onRestart");
} // 服务被绑定成功后被执行
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.print("service connected");
} // 服务被杀掉后执行
@Override
public void onServiceDisconnected(ComponentName name) {
System.out.print("service DisConnected");
}
}

这块不好理解,这个只能在以后的项目里面好好理解了。

安卓 service的更多相关文章

  1. 安卓Service完全解析(上)

    版权声明:本文出自汪磊的博客,转载请务必注明出处. 关于安卓Service相信很多安卓开发者都听说过,作为安卓四大组件之一,即使不经常用也应该听说过,但并不是每一个人都掌握的特别详细,全面.那么今天我 ...

  2. 安卓Service完全解析(中)

    摘要: 版权声明:本文出自汪磊的博客,转载请务必注明出处. 在上一篇中我们学习了Android Service相关的许多基础但是重要的内容,基本涵盖大部分平日里的开发工作.今天我们继续学习一下稍微高级 ...

  3. 安卓四大组件的作用、安卓Service的作用

    Activity好像是应用程式的眼睛,提供与user互动之窗. BroadcastReceiver好像是耳朵,接收来自各方的Intent. Service是在后台运行的. 一个Service 是一段长 ...

  4. Android Service 文档

    应用场景: 1  用于将后台逻辑(Service中)和UI逻辑(Activity中)进行解耦,实现Service功能的复用,为其他程序提供功能. 2  后台功能,由于Activity在进入后台时(On ...

  5. delphi 10 seattle 安卓服务开发(一)

    从delphi 开始支持安卓的开发开始, 安卓service 开发一直都是delphier 绕不过去的坎, 以前也有开发service  的方法,但是都是手工处理启动文件,而且要修改很多东西,基本上成 ...

  6. Android服务Service

    安卓Service服务 一    Service简介 Service是运行在后台的,没有界面的,用来处理耗时比较长的.Service不是一个单独的进程,也不是一个单独的线程. Service有两种类型 ...

  7. 【原创】如何用Android Studio断点安卓自带Service或Bind类型的Service

    很久以来,我一直想找一种方法来断点调试安卓系统自身的Service,或者bind类型的Service,比如我想看WifiManager里面的getWifiApConfiguration函数是如何实现的 ...

  8. 安卓第十三天笔记-服务(Service)

    安卓第十三天笔记-服务(Service) Servcie服务 1.服务概念 服务 windows 服务没有界面,一直运行在后台, 运行在独立的一个进程里面 android 服务没有界面,一直运行在后台 ...

  9. 安卓四大组件之--service

    服务:长期后台运行的没有界面的activity,程序写法和activity类似. 安卓系统进程管理是按照一定规则的: 1.默认情况下,关闭掉一个应用程序,清空了这个应用程序的任务栈,应用程序的进程还会 ...

随机推荐

  1. O365(世纪互联)SharePoint 之文档库使用小记

    前言 当O365越来越流行的时候,大家往往更多使用的是传统的Office功能,有太少订阅用户能触及到O365的一个非常棒的功能,叫做SharePoint online. 下面,我们就以图文并茂的方式, ...

  2. 在VM虚拟机上安装Microsoft Dynamics CRM 2016 步骤图解及安装注意事项

    安装Dynamics CRM 2016环境配置要求: 系统版本:Windows Server 2012 R2(必须) SQL 版本: SQLServer2014SP1-FullSlipstream-x ...

  3. 视图xsl定制之嵌入服务器控件

    SharePoint 2010 视图 xsl 文件中支持嵌入服务器控件,嵌入服务器控件时,系统先采用xsl将视图xml解析成一个类似UserControl的存在,然后执行UserControl. 代码 ...

  4. iOS之百度导航SDK的坐标转换

    百度导航 iOS SDK的坐标转换代码示例,有需要的朋友可以参考下. //导航坐标--------------> 地图坐标 //假设从导航sdk取到了一个点坐标是(116.304847, 40. ...

  5. 【原】iOS触摸事件深度解析

    概述 本文主要解析从我们的手指触摸苹果设备到最终响应事件的整个处理机制.本质上讲,整个过程可以分为两个步骤: 步骤1:找目标.在iOS视图层次结构中找到触摸事件的最终接受者: 步骤2:事件响应.基于i ...

  6. #研发解决方案介绍#IdCenter(内部统一认证系统)

    郑昀 基于朱传志的设计文档 最后更新于2014/11/13 关键词:LDAP.认证.权限分配.IdCenter. 本文档适用人员:研发   曾经一个IT内部系统配一套帐号体系和授权   线上生产环境里 ...

  7. 浅析SQL SERVER执行计划中的各类怪相

    在查看执行计划或调优过程中,执行计划里面有些现象总会让人有些疑惑不解: 1:为什么同一条SQL语句有时候会走索引查找,有时候SQL脚本又不走索引查找,反而走全表扫描? 2:同一条SQL语句,查询条件的 ...

  8. js获取URL地址栏中的参数

    function getUrlParam(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)( ...

  9. jQuery1.6源码分析系列

    原文地址:http://www.cnblogs.com/nuysoft/archive/2011/11/14/2248023.html jQuery源码分析(版本1.6.1) 目录 00 前言开光 0 ...

  10. RedHat6.2搭建FTP服务器

    我的环境: A:Red Hat Enterprise 6.2 IP:192.168.16.12 此机作测试端 B:Red Hat Enterprise 6.2 IP:192.168.16.13 此机做 ...