ActivityManifect.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.hanqi.testservice"><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><serviceandroid:name=".TestService1"android:enabled="true"android:exported="true"></service></application></manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.hanqi.testservice.MainActivity"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><Buttonandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="一般启动服务"android:onClick="bt1_onclick"/><Buttonandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="一般停止服务"android:onClick="bt2_onclick"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><Buttonandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="绑定启动服务"android:onClick="bt3_onclick"/><Buttonandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="解绑停止服务"android:onClick="bt4_onclick"/></LinearLayout></LinearLayout>

TestService.java

package com.hanqi.testservice;import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;public class TestService1 extends Service {public TestService1() {Log.e("TAG","构造Service");}//绑定的回调方法//必须要重写
    @Overridepublic IBinder onBind(Intent intent) {// TODO: Return the communication channel to the service.//throw new UnsupportedOperationException("Not yet implemented");Log.e("TAG","绑定被调用");return new Binder();}@Overridepublic void onCreate() {Log.e("TAG","创建Service");super.onCreate();}@Overridepublic void onDestroy() {Log.e("TAG","销毁Service");super.onDestroy();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.e("TAG","启动命令");return super.onStartCommand(intent, flags, startId);}@Overridepublic boolean onUnbind(Intent intent) {Log.e("TAG","解除绑定");return super.onUnbind(intent);}
}

mainactivity.java

package com.hanqi.testservice;import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}//一般方式启动Service//参考Activity的启动方式public void bt1_onclick(View v){//通过意图Intent启动//显示意图Intent intent = new Intent(this,TestService1.class);startService(intent);Toast.makeText(MainActivity.this, "启动Service", Toast.LENGTH_SHORT).show();}//一般方式停止Service//参考Activity的启动方式public void bt2_onclick(View v){//通过意图Intent启动//显示意图Intent intent = new Intent(this,TestService1.class);stopService(intent);Toast.makeText(MainActivity.this, "停止Service", Toast.LENGTH_SHORT).show();}ServiceConnection serviceConnection;public void bt3_onclick(View v){//通过意图Intent启动//显示意图Intent intent = new Intent(this,TestService1.class);if(serviceConnection == null) {serviceConnection = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {Log.e("TAG", "连接服务");}@Overridepublic void onServiceDisconnected(ComponentName name) {Log.e("TAG", "断开链接服务");}};}//参数: 意图,连接,绑定方式   BIND_AUTO_CREATE(自动创建)
        bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE);Toast.makeText(MainActivity.this, "绑定服务成功", Toast.LENGTH_SHORT).show();}public void bt4_onclick(View v){//判断是否已经绑定,连接是否为空if(serviceConnection != null){//解除绑定
            unbindService(serviceConnection);serviceConnection = null;Toast.makeText(MainActivity.this, "解除绑定", Toast.LENGTH_SHORT).show();}else {Toast.makeText(MainActivity.this, "尚未绑定", Toast.LENGTH_SHORT).show();}}//  Acticity销毁的回调方法
    @Overrideprotected void onDestroy() {if(serviceConnection != null) {//解除绑定
            unbindService(serviceConnection);serviceConnection = null;}super.onDestroy();}
}

转载于:https://www.cnblogs.com/cuikang/p/5423694.html

andorid service 本地服务相关推荐

  1. 服务(service)-----本地服务

    服务的简介:Android四大基本组件之一,没有图形用户界面,总是在后台执行耗时操作. 服务的生命周期:onCreate onStartCommand onDestroy onBind 本地服务:on ...

  2. Windows 服务介绍(本地系统、网络服务、本地服务以及相关的power shell命令

    一.Windows服务概述 服务与进程 Windows服务是指系统自动完成的,不需要和用户交互的过程,可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何 ...

  3. Android Service学习之本地服务

    Service是在一段不定的时间运行在后台,不和用户交互应用组件.每个Service必须在manifest中 通过<service>来声明.可以通过contect.startservice ...

  4. android技术服务,Android Service学习之本地服务

    Service是在一段不定的时间运行在后台,不和用户交互应用组件.每个Service必须在manifest中 通过来声明.可以通过contect.startservice和contect.bindse ...

  5. 本地服务(local Service)的实现

    定义; --后台运行.不可见.没有界面 --优先级高于Activity 用途: --播放音乐.记录地理信息位置的改变.监听某种动作 注意: --运行在主线程,不能用来做耗时的请求活动 --可以在服务中 ...

  6. Service(服务)之 Local Service(本地服务)

    一.Service简介 Android中服务是运行在后台的东西,级别与activity相同(同属四大组件).既然说service是运行在后台的服务,那么它就是不可见的,没有界面的东西.你可以启动一个服 ...

  7. Service(一、本地服务)

    本文 借鉴了网上的文章 http://www.cnblogs.com/linlf03/p/3296323.html 看了很多东西,写了个 本地服务,然后做了下总结: 服务分为 :一.远程服务(不同程序 ...

  8. 【Service】bindService:绑定本地服务和远程服务示例

    绑定本地服务 AndroidManifest.xml中声明服务: <service android:name=".TestLocalService"><inten ...

  9. android学习笔记---40_建立能与访问者进行相互通信的本地服务,后台运行的service

    40_建立能与访问者进行相互通信的本地服务 ------------------------------------------- 1.本地服务:如果服务和访问者在一个进程中那么就叫做本地服务.   ...

最新文章

  1. Python 数据类型:列表
  2. 前端学习(2856):简单秒杀系统学习之定时器循环显示
  3. 解决方案:IndentationError: unindent does not match any outer indentation level
  4. 数据分析,如何构建指标体系
  5. 商业信息敏感、安全处理(口令、数字证书-U盾-密保卡、指纹识别-虹膜识别)...
  6. 百万人学AI 万人在线大会, 15+ 场直播抢先看!
  7. 使用向导进行MFC程序设计
  8. dateutil模块
  9. matlab基因频率是看最大值吗,基于ICA的语音信号盲分离
  10. vue 前端导出xlsx文件
  11. idea activation code记录
  12. SQL语句的连接-内连接 外连接
  13. 计算机网络和智能家居,智能家居与传统智能家居到底有什么区别
  14. Windows 2008 Server线程池前瞻
  15. 内网漫游(lateral movement)的破解之道
  16. 为什么博图中放置按下按钮无反应_为什么点击按钮毫无反应
  17. 抖音申请 TIKTOK 商标被驳回
  18. 标准化API设计的重要性
  19. 3.字符设备驱动高级
  20. 数据库中表的常见七大约束

热门文章

  1. 马化腾:5G和AI双核驱动产业互联网进入“快车道”
  2. 工业4.0进行时:未来协作方式的变革
  3. @程序员,什么才是“2020-1024”的正确打开姿势?
  4. Spring 基于Java配置
  5. 利用卷积神经网络(VGG19)实现火灾分类(附tensorflow代码及训练集)
  6. 【点播系列之一】关于阿里视频云点播解决方案,你想知道的都在这里!
  7. 数据科学家所需的大脑训练
  8. 关于手风琴效果延迟执行解决方式
  9. centos6.5用memcached 来作PHP 的session.save_handler
  10. Android Fragment 嵌套使用报错