ActivityManifect.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hanqi.testservice"> <application
android: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> <service
android: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"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="一般启动服务"
android:onClick="bt1_onclick"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="一般停止服务"
android:onClick="bt2_onclick"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="绑定启动服务"
android:onClick="bt3_onclick"/>
<Button
android: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");
}
//绑定的回调方法
//必须要重写
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
//throw new UnsupportedOperationException("Not yet implemented");
Log.e("TAG","绑定被调用");
return new Binder();
}
@Override
public void onCreate() {
Log.e("TAG","创建Service");
super.onCreate();
} @Override
public void onDestroy() {
Log.e("TAG","销毁Service");
super.onDestroy();
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("TAG","启动命令");
return super.onStartCommand(intent, flags, startId);
} @Override
public 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 { @Override
protected 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() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.e("TAG", "连接服务"); } @Override
public 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销毁的回调方法
@Override
protected void onDestroy() { if(serviceConnection != null) {
//解除绑定
unbindService(serviceConnection); serviceConnection = null;
}
super.onDestroy();
}
}

andorid service 本地服务的更多相关文章

  1. Android Service学习之本地服务

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

  2. Anroid四大组件service之本地服务

    服务是Android四大组件之一,与Activity一样,代表可执行程序.但Service不像Activity有可操作的用户界面,它是一直在后台运行.用通俗易懂点的话来说: 如果某个应用要在运行时向用 ...

  3. Android-bindService本地服务-初步-Service返回对象

    在Android开发过程中,Android API 已经有了startService方式,为什么还需要bindService呢? 答:是因为bindService可以实现Activity-->S ...

  4. android service 本地 远程 总结

    android编写Service入门 android SDK提供了Service,用于类似*nix守护进程或者windows的服务. Service有两种类型: 本地服务(Local Service) ...

  5. windows的服务中的登录身份本地系统账户、本地服务账户和网络服务账户修改

    以一个redis服务为例: 一个redis注册服务后一般是网络服务账户,但是当系统不存在网络服务账户时,就会导致redis服务无法正常启动.接下来修改redis服务的登录身份. cmd下输入如下命令: ...

  6. Android-bindService本地服务-音乐播放(后台播放)-下

    在上一篇  Android-bindService本地服务-音乐播放-上,博客中是不能在后台中播放到,这次博客增加了一个后台播放 通常情况下,Activity切换到后台,Service提升到前台进程, ...

  7. Android-bindService本地服务-初步

    在Android开发过程中,Android API 已经有了startService方式,为什么还需要bindService呢? 答:是因为bindService可以实现Activity-->S ...

  8. i.mx6 Android5.1.1 servicemanager本地服务

    接在之前的 i.mx6 Android5.1.1 初始化流程之init进程 i.mx6 Android5.1.1 初始化流程之init.rc解析 servicemanager是由init创建的本地服务 ...

  9. Service Mesh服务网格新生代--Istio(转)

    万字解读:Service Mesh服务网格新生代--Istio  官网地址:https://preliminary.istio.io/zh/docs/concepts/security/ Servic ...

随机推荐

  1. RocketMQ安装与部署说明

    一.安装说明1.下载安装包,下载地址:https://github.com/alibaba/RocketMQ/releases/download/v3.1.7/alibaba-rocketmq-3.1 ...

  2. 四、Spring——Spring MVC

    Spring MVC 1.Spring MVC概述 Spring MVC框架围绕DispatcherServlet这个核心展开,DispatcherServlet负责截获请求并将其分配给响应的处理器处 ...

  3. oracle远程连接太慢

    场景:Oracle11G DB服务器上用sqlplus直接连很快(秒连),但加上服务名后却半天没反应.一台正式.一台测试都出现此问题. sqlplus:xnwz/xnwz 很快 sqlplus:xnw ...

  4. [转载]强烈推荐学习的blog

    膜拜大牛 原文出处:http://hedengcheng.com/?p=676 ACM Queue (Architecting Tomorrow’s Computing) 网址:http://queu ...

  5. IOS框架研究之SDWebImage的原理以及使用流程

    SDWebImage 这个类库提供一个UIImageView类别以支持加载来自网络的远程图片.具有缓存管理.异步下载.同一个URL下载次数控制和优化等特征. SDWebImage 支持异步的图片下载+ ...

  6. Swing列表框组件

    public class JListTest extends JFrame{    private static final long serialVersionUID=1L;        publ ...

  7. myeclipse搭建SSH框架

    搭建SSH框架 Struts+hibernater+spring架构(myeclipse) 右击,首先加入spring,加入hibernater,再加入struts2 复制jar包(把tomcat发布 ...

  8. [转载]:fortran之format格式化输出总结

    先贴一段别人总结好的: 格式化输出的控制字符非常的丰富,但常用的并不多,一般说来:" I .F.E.A.X "是最常使用的几个格式,最 好把它们都记下来. Iw[.m] 以w个字符 ...

  9. JSON之Asp.net MVC C#对象转JSON,DataTable转JSON,List转JSON,JSON转List,JSON转C#对象

    一.JSON解析与字符串化 JSON.stringify() 序列化对象.数组或原始值 语法:JSON.stringify(o,filter,indent) o,要转换成JSON的对象.数组或原始值 ...

  10. Linux:去除认证,加速 SSH登录

    编辑配置文件 /etc/ssh/sshd_config vim /etc/ssh/sshd_config 找到 UseDNS选项,如果没有注释,将其注释 #UseDNS yes 添加 UseDNS n ...