activity与service进程内通信
package com.example.binbin.testbinder; import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast; /**
* Created by binbin on 2016/7/29.
*/
public class MyService extends Service { //自定义的binder,包含了我们所需的操作
class myBinder extends Binder{ //要在Service进行的操作
public void sayHello(){ Log.d("TAG","sayHello");
Toast.makeText(MyService.this,"Hello",Toast.LENGTH_SHORT).show(); } public void sayBye(){ Log.d("TAG","sayBye");
Toast.makeText(MyService.this,"Bye",Toast.LENGTH_SHORT).show(); } }; @Nullable
@Override
//绑定服务和进行通信的时候,一定要返回一个自定义的Binder
public IBinder onBind(Intent intent) {
Log.d("TAG","onBind");
return new myBinder();
} @Override
public void onCreate() {
super.onCreate();
Log.d("TAG","onCreate");
} @Override
public int onStartCommand(Intent intent, int flags, int startId) { Log.d("TAG","onStartCommand");
return super.onStartCommand(intent, flags, startId); } @Override
public void onDestroy() {
super.onDestroy();
Log.d("TAG","onDestroy"); } @Override
public boolean onUnbind(Intent intent) { Log.d("TAG","onUnbind");
return super.onUnbind(intent);
}
}
自定义了myBinder,在Activity与Service绑定的时候,返回myBinder的实例,Activity就可以操作Service里面的方法了(sayHello和sayBye)。
Activity里面必须要有两个实例,一个是myBinder,用于与Service通信的。另一个是ServiceConnection,代表了activity与服务的连接,在后面绑定的时候要传入这个类的实例,只有两个方法,我们要重写。第一个是连接成功时要干什么,肯定是要把binder返回给activity里面的binder啦。
public class MainActivity extends AppCompatActivity { //四个按钮
Button start, stop, bind, unbind,sayHello,sayBye; //自定义的Binder对象
private MyService.myBinder binder; //绑定服务要添加的对象,ServiceConnection代表与服务的连接,系统自己会调用,我们只需要实现,不需要显示调用
//绑定的时候要把这个对象传进去。
private ServiceConnection conn = new ServiceConnection() { //连接成功
@Override
public void onServiceConnected(ComponentName name, IBinder service) { Log.d("TAG","Connect!");
binder = (MyService.myBinder) service; //获取其实例
//binder.sayHello(); //调用其方法
} //连接失败
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); start = (Button) findViewById(R.id.Start);
stop = (Button) findViewById(R.id.Stop);
bind = (Button) findViewById(R.id.Bind);
unbind = (Button) findViewById(R.id.unBind);
sayHello = (Button) findViewById(R.id.hello);
sayBye = (Button) findViewById(R.id.bye); start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent); }
}); stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { Intent intent = new Intent(MainActivity.this, MyService.class);
stopService(intent); }
}); bind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { Intent intent = new Intent(MainActivity.this,MyService.class);
//传进去了conn对象
bindService(intent,conn,BIND_AUTO_CREATE); }
}); unbind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { //解除只需要传入连接对象
unbindService(conn); }
}); sayHello.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { binder.sayHello(); }
}); sayBye.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { binder.sayBye(); }
}); // ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
开启和绑定服务之后,按Hello,就可以使用服务里面的sayHello方法了。
activity与service进程内通信的更多相关文章
- MXNet源码分析 | KVStore进程内通信
本文主要基于MXNet1.6.0版本进行分析. MXNet的KVStore模块下有几个比较重要的类.KVStore是一个抽象类,提供了一些通用的API,例如Init.Push和Pull等.因为KVSo ...
- activity 与 service 之间的通信
activity和service通信:通过binder 举个我实际项目中的例子:在service中下载更新应用 首先是下载更新apk的service: public class UpdateVersi ...
- 通过messenger实现activity与service的相互通信
布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to ...
- 201709012工作日记--activity与service的通信机制
service生命周期 Service主要包含本地类和远程类. Service不是Thread,Service 是android的一种机制,当它运行的时候如果是Local Service,那么对应的 ...
- Android中Activity、Service和线程之间的通信
Activity.Service和线程应该是Android编程中最常见的几种类了,几乎大多数应用程序都会涉及到这几个类的编程,自然而然的,也就会涉及到三者之间的相互通信,本文就试图简单地介绍一下这三者 ...
- activity与service之间的通信方式
Activity之间的通信 1.activity与activity的通信可以通过Intent来封装数据,startActivityForResult()来实现,当跳转的activity调用finish ...
- Activity与Service通信(不同进程之间)
使用Messenger 上面的方法只能在同一个进程里才能用,如果要与另外一个进程的Service进行通信,则可以用Messenger. 其实实现IPC(Inter-Process Communicat ...
- Activity和Service是否是在同一个进程中运行。
一般情况下,Activity和Service在同一个包名内,并且没有设定属性android:process=":remote",两者在同一个进程中. 因为一个进程只有一个UI线程, ...
- Activity与Service通信的方式有三种:
在博客园看到的,看着挺不错的,借来分享下 继承Binder类 这个方式仅仅有当你的Acitivity和Service处于同一个Application和进程时,才干够用,比方你后台有一个播放背景音乐的S ...
随机推荐
- 【剑指offer】判断一个序列是否是二叉搜索树的后序遍历,C++实现
原创文章,转载请注明出处! 本题牛客网地址 博客文章索引地址 博客文章中代码的github地址 1.题目 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出N ...
- 【数据处理】OneHotEncoder编码
原创博文,转载请注明出处! # OneHotEncoder编码 OneHotEncoder编码称为"哑编码"或"独热编码",是将表示分类的数据扩维度, ...
- 【ci框架学习】控制器-视图
基础的东西就不写了,网上大把的教程,可以慢慢磨.这里说些要注意的要点. 1.所有控制器都必须间接或者直接继承控制器基类 CI_Controller. 2.文件命名尽量小写,因为ci使用的是pathin ...
- WebForm、MVC图片加载失败处理
还是那个该死的WebFrom项目,部分功能替换为MVC后感觉好多了,但是WebForm.MVC都有图片加载失败时显示提示图片的需求,并且统一在js中处理.问题来了,js中图片路径怎么处理呢?现场有可能 ...
- Cloudify介绍
一篇关于Cloudify的介绍,可以看一下:http://timeson.iteye.com/blog/1699730
- div横排放置对齐问题;block,inline,inline-block区别
1.左右两个div都设置为float:left,如果右边div没有设置宽度,右边div的宽度会根据div里的内容自动调整. 缺点:不易控制 2.只有左侧div设置为float:left,右侧div设置 ...
- BZOJ2199 奶牛议会 【2-sat】
BZOJ2199 奶牛议会 Description 由于对Farmer John的领导感到极其不悦,奶牛们退出了农场,组建了奶牛议会.议会以"每头牛 都可以获得自己想要的"为原则, ...
- Linux下Apache服务器配置
Linux下Apache服务器配置 相关包: httpd-2.2.3-29.e15.i386.rpm //主程序包 httpd-devel-2.2.3-29.e15.i ...
- verilog中的有符号数理解(转)
verilog中的有符号数运算 有符号数的计算:若有需要关于有号数的计算,应当利用Verilog 2001所提供的signed及$signed()机制. Ex: input signed [7:0] ...
- UVA11137 Ingenuous Cubrency
题意 PDF 分析 考虑dp. 用\(d(i,j)\)表示用不超过i的立方凑成j的方案数. \(d(i,j)=d(i-1,j)+d(i,j-i^3)\) 时间复杂度\(O(IN+T)\) 代码 #in ...