android基础(四)service
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了android基础(四)service,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4374字,纯文字阅读大概需要7分钟。
内容图文
Intent service =new Intent(this,ServiceClass.class);
bindService(Intent service,ServiceConnection conn, int flags);
ServiceConnection conn=new ServiceConnection(){
//service与Activity建立连接后调用的两个方法,用于使用service中的服务 @Override publicvoid onServiceDisconnected(ComponentName arg0) { // TODO Auto-generated method stub } @Override publicvoid onServiceConnected(ComponentName arg0, IBinder arg1) { //arg1为OnBind()方法中返回的Binder对象, 可通过该对象获得service对象本身(在Binder类中有一个getService()方法用于返回service对象本身) MyBinder binder=(MyBinder)arg1; //调用getService()方法获得service对象本身 BindService bindservice=binder.getService(); //接下来便可调用service中的方法来实现一定功能 String result=bindservice.service(); } };
class BindService extends Service { // 实现onBind()方法返回一个Binder对象public IBinder onBind(Intent arg0){ return mybinder; } //内部类Mybinderclass MyBinder extends Binder{ public BindService getService(){ return BindeService.this; } } //BindService中的方法,将使用binder.getService().service()调用public String service(){ // 方法实现 } }
3、IntentService
由于Service中的代码都是运行在主线程中的,如果在Service中处理一些耗时操作,会容易出现ANR的情况。此时需要在Service中开启一个子线程来处理耗时操作。但是会出现忘记开启线程或线程中操作执行完成后忘记停止服务的情况。
所以Android系统提供了一种更为简便的处理Service中ANR情况的方式——使用IntentService。
IntentService有以下特点:
(1) 它创建了一个独立的工作线程来处理所有的通过onStartCommand()传递给服务的intents。
(2) 创建了一个工作队列,来逐个发送intent给onHandleIntent()。
(3) 不需要主动调用stopSelft()来结束服务。因为,在所有的intent被处理完后,系统会自动关闭服务。
(4) 默认实现的onBind()返回null
(5) 默认实现的onStartCommand()的目的是将intent插入到工作队列中
继承IntentService的类至少要实现两个函数:构造函数和onHandleIntent(Intent intent)函数。要覆盖IntentService的其它函数时,注意要通过super调用父类的对应的函数。
onHandleIntent(Intent intent)中的Intent参数是startService(Intent intent)/bindService(Intent intent)中传进的intent对象,该intent对象可以携带一些参数
在onHandleIntent(Intent intent)方法中可以通过intent携带的参数来区分不同的intent(即如果多次启动同一个service要执行不同操作时可在intent对象中传入不同的参数来区别),接下来便可以执行不同的操作。
// Operation 1 Intent startServiceIntent = new Intent("com.test.intentservice"); Bundle bundle = new Bundle(); bundle.putString("param", "oper1"); startServiceIntent.putExtras(bundle); startService(startServiceIntent); //Operation 2 Intent startServiceIntent2 = new Intent("com.test.intentservice"); Bundle bundle2 = new Bundle(); bundle2.putString("param", "oper2"); startServiceIntent2.putExtras(bundle2); startService(startServiceIntent2);
public class IntentServiceDemo extends IntentService { public IntentServiceDemo() { //必须实现父类的构造方法 super("IntentServiceDemo"); } @Override public IBinder onBind(Intent intent) { System.out.println("onBind"); returnsuper.onBind(intent); } @Override publicvoid onCreate() { System.out.println("onCreate"); super.onCreate(); } @Override publicvoid onStart(Intent intent, int startId) { System.out.println("onStart"); super.onStart(intent, startId); } @Override publicint onStartCommand(Intent intent, int flags, int startId) { System.out.println("onStartCommand"); returnsuper.onStartCommand(intent, flags, startId); } @Override protectedvoid onHandleIntent(Intent intent) { //Intent是从Activity发过来的,携带识别参数,根据参数不同执行不同的任务 String action = intent.getExtras().getString("param"); if (action.equals("oper1")) { System.out.println("Operation1"); }elseif (action.equals("oper2")) { System.out.println("Operation2"); } try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } @Override publicvoid onDestroy() { System.out.println("onDestroy"); super.onDestroy(); } }
原文:http://www.cnblogs.com/waterhorse/p/5142896.html
内容总结
以上是互联网集市为您收集整理的android基础(四)service全部内容,希望文章能够帮你解决android基础(四)service所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。