赵雅智:service_bindService生命周期

赵雅智:service_bindService生命周期

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。生命周期为
赵雅智:service_bindService生命周期

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

点击context.bindService--->home键--->点击context.bindService--->点击context.bindService..
赵雅智:service_bindService生命周期

.--->点击context.stopService

赵雅智:service_bindService生命周期

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

上一篇:赵雅智_BroadcastReceiver短信监听


下一篇:5、jvm内存回收——算法