Activity和Service绑定
Activity和Service绑定后,可以方便Activity随时调用对应的Service里面的方法
绑定代码如下
Activity类代码:
- <span style="font-size:16px;">package com.fox.Activity;
- import com.fox.Activity.service.Service1;
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Toast;
- public class Activity1 extends Activity {
- private Button btn1 = null;
- private static String LOG="mp3";
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- btn1 = (Button) findViewById(R.id.button1);
- btn1.setOnClickListener(new btn1ClickListener());
- //开始绑定
- Intent intent = new Intent(Activity1.this,Service1.class);
- bindService(intent, conn, Context.BIND_AUTO_CREATE);
- }
- private Service1 myservice = null;//绑定的service对象
- //连接对象,重写OnserviceDisconnected和OnserviceConnected方法
- public ServiceConnection conn= new ServiceConnection() {
- @Override
- public void onServiceDisconnected(ComponentName name) {
- Log.i(LOG, "onServiceDisconnected>>>>>>>>");
- myservice = null;
- }
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- Log.i(LOG, "onServiceConnected>>>>>>>>");
- myservice = ((Service1.MyBinder)service).getService();
- Log.i(LOG, myservice+">>>>>>>>");
- }
- };
- class btn1ClickListener implements View.OnClickListener {
- @Override
- public void onClick(View v) {
- unbindService(conn);
- }
- }
- }</span>
Service类代码:
- <span style="font-size:16px;">package com.fox.Activity.service;
- import android.app.Service;
- import android.content.Intent;
- import android.os.Binder;
- import android.os.IBinder;
- import android.util.Log;
- public class Service1 extends Service{
- private final IBinder binder = new MyBinder();
- private static final String LOG="mp3";
- @Override
- public IBinder onBind(Intent intent) {
- Log.i(LOG, "onBind............");
- return binder;
- }
- /**
- * 该类是获得Service对象
- * @author Administrator
- *
- */
- public class MyBinder extends Binder{
- public Service1 getService(){
- return Service1.this;
- }
- }
- @Override
- public void onCreate() {
- Log.i(LOG, "oncreate............");
- super.onCreate();
- }
- @Override
- public void onStart(Intent intent, int startId) {
- Log.i(LOG, "onstart............");
- super.onStart(intent, startId);
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- Log.i(LOG, "onstartcommand............");
- return super.onStartCommand(intent, flags, startId);
- }
- @Override
- public void onDestroy() {
- Log.i(LOG, "ondestory............");
- super.onDestroy();
- }
- }
- </span>
开始绑定调用方法A.bindService()--->S.onCreate--->S.onBind---->>A.onServiceConnected绑定成功,并获得Service对象
结束绑定按钮的监听事件-->>unbindService(conn)关闭连接对象-->>S.destory()销毁该service
注:结束绑定时是不会调用onServiceDisconnected()方法的;
http://blog.csdn.net/huqingwei0824/article/details/6869622
Activity和Service绑定的更多相关文章
- 8.1.2 绑定Activity和Service
8.1.2 绑定Activity和Service 2010-06-21 16:57 李宁 中国水利水电出版社 字号:T | T <Android/OPhone开发完全讲义>第8章Andro ...
- android 47 service绑定
如果一个service已经启动了,activity和service绑定了在解除邦定,则这个service不会销毁,因为这个service不是这个Activity创建的. service生命周期: Ac ...
- activity与service进程内通信
package com.example.binbin.testbinder; import android.app.Service; import android.content.Intent; im ...
- Activity Fragment Service生命周期图
service的生命周期,从它被创建开始,到它被销毁为止,可以有两条不同的路径: A started service 被开启的service通过其他组件调用 startService()被创建. 这种 ...
- Android activity和service的生命周期对比
1Activity生命周期 七个方法 1. void onCreate(Bundle savedInstanceState) 当Activity被第首次加载时执行.我们新启动一个程序的时候其主窗体的o ...
- Activity与Service进行数据交互
Android启动Service有两种方法,一种是startService,一种是bindService.生命周期如下: 执行startService时,调用者如果没有stopService,Serv ...
- Activity和Service的生命周期(图)
1.Activity的生命周期 情形一.一个单独的Activity的正常的生命过程是这样的:onCreate->onStart->onPause->onStop->onDest ...
- Activity与Service通信(不同进程之间)
使用Messenger 上面的方法只能在同一个进程里才能用,如果要与另外一个进程的Service进行通信,则可以用Messenger. 其实实现IPC(Inter-Process Communicat ...
- Android之Activity与Service通信
一.当Acitivity和Service处于同一个Application和进程时,通过继承Binder类来实现. 当一个Activity绑定到一个Service上时,它负责维护Service实例的引用 ...
随机推荐
- HDU4055 - number string(DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4055 思路:dp[i][j]表示处理前i个字符以j结尾可能的序列数. 当a[i]=='I'时,dp[i ...
- My97DatePicker日期控件用法
用法很简单,主要演示都在myDate.html <meta http-equiv="content-type" content="text/html; chars ...
- latex figure \label 放在\caption 后
http://www.cnblogs.com/loca/p/4264686.html latex figure \label 放在\caption 后,否则将显示\section 或者\subsect ...
- Fix VNC Desktop Sharing on Ubuntu Desktop 14.04
Solution 1 sudo apt-get -y install dconf-tools dconf write /org/gnome/desktop/remote-access/require- ...
- Codeforces Round #298 (Div. 2) B. Covered Path
题目大意: 一辆车,每秒内的速度恒定...第I秒到第I+1秒的速度变化不超过D.初始速度为V1,末速度为V2,经过时间t,问最远能走多远. 分析 开始的时候想麻烦了.讨论了各种情况.后来发现每个时刻的 ...
- ios多语言设置,操作
多语言在应用程序中一般有两种做法:一.程序中提供给用户自己选择的机会: NSArray *languages = [NSLocale preferredLanguages]; NSString *cu ...
- zboot/piggyback.c
/* * linux/zBoot/piggyback.c * * (C) 1993 Hannu Savolainen */ /* * This program reads the c ...
- 5 款傻瓜式手机 APP 开发工具
http://www.oschina.net/news/21552/5-app-creators
- 1.PHP站内搜索 分类: PHP开发实例 2015-07-31 22:48 4人阅读 评论(0) 收藏
PHP站内搜索:多关键字.加亮显示 1.SQL语句中的模糊查找 $sql = "SELECT * FROM `message` WHERE `content`like '%$k[0]%' a ...
- UVa 12100打印队列(队列)
原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...