android 之 Service(Context BindService())
2011-10-19 10:09
344 查看
Service 学习第二篇,我们使用BindService()来启动Service.
第一步,同样需要继承Service ,实现它必须的方法。并把这个Service注册到AndroidManifest.xml中,做为一种隐式意图,然后等待Activity的启动。
这里IBinder onBind(Intent intent);会返回一个IBinder 对象。就是我们Acitvity 与Service 交互的对象。
BindService.java
第二步:我们使用三个按钮,分别绑定Service和解除绑定,和获取Service状态。
main.xml
第三步:关键性代码。
第四步:Service启动流程图。
Activity 与 Service 交互的结果。
Service的生命周期
第一步,同样需要继承Service ,实现它必须的方法。并把这个Service注册到AndroidManifest.xml中,做为一种隐式意图,然后等待Activity的启动。
这里IBinder onBind(Intent intent);会返回一个IBinder 对象。就是我们Acitvity 与Service 交互的对象。
BindService.java
package com.hkrt.action; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; public class BindService extends Service { private int count; private boolean quit; private MyBinder binder = new MyBinder(); public class MyBinder extends Binder { public int getCount() { return count; } } @Override public IBinder onBind(Intent intent) { System.out.println("service is Binded"); return binder; } @Override public void onCreate() { System.out.println("Service is Create"); super.onCreate(); new Thread(){ @Override public void run(){ while(!quit){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } count++; } } }.start(); } @Override public boolean onUnbind(Intent intent) { System.out.println("Service is Unbind"); return true; } @Override public void onDestroy() { System.out.println("Service is Destroy"); super.onDestroy(); this.quit=true; } }AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hkrt.action" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".ServiceDemo2Activity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".BindService"> <intent-filter > <action android:name="com.hkrt.action.BIND_SERVICE"/> </intent-filter> </service> </application> </manifest>
第二步:我们使用三个按钮,分别绑定Service和解除绑定,和获取Service状态。
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <TableLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TableRow > <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="绑定Service" android:id="@+id/bind" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="解除绑定" android:id="@+id/unBind" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="获取Service状态" android:id="@+id/serviceCount" /> </TableRow> </TableLayout> </LinearLayout>效果图:
第三步:关键性代码。
package com.hkrt.action; 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.view.View; import android.widget.Button; import android.widget.Toast; public class ServiceDemo2Activity extends Activity { Button bind, unBind, serviceCount; BindService.MyBinder binder; private ServiceConnection conn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { binder = (BindService.MyBinder) service; System.out.println("service Connected"); } @Override public void onServiceDisconnected(ComponentName name) { System.out.println("service disConnectioned"); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); bind = (Button) findViewById(R.id.bind); unBind = (Button) findViewById(R.id.unBind); serviceCount = (Button) findViewById(R.id.serviceCount); final Intent intent = new Intent(); intent.setAction("com.hkrt.action.BIND_SERVICE"); bind.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { bindService(intent, conn, Service.BIND_AUTO_CREATE); } }); unBind.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { unbindService(conn); } }); serviceCount.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(ServiceDemo2Activity.this, "service的count值为" + binder.getCount(), 1).show(); } }); } }
第四步:Service启动流程图。
Activity 与 Service 交互的结果。
Service的生命周期
相关文章推荐
- Android学习札记三:初涉Service(1)之Context.startService()与Context.bindService()区别
- Android Context.bindService 返回 false 问题
- android 之 Service(Context BindService())
- Android学习札记三:初涉Service(1)之Context.startService()与Context.bindService()区别(转)
- Android:Context.startService/Context.bindService的区别
- 深入理解Android的startservice和bindservice
- Android TabActivity无法正常bindService解决方法
- Android 5.0源码分析---startService与bindService的区别
- 深入理解Android的startservice和bindservice
- 【转】Android TabActivity无法正常bindService解决方法
- android 开发StartService 和BindService
- android - BindService
- Android bindService失败,解决方法。
- Android Bind Service 使用Messenger
- Android Service 服务(三)—— bindService与remoteService
- [AndroidTips]startService与bindService的区别
- Android Service (Bind Service,使用 Messenger)
- Service组件(startService,stopService,bindService,unbindService方法比较)-Android
- Android bindservice失败解决方法
- (转)Android中BindService方式使用的理解