Activity和Service绑定后,可以方便Activity随时调用对应的Service里面的方法

绑定代码如下

Activity类代码:

  1. <span style="font-size:16px;">package com.fox.Activity;
  2. import com.fox.Activity.service.Service1;
  3. import android.app.Activity;
  4. import android.content.ComponentName;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.ServiceConnection;
  8. import android.os.Bundle;
  9. import android.os.IBinder;
  10. import android.util.Log;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.Toast;
  14. public class Activity1 extends Activity {
  15. private Button btn1 = null;
  16. private static String LOG="mp3";
  17. @Override
  18. public void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.main);
  21. btn1 = (Button) findViewById(R.id.button1);
  22. btn1.setOnClickListener(new btn1ClickListener());
  23. //开始绑定
  24. Intent intent = new Intent(Activity1.this,Service1.class);
  25. bindService(intent, conn, Context.BIND_AUTO_CREATE);
  26. }
  27. private Service1 myservice = null;//绑定的service对象
  28. //连接对象,重写OnserviceDisconnected和OnserviceConnected方法
  29. public ServiceConnection conn= new ServiceConnection() {
  30. @Override
  31. public void onServiceDisconnected(ComponentName name) {
  32. Log.i(LOG, "onServiceDisconnected>>>>>>>>");
  33. myservice = null;
  34. }
  35. @Override
  36. public void onServiceConnected(ComponentName name, IBinder service) {
  37. Log.i(LOG, "onServiceConnected>>>>>>>>");
  38. myservice = ((Service1.MyBinder)service).getService();
  39. Log.i(LOG, myservice+">>>>>>>>");
  40. }
  41. };
  42. class btn1ClickListener implements View.OnClickListener {
  43. @Override
  44. public void onClick(View v) {
  45. unbindService(conn);
  46. }
  47. }
  48. }</span>

Service类代码:

  1. <span style="font-size:16px;">package com.fox.Activity.service;
  2. import android.app.Service;
  3. import android.content.Intent;
  4. import android.os.Binder;
  5. import android.os.IBinder;
  6. import android.util.Log;
  7. public class Service1  extends Service{
  8. private final IBinder binder = new MyBinder();
  9. private static final String LOG="mp3";
  10. @Override
  11. public IBinder onBind(Intent intent) {
  12. Log.i(LOG, "onBind............");
  13. return binder;
  14. }
  15. /**
  16. * 该类是获得Service对象
  17. * @author Administrator
  18. *
  19. */
  20. public class MyBinder extends Binder{
  21. public Service1 getService(){
  22. return Service1.this;
  23. }
  24. }
  25. @Override
  26. public void onCreate() {
  27. Log.i(LOG, "oncreate............");
  28. super.onCreate();
  29. }
  30. @Override
  31. public void onStart(Intent intent, int startId) {
  32. Log.i(LOG, "onstart............");
  33. super.onStart(intent, startId);
  34. }
  35. @Override
  36. public int onStartCommand(Intent intent, int flags, int startId) {
  37. Log.i(LOG, "onstartcommand............");
  38. return super.onStartCommand(intent, flags, startId);
  39. }
  40. @Override
  41. public void onDestroy() {
  42. Log.i(LOG, "ondestory............");
  43. super.onDestroy();
  44. }
  45. }
  46. </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绑定的更多相关文章

  1. 8.1.2 绑定Activity和Service

    8.1.2 绑定Activity和Service 2010-06-21 16:57 李宁 中国水利水电出版社 字号:T | T <Android/OPhone开发完全讲义>第8章Andro ...

  2. android 47 service绑定

    如果一个service已经启动了,activity和service绑定了在解除邦定,则这个service不会销毁,因为这个service不是这个Activity创建的. service生命周期: Ac ...

  3. activity与service进程内通信

    package com.example.binbin.testbinder; import android.app.Service; import android.content.Intent; im ...

  4. Activity Fragment Service生命周期图

    service的生命周期,从它被创建开始,到它被销毁为止,可以有两条不同的路径: A started service 被开启的service通过其他组件调用 startService()被创建. 这种 ...

  5. Android activity和service的生命周期对比

    1Activity生命周期 七个方法 1. void onCreate(Bundle savedInstanceState) 当Activity被第首次加载时执行.我们新启动一个程序的时候其主窗体的o ...

  6. Activity与Service进行数据交互

    Android启动Service有两种方法,一种是startService,一种是bindService.生命周期如下: 执行startService时,调用者如果没有stopService,Serv ...

  7. Activity和Service的生命周期(图)

    1.Activity的生命周期 情形一.一个单独的Activity的正常的生命过程是这样的:onCreate->onStart->onPause->onStop->onDest ...

  8. Activity与Service通信(不同进程之间)

    使用Messenger 上面的方法只能在同一个进程里才能用,如果要与另外一个进程的Service进行通信,则可以用Messenger. 其实实现IPC(Inter-Process Communicat ...

  9. Android之Activity与Service通信

    一.当Acitivity和Service处于同一个Application和进程时,通过继承Binder类来实现. 当一个Activity绑定到一个Service上时,它负责维护Service实例的引用 ...

随机推荐

  1. HDU4055 - number string(DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4055 思路:dp[i][j]表示处理前i个字符以j结尾可能的序列数. 当a[i]=='I'时,dp[i ...

  2. My97DatePicker日期控件用法

    用法很简单,主要演示都在myDate.html  <meta http-equiv="content-type" content="text/html; chars ...

  3. latex figure \label 放在\caption 后

    http://www.cnblogs.com/loca/p/4264686.html latex figure \label 放在\caption 后,否则将显示\section 或者\subsect ...

  4. 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- ...

  5. Codeforces Round #298 (Div. 2) B. Covered Path

    题目大意: 一辆车,每秒内的速度恒定...第I秒到第I+1秒的速度变化不超过D.初始速度为V1,末速度为V2,经过时间t,问最远能走多远. 分析 开始的时候想麻烦了.讨论了各种情况.后来发现每个时刻的 ...

  6. ios多语言设置,操作

    多语言在应用程序中一般有两种做法:一.程序中提供给用户自己选择的机会: NSArray *languages = [NSLocale preferredLanguages]; NSString *cu ...

  7. zboot/piggyback.c

    /* *    linux/zBoot/piggyback.c * *    (C) 1993 Hannu Savolainen */ /* *    This program reads the c ...

  8. 5 款傻瓜式手机 APP 开发工具

    http://www.oschina.net/news/21552/5-app-creators

  9. 1.PHP站内搜索 分类: PHP开发实例 2015-07-31 22:48 4人阅读 评论(0) 收藏

    PHP站内搜索:多关键字.加亮显示 1.SQL语句中的模糊查找 $sql = "SELECT * FROM `message` WHERE `content`like '%$k[0]%' a ...

  10. UVa 12100打印队列(队列)

    原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...