服务(Service)的使用方法
2017-01-19 14:53
183 查看
/** * 服务的基本用法 * * ******定义一个服务 * 继承Service类 * 必须重写的方法是onBind * 要完成处理事情的逻辑需要重写的类有onCreate,onStartCommand,onDestroy * * 启动和停止服务 * Intent startIntent=new Intent(this,FileManagerService.class); * startService(startIntent); * Intent stopIntent=new Intent(this,FileManagerService.class); * stopService(stopIntent); * * ******活动和服务进行通信 * 在继承服务的类中需要书写的相关内容,写一个继承Binder的类,在必须重写的方法onBind中返回 * 继承Binder的类的对象: * private ServiceBinder mBinder=new ServiceBinder(); * public class FileManagerService extends Service { * private ServiceBinder extends Binder{ * 定义相关方法 * } * } * @Override * public IBinder onBind(Intent intent){ * return mBinder; * } * * *******在调用服务的类中需要完成的内容; * 定义一个继承Binder的类的对象,再初始化一个ServiceConnection接口,初始化该接口需要重写的类 * 有onServiceDisconnected,onServiceConnected * 在以上方法中可以通过继承Binder的类的对象调用在服务中定义的相关方法,这样就将服务和活动联系 * 在一起了,如下: * private FileManagerService.ServiceBinder serviceBinder; * private ServiceConnection connection=new ServiceConnection(){ * @Override * public void onServiceDisconnected(ComponentName name{ * } * * @Override * public void onServiceConnected(ComponentName name,IBinder service){ * serviceBinder=(FileManagerService.ServiceBinder)service; * //接下来就可以调用在服务中定义的相关方法了 * } * } * *最后进行服务和活动的绑定 * Intent bindIntent=new Intent(this,ServiceBinder.class); * bindService(bindIntent,connection,BIND_AUTO_CREATE); * 解绑 * unbindService(connection); */ 活动中书写的绑定服务的内容: private final ServiceConnection mServiceConnection=new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { LogUtils.d(TAG,"onServiceConnected"); mService=((FileManagerService.ServiceBinder)service).getServiceInstance(); mServiceBinded=true; serviceConnected(); } @Override public void onServiceDisconnected(ComponentName name) { mService.disconnected(this.getClass().getName()); mServiceBinded=false; LogUtils.w(TAG,"onServiceDisconnected"); } }; bindService(new Intent(getApplicationContext(),FileManagerService.class), mServiceConnection,BIND_AUTO_CREATE);//绑定服务 unbindService(mServiceConnection);//解除绑定 服务中书写的内容: import android.app.Service; import android.os.AsyncTask; import java.io.File; import java.util.HashMap; import java.util.Iterator; import java.util.List; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.util.Log; public class FileManagerService extends Service { private ServiceBinder mBinder=null;//用于在onBind方法中返回 @Override public void onCreate(){ super.onCreate(); mBinder=new ServiceBinder(); AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);//设置AsyncTask的运行方式? } @Override public int onStartCommand(Intent intent,int flags,int startId){ LogUtils.d(TAG,"onStartCommand..."); super.onStartCommand(intent,flags,startId); return START_NOT_STICKY; //“非粘性的”。使用这个返回值时,如果在执行完onStartCommand后,服务被异常kill掉,系统不会自动重启该服务 } public class ServiceBinder extends Binder{ //这个方法得到FileServiceInstance的实例 public FileManagerService getServiceInstance(){ return FileManagerService.this; } } @Override public IBinder onBind(Intent intent){ DrmManager.getInstance().init(this); return mBinder; } }
相关文章推荐
- 调用android非unbind的服务中的方法(不使用bindService启动的服务)
- 说说在Android如何使用服务(Service)的方法
- 服务service生命周期及方法使用
- 使用Service的stopSelf()方法来停止服务
- 为什要使用BindService?为了调用服务中的方法
- mysql 使用service mysqld start 提示未识别服务 进入/etc/rc.d/init.d 下面未发现有mysqld解决方法
- Angularjs自定义服务~使用Module的service方法
- C# Web Service 不使用服务引用直接调用方法
- CORBA Programming with TAO - 6.Naming Service(使用命名服务)
- 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(二)----使用方法
- 使用Apache Axis部署 Web服务时的常见问题及其解决方法(转)
- 使用javaservice 将jboss 注册为服务
- 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(二)----使用方法
- 使用Java Service Wrapper 把Java程序作为Windows系统服务
- 使用 WebSphere Service Registry and Repository 实现和执行服务生命周期
- 如何在 Web 服务中使用 Service Data Objects 2.1 的动态数据 API
- npkcrypt 服务启动失败解决方法;事件来源: Service Control Manager;事件 ID: 7000
- C#使用异步Socket实现TCP网络服务的CS的通讯构架(二)使用方法
- 使用Java Service Wrapper 把Java程序作为Windows系统服务
- 使用Apache Axis部署 Web服务时的常见问题及其解决方法