zoukankan      html  css  js  c++  java
  • 赵雅智:service_bindService生命周期


    案例演示

    布局文件

    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生命周期见http://blog.csdn.net/zhaoyazhi2129/article/details/32711481


  • 相关阅读:
    用ASP.Net2005和Oracle9i做模糊查询时报ORA01722错误解决方法
    ☠360与腾讯QQ的战争☠之我见
    【转】GIS原理学习
    【转】JavaScript写的Cookie类
    怀疑自己的笔记本电脑可能受到了网络攻击被人监视或操控
    “密码人”越来越多将会出现“密码危机”
    【转】确保 ASP.NET 应用程序和 Web Services 的安全
    超图软件(SuperMap)学习资料:桌面软件(Deskpro)
    用Sql Server 2000的数据库备份来还原Sql Server 2005中的数据库
    一个有用的Windows服务小程序——用来完成Server端的Socket通信
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/7351066.html
Copyright © 2011-2022 走看看