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. codeforce 459DIV2 C题

    题意 一串括号字符串,里面存在一些‘?’,其中‘?’既可以当作 '(' 又可以当作 ')' ,计算有多少对(l,r),在s中[sl,s(l+1),s(l+2),.....sr],内的括号是匹配的.n= ...

  2. jdbc中Statement和PreparedStatement有什么区别?哪个性能更好?

    Statement和PreparedStatement的功能主要是对sql语句的执行 区别 (1)Statement每执行一条sql语句就需要生成一条执行计划,执行100条就需要100条执行计划Pre ...

  3. fiddler抓包时显示Tunnel to......443

    打开手机浏览器,输入http://192.168.0.65:8888/FiddlerRoot.cer

  4. Reading——The Non-Designer's Design Book

    看这本书的时候真的好恨没有CS7在手><,不然我百度几张图来模拟下,体验下设计的快感. 人们总是很容易注意到在他们潜意识里存在的东西,比如说这个图:    我们很容易联想到微信,但是3   ...

  5. LeftStr函数使用

    LeftStr(s, i); 表示返回字符串s的左边共I位字符的一个新字符串. var i: integer; s: string; result: string; begin i := ; s := ...

  6. Arduino ADC + 模拟温度传感器LM35D

    LM35是美国国家半导体(后被TI收购)推出的精密温度传感IC系列,其信号输出方式为模拟输出,输出电压值与摄氏温度值呈正比,且用户不需额外的校正就能获得较高的测量精度.其主要特性有: 供电电压:4~3 ...

  7. Task 回调

    前正无生意,且记录Task回调之用法. using System; using System.Collections.Generic; using System.Diagnostics; using ...

  8. arp欺骗进行流量截获-1

    这边博文主要讲一下怎么使用arp欺骗进行流量截获,主要用于已经攻入内网以后,进行流量监听以及修改. 一.什么是arp     arp协议是以太网的基础工作协议,其主要作用是是一种将IP地址转化成物理地 ...

  9. vs2015+opencv3.3.1 实现 c++ 双边滤波器(Bilateral Filter)

    #include <opencv2\highgui\highgui.hpp> #include <iostream> #include<vector> using ...

  10. Go环境搭建(Windows)

    下载MSI MSI地址 配置环境变量 GOPATH: 用于存放Go语言Package的目录,这个目录不能在Go的安装目录中 GOBIN: Go二进制文件存放目录,写成%GOROOT%\bin就好 PA ...