watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhemhpMjEyOQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

案例演示

布局文件

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context="${packageName}.${activityClass}" >
  6. <Button
  7. android:id="@+id/btn_bind"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:layout_alignParentLeft="true"
  11. android:layout_below="@+id/btn_stop"
  12. android:onClick="click"
  13. android:text="context.bindService" />
  14. <Button
  15. android:id="@+id/btn_unbind"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:layout_below="@+id/btn_bind"
  19. android:onClick="click"
  20. android:text="context.unbindService" />
  21. </RelativeLayout>

注冊service

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.android_servicedemo"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="14"
  8. android:targetSdkVersion="19" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme" >
  14. <activity
  15. android:name="com.example.android_servicedemo.MainActivity"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. <service
  23. android:name="ServiceTese"
  24. android:permission="com.example.permission.servers" >
  25. <intent-filter>
  26. <action android:name="com.example.serversdemo.action" />
  27. </intent-filter>
  28. </service>
  29. </application>
  30. </manifest>

主activity

package com.example.android_servicedemo;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View; public class MainActivity extends Activity {
private Intent service;
MyConn myConn;
public static final String TAG = "MainActivity"; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 隐士意图的启动
service = new Intent();
service.setAction("com.example.serversdemo.action");
} public void click(View v) {
int id = v.getId();
switch (id) {
case R.id.btn_bind:
// con:该參数是一个ServiceConnection对象。该对象用于监听訪问者与service之间的连接情况
// flag:指定绑定时是否自己主动创建service(假设service还没创建)0:不自己主动创建。BIND_AUTO_CREATE自己主动创建
myConn = new MyConn();
this.bindService(service, myConn, Service.BIND_AUTO_CREATE);
break;
case R.id.btn_unbind:
if (myConn != null) {
this.unbindService(myConn);
}
break;
default:
break;
}
} class MyConn implements ServiceConnection {
/**
* 当连接成功的时候回调此方法
*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
Log.i(TAG, "---------onServiceConnected" + name);
} /**
* 当service所在的宿主矜持因为异常终止或因为其它原因终止,导致该service与訪问者之间断开连接的时候回调
*/
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, "---------onServiceDisconnected" + name); } }
}


执行结果

点击context.bindService--->点击context.unbindService。生命周期为

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhemhpMjEyOQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

点击context.bindService--->home键--->点击context.bindService--->点击context.bindService..

.--->点击context.stopService

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhemhpMjEyOQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

所以仅仅能点击一次unbind

startService生命周期见http://blog.csdn.net/zhaoyazhi2129/article/details/32711481

赵雅智:service_bindService生命周期的更多相关文章

  1. 赵雅智_Fragment生命周期

    官网帮助文档链接:  http://developer.android.com/guide/components/fragments.html 主要看两张图.和跑代码 一,Fragment的生命周 w ...

  2. 赵雅智_service生命周期

    Android中的服务和windows中的服务是类似的东西,服务一般没实用户操作界面.它执行于系统中不easy被用户发觉,能够使用它开发如监控之类的程序. 服务的开发步骤 第一步:继承Service类 ...

  3. 赵雅智:android教学大纲

    带下划线为详细内容链接地址.点击后可跳转.希望给大家尽一些微薄之力.眼下还在整理中 教学章节 教学内容 学时安排 备注 1 Android高速入门 2 Android模拟器与常见命令 3 Androi ...

  4. 赵雅智:service_startService生命周期

    案例演示 布局文件 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xm ...

  5. 赵雅智_BroadcastReceiver

    BroadcastReceiver  用于接收程序(包含用户开放的程序和系统内建程序)所发出的Broadcast intent 耗电量 开机启动 窃取别人短信 窃取别人电话 开发: 创建须要启动的Br ...

  6. 赵雅智_Swift(1)_swift简单介绍及类型

    Swift 是 iOS 和 OS X 应用开发的一门新语言. 假设你有 C 或者 Objective-C 开发经验, Swift 的非常多内容都是你熟悉的. Swift 的类型是在 C 和 Objec ...

  7. 赵雅智_Fragment

    当我们须要动态的多界面切换的时候,就须要将UI元素和Activity融合成一个模块. 在2.3中我们一般通过各种Activity中进行跳转来实现多界面的跳转和单个界面动态改变.在4.0或以上系统中就能 ...

  8. 赵雅智_ContentProvider

    ContentProvider介绍 ContentProvider是不同应用程序之间进行交换数据的标志API 也就是说:一个应用程序通过ContentProvider暴露自己的数据操作接口,那么无论该 ...

  9. 赵雅智:js知识点汇总

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhemhpMjEyOQ==/font/5a6L5L2T/fontsize/400/fill/I0 ...

随机推荐

  1. 《学习opencv》笔记——矩阵和图像操作——cvAbs,cvAbsDiff and cvAbsDiffS

    矩阵和图像的操作 (1)cvAbs,cvAbsdiff,cvAbsDiffS 它们的结构为: void cvAbs( //取src中元素的绝对值,写到dst中 const CvArr* src, co ...

  2. 利用Spring MVC 上传图片文件

    本文转自:http://amcucn.iteye.com/blog/264457.感谢作者 近日在工作当中,需要用到上传图片的功能,然而自己平时学习的时候只会使用struts的上传功能,但因为项目并没 ...

  3. Animatepacker for cocos2d-x 3.0 解析

    AnimatePacker 是大神老G为cocos2d-x2.0做的一个很简洁的工具 近期 e 的新项目用的是最新的cocos2d-x 3.0,因此改进了对应的解析代码 说明: 1.工具还是用的之前的 ...

  4. utils/CCArmatureDefine

    #ifndef __CCARMATUREDEFINE_H__ #define __CCARMATUREDEFINE_H__ //#define _USRDLL 1 #include "coc ...

  5. jsp数据库连接大全和数据库操作封装到Javabean

    一.jsp连接Oracle8/8i/9i数据库(用thin模式) testOracle.jsp如下: <%@ page contentType="text/html;charset=g ...

  6. java中方法drawImage()的参数详细解释

    public abstract boolean drawImage(Image img,int x,int y,int width,int height,ImageObserver observer) ...

  7. java.lang.UnsatisfiedLinkError:no dll in java.library.path终极解决之道

     Java调用Dll时,会出现no dll in java.library.path异常,在Java Project中不常见,因为只要将Dll拷贝到system32目录下即可:         但若是 ...

  8. 玩转rocketMQ

    下载地址https://github.com/alibaba/RocketMQ 安装环境需要jdk,maven,git http://maven.apache.org/download.html

  9. 让子弹飞Demo版

    让子弹飞是我非常喜欢的一款游戏.今天的目标就是利用cocos2dx 3.0 和box2d 打造一款这样的类型游戏的Demo版.本来cocos2dx 3.0 已经封装了physicals模块,可是我在使 ...

  10. sscanf,sprintf用法

    #include<string.h> #include<stdio.h> int main() { ],sztime1[],sztime2[]; sscanf("12 ...