Android绑定方式开始服务&调用服务当中的方法
来源:互联网 发布:二叉树叶子节点数算法 编辑:程序博客网 时间:2024/05/21 15:07
绑定方式开启服务,调用服务当中的方法
1、调用过程图解
2、案例代码
package com.example.bindCreateService;import com.example.bindCreateService.ChunGeService.Mybinder;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 ChunGeService.Mybinder myybinder;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void start(View view){Intent intent = new Intent(this, ChunGeService.class);startService(intent);}public void stop(View view){Intent intent = new Intent(this, ChunGeService.class);stopService(intent);}public void bind(View view){Intent intent = new Intent(this, ChunGeService.class);//intent激活意图,设置绑定服务的对象//conn 设置代理,用来个服务建立联系,不能为空//在绑定服务的时候,如果服务不存在,将自动创建。bindService(intent, new myConn(), BIND_AUTO_CREATE);}private class myConn implements ServiceConnection{@Override//服务被绑定成功后调用的方法public void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stubSystem.out.println("春哥把代理人返回回来了....");System.out.println(service.toString()); myybinder = (Mybinder) service;}@Override//服务被断开后调用的方法public void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stub}}public void change(View view){myybinder.CallChangeSing("月亮之上");}}
package com.example.bindCreateService;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.widget.Toast;public class ChunGeService extends Service {@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubSystem.out.println("春哥服务被成功的绑定了。。。");Mybinder mybinder = new Mybinder();System.out.println(mybinder.toString());return mybinder; //返回一个自定义的代理}public class Mybinder extends Binder{public void CallChangeSing(String singName){changeSing(singName);}}@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();System.out.println("服务开始了,春哥开始唱歌了.....");}@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();System.out.println("服务销毁了,春哥停止唱歌了.....");}/** * 更改唱的歌曲 * @param singName */public void changeSing(String singName){Toast.makeText(getApplicationContext(), "开始唱"+singName+"...", 0).show();}@Overridepublic boolean onUnbind(Intent intent) {// TODO Auto-generated method stubreturn super.onUnbind(intent);}}
3、绑定服务:绑定服务开启服务,可以调用服务里面的方法。解除绑定服务后、或应用程序关闭,服务也将会停止
开启服务:不能够调用服务当中的方法,应用程序关闭,服务仍然会在后台运行。
--需求:需要能够调用服务中的方法,并能够长期在后台运行。
解决办法:先开启服务,然后使用绑定服务。这样当应用程序退出后,虽然绑定会解除,但是服务仍然会在后台运行。
0 0
- Android绑定方式开始服务&调用服务当中的方法
- Android--绑定服务调用服务的方法
- 绑定的方式调用服务的方法
- Android -- service的开启方式, start开启和绑定开启服务,调用服务的的方法, aidl调用远程服务
- android,aidl,绑定远程服务,调用远程服务的方法
- android之绑定服务调用服务的方法
- Android启动服务和绑定服务的方法调用
- 绑定服务&调用服务里面的方法
- 绑定服务:调用服务内部的方法
- andorid,绑定服务,调用服务的方法
- activity以绑定服务的方式开启服务并调用服务里面的方法
- 22_绑定的方式调用服务的方法
- android 服务的绑定调用
- 绑定服务并调用服务的方法,解除绑定
- 91_采用绑定服务的方式调用服务里面的方法
- Android中服务里面方法的调用,开启,绑定
- 绑定的方式调用服务的方法和采用接口方式抽取方法
- 绑定服务调用方法的回顾
- ie浏览器 spring mvc返回json数据弹出下载页面 解决方案
- 如何打log 检查 Android CTS failure
- MyEclipse部署文件被lock的问题
- vs2010下添加另一个工程的对话框
- PostgreSQL 里 下标从 1 开始
- Android绑定方式开始服务&调用服务当中的方法
- 测试与调试(Testing && Debugging)
- 《JavaScript高级程序设计》读书笔记(四)
- Arduino连接HMC5883L三轴电子罗盘传感器
- window.open打开的页面向父页面传值
- mac上idea的bug,new project 的时候一定要让idea自己去新建路径!!
- switf元组
- Android强制横屏或竖屏注意事项
- HTTP协议详解