博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
赵雅智:service_bindService生命周期
阅读量:7097 次
发布时间:2019-06-28

本文共 3405 字,大约阅读时间需要 11 分钟。

案例演示

布局文件

  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.   
  7.     <Button  
  8.         android:id="@+id/btn_bind"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_below="@+id/btn_stop"  
  13.         android:onClick="click"  
  14.         android:text="context.bindService" />  
  15.   
  16.     <Button  
  17.         android:id="@+id/btn_unbind"  
  18.         android:layout_width="match_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_below="@+id/btn_bind"  
  21.         android:onClick="click"  
  22.         android:text="context.unbindService" />  
  23.   
  24.   
  25. </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.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="14"  
  9.         android:targetSdkVersion="19" />  
  10.   
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.example.android_servicedemo.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.   
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.   
  26.         <service  
  27.             android:name="ServiceTese"  
  28.             android:permission="com.example.permission.servers" >  
  29.             <intent-filter>  
  30.                 <action android:name="com.example.serversdemo.action" />  
  31.             </intent-filter>  
  32.         </service>  
  33.     </application>  
  34.   
  35. </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。生命周期为
点击context.
bindService--->home键--->点击context.
bindService--->点击context.
bindService..

.--->点击context.stopService

所以仅仅能点击一次unbind

startService生命周期见

你可能感兴趣的文章
CGAffineTransform相关函数
查看>>
字符编码与字符集区别与联系(网页/PHP文件/MYSQL数据库乱码问题)
查看>>
黑马程序员-----const和readonly的区别
查看>>
转载:基于MapXtreme的鹰眼功能
查看>>
黄聪:远程序桌面登录的.NET(C#)开发
查看>>
JMeter聚合报告(Aggregate Report)理解
查看>>
C# 多线程Thread.IsBackground=True的作用
查看>>
Oracle数据库安装问题记录
查看>>
Error:flask_sqlalchemy
查看>>
算法3-排序-简单选择排序
查看>>
使用mysql触发器脚本,解决流水数据的添加。
查看>>
SIP and RTP Stack
查看>>
Activity间用Intent、Bundle、onActivityResult进行传值
查看>>
在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke。
查看>>
AC自动机 - 多模式串的匹配运用 --- HDU 3065
查看>>
B-树学习笔记
查看>>
黑客发布iOS 4.1永久越狱程序
查看>>
T-Mobile证实数据泄露 但客户暂无风险
查看>>
使用 MEF 公开 Silverlight“.NET研究” MVVM 应用程序中的接口
查看>>
Random Sequence 2011ACM福州赛区网络赛
查看>>