Android 绑定Service并与之通信
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并与之通信的更多相关文章
- android绑定Service失败原因
今天抄一个代码,学习Service,中间Service的绑定一直是失败的. bindService返回false 上网查询的话都是一些,比如说TabHost的问题 发现和自己的问题不一样. 最后想了想 ...
- Android中AIDL的理解与使用(二)——跨应用绑定Service并通信
跨应用绑定Service并通信: 1.(StartServiceFromAnotherApp)AIDL文件中新增接口: void setData(String data); AppService文件中 ...
- android学习-IPC机制之ACtivity绑定Service通信
bindService获得Service的binder对象对服务进行操作 Binder通信过程类似于TCP/IP服务连接过程binder四大架构Server(服务器),Client(客户端),Serv ...
- Android菜鸟的成长笔记(18)——绑定本地Service并与之通信
在上一篇中介绍了Service与Activity的区别及Service两种启动方式中的第一种启动方式startService(). 我们会发现用startService().stopService() ...
- Android中AIDL的理解与使用(一)——跨应用启动/绑定Service
AIDL(Android Interface Definition Language)--安卓接口定义语言 一.startService/stopService 1.同一个应用程序启动Service: ...
- Android开发学习之路-Service和Activity的通信
在很多时候,Service都不仅仅需要在后台运行,还需要和Activity进行通信,或者接受Activity的指挥,如何来实现,来看代码. 定义一个服务 // 创建一个服务,然后在onBind()中返 ...
- Android Service与Activity之间通信的几种方式
在Android中,Activity主要负责前台页面的展示,Service主要负责需要长期运行的任务,所以在我们实际开发中,就会常常遇到Activity与Service之间的通信,我们一般在Activ ...
- 绑定本地Service并与之通信
绑定Service需要调用 public boolean bindService (Intent service, ServiceConnection conn, int flags): 传入一个Se ...
- (六)Android中Service通信
一.启动Service并传递参数 传递参数时只需在startService启动的Intent中传入数据便可,接收参数时可在onStartCommand函数中通过读取第一个参数Intent的内容来实现 ...
随机推荐
- 【codevs2495】水叮当的舞步
题目描述 Description 水叮当得到了一块五颜六色的格子形地毯作为生日礼物,更加特别的是,地毯上格子的颜色还能随着踩踏而改变.为了讨好她的偶像虹猫,水叮当决定在地毯上跳一支轻盈的舞来卖萌~~~ ...
- 分布式缓存产品Redis和memcached比较区别(图)
- /error处理
1 BasicErrorController 1.1 简述 SpringMVC框架在出现错误时有一个默认的错误请求 /error:出现异常之后执行/error请求之前框架会判断出现异常的请求类型,然后 ...
- FacadePattern(23种设计模式之一)
设计模式六大原则(1):单一职责原则 设计模式六大原则(2):里氏替换原则 设计模式六大原则(3):依赖倒置原则 设计模式六大原则(4):接口隔离原则 设计模式六大原则(5):迪米特法则 设计模式六大 ...
- Oracle——SQL基础
一.SQL语句分为以下三种类型: DML: Data Manipulation Language 数据操纵语言DDL: Data Definition Language 数据定义语言DCL: Data ...
- Struts2获取Action中的数据
当我们用Struts2框架开发时,经常有要获取jsp页面的数据或者在jsp中获取后台传过来的数据(Action),那么怎么去获取自己想要的数据呢? 后台获取前端数据: 在java程序中生成要获取字段的 ...
- (转)QueryBuilder : 打造优雅的Linq To SQL动态查询
原文地址:http://www.cnblogs.com/coolcode/archive/2009/09/28/IQueryBuilder.html 首先我们来看看日常比较典型的一种查询Form 这个 ...
- 解决Spring Boot(2.1.3.RELEASE)整合spring-data-elasticsearch3.1.5.RELEASE报NoNodeAvailableException[None of the configured nodes are available
Spring Boot(2.1.3.RELEASE)整合spring-data-elasticsearch3.1.5.RELEASE报NoNodeAvailableException[None of ...
- Java java.lang.Thread#join()方法分析
结论:A 线程调用 B 线程对象的 join 方法,则 A 线程会被阻塞,直到 B 线程 挂掉 (Java Doc 原话: Watis for this thread to die). 一.分析 查看 ...
- Linux 内核list_head 学习
Linux 内核list_head 学习(一) http://www.cnblogs.com/zhuyp1015/archive/2012/06/02/2532240.html 在Linux内核中,提 ...