1.绑定service

2.实现方法

3.在Androidmanifest.xml文件中配置service

<service android:name=".Myservice"></service>

4.java后台

MainActivity界面

package com.lucky.test38service2;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends AppCompatActivity {
Button button_setbind;
Button button_unsetbind;
Button button_getvalue;
Button button_clear;
Intent intent;
Myservice.Mybind mybind; //定义一个Mybind
ServiceConnection connection; //定义一个ServiceConnection @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_setbind=findViewById(R.id.button_setbind);
button_unsetbind=findViewById(R.id.button_unsetbind);
button_getvalue=findViewById(R.id.button_getvalue);
button_clear=findViewById(R.id.button_clear);
intent=new Intent(MainActivity.this,Myservice.class); //ServiceConnection 建立service连接
connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mybind= (Myservice.Mybind) service; //利用mybind作为MainActivity与Myservice信息交互的桥梁
} @Override
public void onServiceDisconnected(ComponentName name) { }
}; button_setbind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bindService(intent,connection,BIND_AUTO_CREATE); //绑定service
}
});
button_unsetbind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
unbindService(connection); //解除service
}
});
button_getvalue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//显示service内容
Toast.makeText(MainActivity.this,mybind.getCount()+"",Toast.LENGTH_SHORT).show();
}
});
button_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mybind.setCount(0); //设置count值
}
});
}
}

Myservice

package com.lucky.test38service2;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder; public class Myservice extends Service { boolean flag1;
int count; //新建绑定工具类,绑定count
public class Mybind extends Binder{
//获取count值
public int getCount(){
return count;
} //设置count值
public void setCount(int arg){
count=arg;
}
} Mybind mybind=new Mybind(); @Override
public IBinder onBind(Intent intent) {
return mybind;
} //service创建时调用的方法
@Override
public void onCreate() {
super.onCreate();
flag1=true;
new Thread(){
@Override
public void run() {
while (flag1){
try {
Thread.sleep(1000); //延时线程
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
}
}
}.start();
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
} @Override
public void onDestroy() {
super.onDestroy();
flag1=false;
} }

Android 绑定Service并与之通信的更多相关文章

  1. android绑定Service失败原因

    今天抄一个代码,学习Service,中间Service的绑定一直是失败的. bindService返回false 上网查询的话都是一些,比如说TabHost的问题 发现和自己的问题不一样. 最后想了想 ...

  2. Android中AIDL的理解与使用(二)——跨应用绑定Service并通信

    跨应用绑定Service并通信: 1.(StartServiceFromAnotherApp)AIDL文件中新增接口: void setData(String data); AppService文件中 ...

  3. android学习-IPC机制之ACtivity绑定Service通信

    bindService获得Service的binder对象对服务进行操作 Binder通信过程类似于TCP/IP服务连接过程binder四大架构Server(服务器),Client(客户端),Serv ...

  4. Android菜鸟的成长笔记(18)——绑定本地Service并与之通信

    在上一篇中介绍了Service与Activity的区别及Service两种启动方式中的第一种启动方式startService(). 我们会发现用startService().stopService() ...

  5. Android中AIDL的理解与使用(一)——跨应用启动/绑定Service

    AIDL(Android Interface Definition Language)--安卓接口定义语言 一.startService/stopService 1.同一个应用程序启动Service: ...

  6. Android开发学习之路-Service和Activity的通信

    在很多时候,Service都不仅仅需要在后台运行,还需要和Activity进行通信,或者接受Activity的指挥,如何来实现,来看代码. 定义一个服务 // 创建一个服务,然后在onBind()中返 ...

  7. Android Service与Activity之间通信的几种方式

    在Android中,Activity主要负责前台页面的展示,Service主要负责需要长期运行的任务,所以在我们实际开发中,就会常常遇到Activity与Service之间的通信,我们一般在Activ ...

  8. 绑定本地Service并与之通信

    绑定Service需要调用 public boolean bindService (Intent service, ServiceConnection conn, int flags): 传入一个Se ...

  9. (六)Android中Service通信

    一.启动Service并传递参数 传递参数时只需在startService启动的Intent中传入数据便可,接收参数时可在onStartCommand函数中通过读取第一个参数Intent的内容来实现 ...

随机推荐

  1. sql2012增加Sequence对象

    官方给出了一大堆SQL2012相对于SQL2008R2的新特性,但是大多数对于普通开发人员来说都是浮云,根本用不到,下面就说说一些对于开发人员来说比较有用的新特性. Sequence对象对于Oracl ...

  2. 用rand5()生成rand(n)

    问题:有函数rand5(),它能够等概率生成[0,5)之间的整数.由rand5()构造rand(n)使其能够等概率生成[0,n)之间的整数. 思路1:有rand5()先生成等概率生成0和1的rand0 ...

  3. 数据库SQL优化大总结之 百万级数据库优化方案 【转载】

    网上关于SQL优化的教程很多,但是比较杂乱.近日有空整理了一下,写出来跟大家分享一下,其中有错误和不足的地方,还请大家纠正补充. 这篇文章我花费了大量的时间查找资料.修改.排版,希望大家阅读之后,感觉 ...

  4. css一div内文字居中

    在说到这个问题的时候,也许有人会问CSS中不是有vertical-align属性来设置垂直居中的吗?即使是某些浏览器不支持我只需做少许的CSS Hack技术就可以啊!所以在这里我还要啰嗦两句,CSS中 ...

  5. Python 网络爬虫 010 (高级功能) 解析 robots.txt 文件

    解析 robots.txt 文件 使用的系统:Windows 10 64位 Python 语言版本:Python 2.7.10 V 使用的编程 Python 的集成开发环境:PyCharm 2016 ...

  6. c语言split的实现代码

    我们知道在其他语言中有split函数可以把一个字符串按你自己想要的分隔符分割成多个字符串并以列表的形式返回.但是对于c语言来说,是没有这样一个函数接口可以直接调用的.但是有时候在项目工作中,又会用到这 ...

  7. Python基础入门-While循环

    讲完了for循环我们继续来看第二个循环,那就是while循环,while循环和for循环虽然都是循环,但是有着本质的不同.我们先来看下她们之间的区别和联系: While循环和for循环区别: 1.fo ...

  8. (转)ASP.NET基础之HttpHandler学习

    原文地址:http://www.cnblogs.com/wujy/archive/2013/08/18/3266009.html 经过前两篇[ASP.NET基础之HttpModule学习]和[ASP. ...

  9. MVC上的jsonp扩展,解决跨域访问问题

    总是有人会遇到跨域问题,然后有个jsonp的解决方案,MVC中代码如下: public class JsonpResult : System.Web.Mvc.JsonResult { object d ...

  10. Service Fabric 用 Powershell 部署应用到本地

    前置说明 安装 Service Fabric SDK,会在本机 C:\Program Files\Microsoft SDKs\Service Fabric\Tools\PSModule\Servic ...