首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >安卓服务(Service)的两种开启方式以及服务的生命周期

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

作者头像
全栈程序员站长
发布2022-07-10 13:56:17
发布2022-07-10 13:56:17
1.8K0
举报

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

安卓中服务的开启方式

一:採用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

2.为button设置监听事件。有好几种方式。 3.处理事件。

当点击绑定的时候:

代码语言:javascript
复制
    /*绑定的创建方式 */
    Intent intent = new Intent(this, MyService.class);
    bindService(intent, conn, BIND_AUTO_CREATE);

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

代码语言:javascript
复制
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代码例如以下:

代码语言:javascript
复制
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();
        }
    }
}

布局文件:

代码语言:javascript
复制
<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.cn/115459.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年2月4,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安卓中服务的开启方式
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档