安卓服务(Service)的两种开启方式以及服务的生命周期

安卓服务(Service)的两种开启方式以及服务的生命周期

大家好,又见面了,我是全栈君。

安卓中服务的开启方式

一:採用start的方式开启服务

调用函数:startService(Intent)->onCreate()->onStart()/onStartCommand()->onDestroy()
特点:服务被开启后不会反复开启,仅仅会调用onStart(),服务仅仅会被停止一次。
二:採用bind的方式开发服务
调用函数:bindService(Intent…)->onCreate()->onBind()->onUnBind()->onDestroy();
特点:绑定不会调用onStart()[过时了]和onStartCommand()方法。

两种服务的差别:
start方式开发服务,一旦服务开启跟调用者就没有不论什么关系了。比方我们的服务是在Activity中调用开启的,当Activity关闭的时候,服务不会关闭。
bind方式开启服务,调用者没了。服务也会关闭,能够理解为同生共死。

对于start开启服务的方式比較简单。重点解说bind的方式。

样例:
1.布局里面设置三个button
demo截图
2.为button设置监听事件。有好几种方式。
3.处理事件。

当点击绑定的时候:

    /*绑定的创建方式 */
    Intent intent = new Intent(this, MyService.class);
    bindService(intent, conn, BIND_AUTO_CREATE);

conn是自己实现的功能类,继承自ServiceConnection。
完整代码例如以下:

public class MainActivity extends ActionBarActivity {
    private MyConn conn;
    private Call c;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//Call相似中间人的功能。
    }
    public void bind(View v){
        conn = new MyConn();
        Intent intent = new Intent(this, MyService.class);
        bindService(intent, conn, BIND_AUTO_CREATE);
    }

    public void unbind(View v){
        unbindService(conn);
        c = null;
    }
    /* * 调用服务里的方法 */
    public void call(View v){
        c.callMethodInService();
    }

    private class MyConn implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            System.out.println("调用服务里面的方法");
            c = (Call) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }
}

service代码例如以下:

public class MyService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        System.out.println("服务被成功绑定了");
        return new Call();
    }

    @Override
    public void onCreate() {
        System.out.println("onCreate");
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("onstartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        System.out.println("ondestroy");
        super.onDestroy();
    }

    public void methodInService(){
        Toast.makeText(this, "methodInService", 0).show();
    }

    public class Call extends Binder{
        public void callMethodInService(){
            methodInService();
        }
    }
}

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.servicelife.MainActivity" >
    <Button android:onClick="bind" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="start"/>

    <Button android:onClick="unbind" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="end"/>

    <Button android:onClick="call" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="调用服务里的方法"/>


</LinearLayout>

点击下载源码

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115459.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • checkbox选中和不选中的值_设置checkbox选中状态

    checkbox选中和不选中的值_设置checkbox选中状态1.设置选中:$(“#hasApply”).prop(“checked”,true);设置不选中:$(“#hasApply”).prop(“checked”,false);或如下方法://$(“#ck”).attr(“checked”,true)//选中//$(“#ck”).attr(“checked”,false)//未选中2.获取选中的状态:varstatus…

    2022年4月20日
    976
  • 运行时异常和编译异常

    运行时异常和编译异常解释异常中的编译异常以及运行时异常 并介绍异常处理方式

    2025年9月17日
    3
  • struts-request 请求参数的生命

    struts-request 请求参数的生命

    2021年8月17日
    60
  • STM32中文参考手册_haar小波分解

    STM32中文参考手册_haar小波分解注:本文是程序的说明和实现思路,代码见:https://download.csdn.net/download/hnxyxiaomeng/10301718一、主要思路原始信号:OrgSig信号长度:DWT_SIG_LEN小波分解层数:N与MATLAB类似,小波分解后产生2个数组DWT_L和DWT_C,但定义与MATLAB不同。定义如下:DWT_L:[DWT_SIG_LEN,c…

    2022年10月19日
    2
  • linux抓包UDP流量[通俗易懂]

    linux抓包UDP流量[通俗易懂]a)安装工具,命令如下:yuminstall-yngrepb)抓包,命令如下:timeout5ngrep-qWbyline’XXX’-dloport80

    2022年8月31日
    4
  • NHibernate 进阶

    NHibernate 进阶  在我的最近文章中,我介绍了 Nhibernate。在这里和在其他的论坛中 , 因为我没有强调NHibernate 只是许多可得的 ORM 解决方案之一,(事实上,现在对.NET开发者来说,开源的加上商业的 ORM 的架构现在是超过 50个可供选择)。 作为一个开发顾问,我会经常用到Hibernate(大家都知道它吧)既然我必须在.NET平台下选择一个,NHibernate是我最明智的选择。 我

    2022年7月14日
    16

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号