初识Service
来源:互联网 发布:java 两张图片合成 编辑:程序博客网 时间:2024/06/05 07:56
一.启动Service–startService
步骤,方法
1.
准备工作
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/textView1" android:onClick="doclick" android:text="startService" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/button1" android:onClick="doclick" android:text="stopService" />
2.创建一个类继承Service实现生命周期中的四个方法
@Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); }@Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; }
当点击的时候利用接口的方法启动服务
public void doclick(View v){ switch (v.getId()) { case R.id.button1: //注意,这就是普通的启动service的方法 //此处service1声明在头部为Intent对象 service1 = new Intent(MainActivity.this,MyService.class); startService(service1); break; case R.id.button2: stopService(service1); break;}
二.绑定Service–bindService
前两步都是一样的
继承service时实现的方法不一样
1.准备工作
<Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/textView2" android:onClick="doclick" android:text="bind" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/button3" android:onClick="doclick" android:text="unbind" />
2.创建一个类继承Service实现生命周期中的四个方法
@Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); } //因为我们要试着把service对象闯入activity中用,而binder类中封装了这方面的方法,所以我们新建一个这样的类继承binder并返回一个binder对象 public class MyBinder extends Binder{ public MyBindService getService(){ return MyBindService.this; } } @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub //在这new一个binder对象并返回,那么只要service已绑定,就会有一个当前帮定的服务对象传出 return new MyBinder(); } @Override public boolean onUnbind(Intent intent) { // TODO Auto-generated method stub return super.onUnbind(intent); }
3.创建ServiceConnection对象,第一个方法防止服务没解绑的意外推出,第二个方法连接服务之后执行的方法,我们之所有要用类还有一个原因就是在下面要bindService()和unbind()的时候有一个connection的对象参数
ServiceConnection conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName arg0) { // TODO Auto-generated method stub } @Override public void onServiceConnected(ComponentName arg0, IBinder binder) { // TODO Auto-generated method stub service = ((MyBinder)binder).getService(); } };
4.添加事件
case R.id.button3: service2 = new Intent(MainActivity.this,MyBindService.class); bindService(service2, conn, Service.BIND_AUTO_CREATE); break; case R.id.button4: unbindService(conn); break;
还可以做一下兼容
当activity退出的时候自动销毁或解绑service
protected void onDestroy() { // TODO Auto-generated method stub unbindService(conn); super.onDestroy(); }
0 0
- 初识 Service
- Service初识
- 初识Service
- 初识 Service(一)
- Windows Service初识
- Web Service初识
- Service初识(一)
- Service初识(二)
- android 初识service(一)
- Android Service(一) Service初识
- web service 初识 axis (一)
- [Android开发入门]初识Service
- 初识android——Service
- XFire初识-使用Eclipse开发Web Service
- [SharePoint 2010] 初识SharePoint BI -- PerformancePoint Service
- [SharePoint 2010] 初识SharePoint BI -- PerformancePoint Service
- [SharePoint 2010] 初识SharePoint BI -- PerformancePoint Service
- web service初识和问题(一)
- clearfix清除浮动
- MFC:Picture控件的用法
- C++浅析——继承类内存分布和虚析构函数
- 100万并发连接服务器笔记之1M并发连接目标达成
- Triplet Format for Sparse Matrices
- 初识Service
- codeforces-432B Football Kit
- 求N!最后非0位 O(N log N)
- MFC:CCheckListBox使用教程
- iOS OpenGL基础
- 17--黑马程序员--基础加强之泛型
- vi使用
- 【linux驱动】linux驱动总览
- POJ 1157 解题报告