Service中有四个重要函数:

publicIBinderonBind(Intentarg0);//必须实现,返回接口给ServicepublicvoidonCreate();//Service创建时调用publicvoidonStart(Intentintent,intstartId);//通过startService()会调用publicvoidonDestroy();//销毁时StopService()调用


通过StartActivity()函数启动Service,当第一次调用时会分别调用onCreate()和onStart在();

之后只会调用onStart();

通过函数StopService()结束Service,会调用onDestroy();

调用BindService():当Service未创建时调用onCreate()和onBind();当创建了只调用onBind();

使用函数bindService()和函数unbindService()可以绑定和解除绑定

对已经绑定的Service调用bindService()无效,即多次调用bindService()和调用一次bindService()一样。 unbindService()只能使用一次,即对于一个绑定的Service,只能调用一次unbindService(),多次调用会产生错误


该函数原型为:

bindService(Intent,ServiceConnection,BIND_AUTO_CREATE);

ServiceConnection是一个服务连接类,必须实现以下两个函数:

publicvoidonServiceConnected(ComponentNamearg0,IBinderarg1)//连接成功时调用publicvoidonServiceDisconnected(ComponentNamearg0)//连接失败时调用

示例如下:

privateServiceConnectionconn=newServiceConnection(){@OverridepublicvoidonServiceConnected(ComponentNamearg0,IBinderarg1){//TODOAuto-generatedmethodstubToast.makeText(MainActivity.this,"success",Toast.LENGTH_LONG).show();Log.i("SERVICE","success");}@OverridepublicvoidonServiceDisconnected(ComponentNamearg0){//TODOAuto-generatedmethodstubToast.makeText(MainActivity.this,"errer",Toast.LENGTH_LONG);Log.i("SERVICE","errer");}



Service实例:

MainActivity.java:

privateServiceConnectionconn=newServiceConnection(){@OverridepublicvoidonServiceConnected(ComponentNamearg0,IBinderarg1){//TODOAuto-generatedmethodstubToast.makeText(MainActivity.this,"success",Toast.LENGTH_LONG).show();Log.i("SERVICE","success");}@OverridepublicvoidonServiceDisconnected(ComponentNamearg0){//TODOAuto-generatedmethodstubToast.makeText(MainActivity.this,"errer",Toast.LENGTH_LONG);Log.i("SERVICE","errer");}};
protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Buttonbutton1=(Button)this.findViewById(R.id.btn1);Buttonbutton2=(Button)this.findViewById(R.id.btn3);Buttonbutton3=(Button)this.findViewById(R.id.btn4);Buttonbutton4=(Button)this.findViewById(R.id.btn5);button1.setOnClickListener(newOnClickListener(){@OverridepublicvoidonClick(Viewarg0){//TODOAuto-generatedmethodstubstartService(newIntent(MainActivity.this,NewService.class));}});button2.setOnClickListener(newOnClickListener(){@OverridepublicvoidonClick(Viewarg0){//TODOAuto-generatedmethodstubstopService(newIntent(MainActivity.this,NewService.class));}});button3.setOnClickListener(newOnClickListener(){@OverridepublicvoidonClick(Viewarg0){//TODOAuto-generatedmethodstubbindService(newIntent(MainActivity.this,NewService.class),conn,BIND_AUTO_CREATE);}});button4.setOnClickListener(newOnClickListener(){@OverridepublicvoidonClick(Viewarg0){//TODOAuto-generatedmethodstubunbindService(conn);}});}

NewService.java:

publicclassNewServiceextendsService{@OverridepublicIBinderonBind(Intentarg0){//TODOAuto-generatedmethodstubToast.makeText(NewService.this,"onBind",Toast.LENGTH_LONG).show();Log.i("SERVICE","onbind");returnnull;}publicvoidonCreate(){super.onCreate();Log.i("SERVICE","oncreat");Toast.makeText(NewService.this,"onCreat",Toast.LENGTH_LONG).show();}publicvoidonStart(Intentintent,intstartId){Log.i("SERVICE","onstart");Toast.makeText(NewService.this,"onStart",Toast.LENGTH_LONG).show();}publicvoidonDestroy(){Log.i("SERVICE","ondestory");Toast.makeText(NewService.this,"onDestory",Toast.LENGTH_LONG).show();}}

Activity.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><Buttonandroid:id="@+id/btn1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="start"/><Buttonandroid:id="@+id/btn3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="stop"/><Buttonandroid:id="@+id/btn4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="bind"/><Buttonandroid:id="@+id/btn5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="unbind"/></LinearLayout>

AndroidManifest.xml增加:

<serviceandroid:name="com.example.new1.NewService"/>




更多相关文章

  1. C语言函数的递归(上)
  2. Android(安卓)-- SurfaceFlinger 合成主线程 系列 (三)
  3. cocos2dx通过Jni调用Android的Java层代码(下)
  4. Android(安卓)Camera Framework层分析
  5. android 中获取所有有效网卡和对应的IP地址
  6. Android(安卓)ListView长按弹出对话框
  7. vold.fstab DirectVolume
  8. Android培训班(110)start_kernel函数7
  9. Android(安卓)StateMachine和AsyncChannel

随机推荐

  1. AppWidget数据持久化
  2. [Android-Tips] findviewbyid function r
  3. 【android】 调用别的应用的activity
  4. TextView的省略号(elipsized属性)工作原理
  5. Security and Permissions安全与权限(三)
  6. Android(安卓)MVP 模式应用实例
  7. Android中使用Timer配合postInvalidate()
  8. Android(安卓)ViewFlipper实现页面的滑动
  9. android CoordinatorLayout使用
  10. 自定义Dialog步骤