Android Service控件用法实例分析

Android Service控件用法实例分析,第1张

概述本文实例讲述了AndroidService控件用法。分享给大家供大家参考,具体如下:1、Service是一个应用程序的组件

本文实例讲述了AndroID Service控件用法。分享给大家供大家参考,具体如下:

1、Service是一个应用程序的组件
2、Service没有图形化界面
3、用来处理耗时比较长的功能(下载、播放MP3)
4、更新ContentProvIDer、Intent以及系统的启动

ServcIE不是一个单独的进程,不是一个线程

定义一个Service比较简单,只要继承Service类,实现其生命周期的方法即可。一个定义好的Service必须在AndroIDManifest.xml文件中通过<service>声明才能使用

<service androID:name="MyService">  <intent-filter>    <action androID:name="hb.com.MYSERVICE"/>  </intent-filter></service>

备注:MyService一定要是继承了Service类的,并且名称和类名是一致的

action的名称是自定义的,只要在bindService或者 *** 作Intent的时候就能够被捕获

public class MyService extends Service {  public class mybinder extends Binder{    public MyService getMyService(){      return MyService.this;    }  }  public voID test(){    System.out.println("test");  }  @OverrIDe  public IBinder onBind(Intent intent) {    // Todo auto-generated method stub    System.out.println("onBind");    return new mybinder();  }  @OverrIDe  public voID onCreate() {    // Todo auto-generated method stub    System.out.println("onCreate");    super.onCreate();  }  @OverrIDe  public boolean onUnbind(Intent intent) {    System.out.println("I am unbind");    return super.onUnbind(intent);  }}
public class MainActivity extends Activity {  private static final String MYSERVICE = "hb.com.MYSERVICE";  private boolean flag = false;  //bindService()方法需要ServiceConnection接口作为参数,所以定义了这个变量,目的是为了实现里面的两个方法  ServiceConnection conn = new ServiceConnection() {    @OverrIDe    public voID onServicedisconnected(Componentname name) {      System.out.println("onServicedisconnected");    }    @OverrIDe    public voID onServiceConnected(Componentname name,IBinder service) {      System.out.println("onServiceConnected");      mybinder mybinder = (mybinder)service;      MyService myService = mybinder.getMyService();      myService.test();    }  };  @OverrIDe  public voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.main);    findVIEwByID(R.ID.start).setonClickListener(new button.OnClickListener(){      @OverrIDe      public voID onClick(VIEw v) {        Intent intent = new Intent();        intent.setAction(MYSERVICE);        //先调用MyService的onCreate()方法,然后调用onBind()方法,最后调用onServiceConnected()方法        //因此IBinder返回的值就是通过onBind()方法返回的对象,定义getMyService()方法是为了得到MyService对象,用户在这个类中添加自己需要的一些方法,这样可以做一些逻辑处理        bindService(intent,conn,Service.BIND_auto_CREATE);        flag = true;      }    });    findVIEwByID(R.ID.stop).setonClickListener(new button.OnClickListener(){      @OverrIDe      public voID onClick(VIEw v) {        if(flag){          //取消绑定事件,会调用MyService的onUnbind()方法,但是不会调用onServicedisconnected()这个方法,原因不明          unbindService(conn);          flag = false;        }      }    });  }}

更多关于AndroID相关内容感兴趣的读者可查看本站专题:《Android文件 *** 作技巧汇总》、《Android编程开发之SD卡 *** 作方法汇总》、《Android开发入门与进阶教程》、《Android资源 *** 作技巧汇总》、《Android视图View技巧总结》及《Android控件用法总结》

希望本文所述对大家AndroID程序设计有所帮助。

总结

以上是内存溢出为你收集整理的Android Service控件用法实例分析全部内容,希望文章能够帮你解决Android Service控件用法实例分析所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1149144.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存