android90 bind方式启动服务service调用service里的方法
package com.itheima.banzheng; import com.itheima.banzheng.LeaderService.ZhouMi; import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View; public class MainActivity extends Activity { private Intent intent;
private MyServiceConn conn;
PublicBusiness pb; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent(this, LeaderService.class);
conn = new MyServiceConn();//服务连接对象,成功后就调用
//绑定领导服务
bindService(intent, conn, BIND_AUTO_CREATE);//执行这个代码后服务就成功的绑定了连接就建立了,连接后会调用LeaderService的onBind方法返回IBinder对象,
} public void click(View v){
//调用服务的办证方法
pb.QianXian();//这样就可以调用LeaderService的banZheng方法,否则是不能调用LeaderService的banZheng方法的
} class MyServiceConn implements ServiceConnection{//服务连接对象
//连接服务成功(成功拿到中间人IBinder的ZhouMi对象),此方法调用
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//service就是中间人ZhouMi
pb = (PublicBusiness) service;
} @Override
public void onServiceDisconnected(ComponentName name) {
}
}
}
Service:
package com.itheima.banzheng; import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder; public class LeaderService extends Service { @Override
public IBinder onBind(Intent intent) {
// 返回一个IBinder对象,这个对象就是MainActivity和LeaderService之间的中间人对象
return new ZhouMi();//返回给了onServiceConnected的service参数
} class ZhouMi extends Binder implements PublicBusiness{
public void QianXian(){
banZheng();
} public void daMaJiang(){
System.out.println("陪李处打麻将");
}
} public void banZheng(){
System.out.println("李处帮你来办证");
}
}
接口:
package com.itheima.banzheng;
public interface PublicBusiness {
void QianXian();
}
清单文件:
<service android:name="com.itheima.banzheng.LeaderService"></service>
音乐播放器:
package com.itheima.musicplayer; import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View; public class MainActivity extends Activity { MusicInterface mi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, MusicService.class);
//混合调用
//为了把服务所在进程变成服务进程
startService(intent);
//为了拿到中间人对象
bindService(intent, new MusicServiceConn(), BIND_AUTO_CREATE);
//* 服务的混合调用
//* 先开始、再绑定,先解绑、再停止
} class MusicServiceConn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mi = (MusicInterface) service;//返回中间人对象
}
@Override
public void onServiceDisconnected(ComponentName name) {
} } //开始播放按钮
public void play(View v){//Activity的方法
mi.play();//中间人的方法,调用Service的方法
}
//暂停播放按钮
public void pause(View v){
mi.pause();
}
}
//清单文件: <service android:name="com.itheima.musicplayer.MusicService"></service>
package com.itheima.musicplayer; import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder; public class MusicService extends Service{
@Override
public IBinder onBind(Intent intent) {
return new MusicController();//中间人对象
} //必须继承binder,才能作为中间人对象返回给onServiceConnected的service
class MusicController extends Binder implements MusicInterface{
public void play(){
MusicService.this.play();//中间人对象调用Service的方法
}
public void pause(){
MusicService.this.pause();
}
} public void play(){//Service的方法
System.out.println("播放音乐");
} public void pause(){
System.out.println("暂停播放");
} }
package com.itheima.musicplayer;
public interface MusicInterface {
void play();
void pause();
}
#找领导办证
* 把服务看成一个领导,服务中有一个banZheng方法,如何才能访问?
* 绑定服务时,会触发服务的onBind方法,此方法会返回一个Ibinder的对象给MainActivity,通过这个对象访问服务中的方法
* 绑定服务 Intent intent = new Intent(this, BanZhengService.class);
bindService(intent, conn, BIND_AUTO_CREATE);
* 绑定服务时要求传入一个ServiceConnection实现类的对象
* 定义这个实现类 class MyServiceconn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
zjr = (PublicBusiness) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
} }
* 创建实现类对象 conn = new MyServiceconn();
* 在服务中定义一个类实现Ibinder接口,以在onBind方法中返回 class ZhongJianRen extends Binder implements PublicBusiness{
public void QianXian(){
//访问服务中的banZheng方法
BanZheng();
}
public void daMaJiang(){ }
}
* 把QianXian方法抽取到接口PublicBusiness中定义 ---
#两种启动方法混合使用
* 用服务实现音乐播放时,因为音乐播放必须运行在服务进程中,可是音乐服务中的方法,需要被前台Activity所调用,所以需要混合启动音乐服务
* 先start,再bind,销毁时先unbind,在stop
android90 bind方式启动服务service调用service里的方法的更多相关文章
- 在JS中调用CS里的方法(PageMethods)
在JS中调用CS里的方法(PageMethods) 2014年04月28日 11:18:18 被动 阅读数:2998 最近一直在看别人写好的一个项目的源代码,感觉好多东西都是之前没有接触过的.今天 ...
- 【FLEX教程】#007 如何让JS调用SWF里的方法
HTML中,JS如何调用SWF里面已经封装好的代码呢? 有一些事情Flex没办法实现的,需要通过调用JS来实现. eg: 当浏览器窗体关闭的时候,弹出一个对话框,提示用户是否退出?或者是否保存当前的操 ...
- 解决未能启动服务“VMware Authorization Service”
计算机-管理-服务--服务列表找到VMware Authorization Service 并双击 打开服务.
- Java中多线程启动,为什么调用的是start方法,而不是run方法?
前言 大年初二,大家新年快乐,我又开始码字了.写这篇文章,源于在家和基友交流的时候,基友问到了,我猛然发现还真是这么回事,多线程启动调用的都是start,那么为什么没人掉用run呢?于是打开我的ide ...
- vue 进入页面每次都调用methods里的方法
// 监听路由,每次进入页面调用方法,放在method里 mounted(){ this.getPath() }, methods: { getPath(){ console.log(this.$ro ...
- 安卓服务(Service)的两种开启方式以及服务的生命周期
安卓中服务的开启方式 一:採用start的方式开启服务 调用函数:startService(Intent)->onCreate()->onStart()/onStartCommand()- ...
- Wildfly在Linux下以Service的方式启动 配置步骤
1.前提介绍 在目前项目中使用Wildfly9在linux下部署项目,经常会通过远程的SSH来启动关闭服务.但是通过SHH启动 standalone.sh 的服务,如果关闭窗口服务就会被停掉.所以就想 ...
- centos7也支持service命令启动服务吗,对于centos7 中的systemctl和旧的service命令的区别和联系
一.centos7也支持service命令启动服务吗 CentOS 7.0中一个最主要的改变,就是切换到了systemd.它用于替代红帽企业版Linux前任版本中的SysV和Upstart,对系统和服 ...
- Android开发之通过Intent启动其他App的Service
在Android5.0以前可以通过隐式Intent方式启动其他App的Service,就跟Activity启动隐式Intent一样的. 但是在5.0以后,只能使用显示的Intent方式启动了. 启动其 ...
随机推荐
- [codility]tree_height
http://codility.com/demo/take-sample-test/treeheight 非常非常简单的求树的深度.不忍直视. // you can also use includes ...
- Linux IP 路由实现
以下代码取自 kernel . [数据结构] 该结构被基于路由表的classifier使用,用于跟踪与一个标签(tag)相关联的路由流量的统计信息,该统计信息中包含字节数和报文数两类信息. 这个结构包 ...
- ubuntu操作系统下载
原文网址:http://www.cyberciti.biz/linux-news/download-ubuntu-14-4-cd-dvd-iso-images/ Download of the day ...
- java web的一些特殊用法(一)
1.查看jquery ajax请求的数据的具体格式 很多时候,我们需要查看到ajax返回时的具体格式才知道怎么去解析他.在最原始的ajax写法中,可以通过xmlhttp.responseText查看到 ...
- 在User Profile Service中配置AD的同步连接
转:http://www.360sps.com/Item/ConfigureSynchronizationConnections.aspx 如果要将Active Directory.LDAP 目录和业 ...
- 解决easyui和bootstrap兼容问题
在使用bootstrap和easyui的时候,发现很多有冲突的地方,包括datagrid控件和combo等,以下进行的问题修正,保证easyui正常显示 /*bootstrap兼容问题和easyui的 ...
- 浏览器兼容性的css hack 写法
IE各版本浏览器之间的识别概括如下: IE6:能识别 * .\9 和 _ ,不能识别 !important IE7:能识别 * .\9 和 !important,不能识别 _ IE8:能识别 \9 和 ...
- 集成电路中的assert和deassert应该如何翻译?
转载自:http://m.blog.csdn.net/blog/code_robot/37663085 我每次看到电路中的assert与deassert时,总是感觉别扭,因为词典翻译总是"断 ...
- poj2186 Popular Cows(强连通)
崇拜有传递性.求所有牛都崇拜的牛tarjan算法求强连通. 如果不连通就不存在.如果联通,缩点后唯一一个出度为零的点就是答案,有多个则不存在. #include <vector> #inc ...
- Linux内存寻址之分页机制
在上一篇文章Linux内存寻址之分段机制中,我们了解逻辑地址通过分段机制转换为线性地址的过程.下面,我们就来看看更加重要和复杂的分页机制. 分页机制在段机制之后进行,以完成线性—物理地址的转换过程.段 ...