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. EF中的批量操作

    阅读目录 插入 更新 删除 在使用EF的过程中,我们经常会遇到需要批量操作数据的场景,批量操作有的时候不仅能提高性能,比如使用SqlBulkCopy进入批量插入的时候,而且比较方便操作,提高效率.那么 ...

  2. Content 控件

    转自:http://www.cnblogs.com/superfang/archive/2008/06/29/1232158.html 创建一个服务器控件,该控件包含呈现到母版页中的 ContentP ...

  3. 2-chrome无法添加扩展程序

    1.更多工具->拓展程序->打开开发者模式->重启浏览器 2.将拓展程序拖入,确认安装

  4. The 'Microsoft Jet OLEDB 4.0 Provider' is not registered on the local machine

    在一台Win7 64位的操纵系统上部署的C# Web系统,操作Excel,批量导入数据,报错,提示错误信息: The ‘Microsoft Jet OLEDB 4.0 Provider' is not ...

  5. c语言学习笔记 if语句执行流程和关系运算符

    回想现实生活中,我们会遇到这样的情况,如果下雨了就带伞上班,如果没下雨就不带伞上班,这是很正常的逻辑.程序是解决生活中的问题的,那么自然在程序中也需要这样的判断,当满足某个条件的时候做一件事情,这种东 ...

  6. bootstrap实现嵌套列

    <!DOCTYPE html> <html> <head> <title>Bootstrap 实例 - 嵌套列</title> <li ...

  7. Gson 配置解析

    之前项目用到了gson对json和Java类之间互转,现在将gson的配置总结一下. 首先,创建gson对象之间,建立gsonbuilder对象,并配置 // 不导出实体类中没有用@Expose注解的 ...

  8. VUE实战项目-数据转换之道

    前言 公司的这个项目从去年底启动.至今经历winform版本与当前的VUE两个版本,前后经历不足3个月的时间.从纯技术角度来看,推进速度都很优异.究其原因,大抵我们都是喜欢“偷懒”的程序员,把能封装. ...

  9. (函数分治法)实现pow函数(x的y次方幂)

    题目:实现pow函数. 题目分析:因为一个一个乘,循环太大,参考矩阵连乘问题:对于n=4的话,可以得出x的平方,然后平方与平方相乘.节省计算次数.对于偶数的幂,只要x的平方多次递归调用即可:对于奇数的 ...

  10. 2014-4-2解决无法访问github和google的问题

    github是个好地方,但是上不去就蛋疼了. 今天github上不去,果断f12下,看下network,发现里面好多请求都是指向 github.global.ssl.fastly.net这个域名的,然 ...