Service的浅谈
发布日期:2021-06-29 04:56:05 浏览次数:3 分类:技术文章

本文共 3892 字,大约阅读时间需要 12 分钟。

package com.example.fafa.justabouthandler;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.support.annotation.Nullable;import android.util.Log;/** * Created by zhang on 2017/7/3. */public class MyService extends Service {    //为了让活动和服务关联起来 借助onbind方法 把自己写的新的binder方法 用onbind返回    private DownloadBinder mbinder= new DownloadBinder();    public MyService() {    }    @Nullable    @Override    public IBinder onBind(Intent intent) {        return mbinder;    }    @Override    public void onCreate() {        super.onCreate();        Log.e("TAG", "onCreate:  exe" );    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Log.e("TAG", "onStartCommand: exe" );        return super.onStartCommand(intent, flags, startId);    }    @Override    public void onDestroy() {        Log.e("TAG", "onDestroy: exe" );        super.onDestroy();    }    class DownloadBinder extends Binder{        public void satrtDownload(){            Log.e("TAG", "satrtDownload exe" );        }        public int getProgress(){            Log.e("TAG", "getProgress exe");            return 0;        }    } } //首先先建立一个服务    然后再建立一个活动来关联服务 package com.example.fafa.justabouthandler;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;/** * Created by zhang on 2017/7/3. */public class MyserviceActivity extends AppCompatActivity implements View.OnClickListener {    private Button start;    private Button stop;    private Button bind;    private Button unbind;    private MyService.DownloadBinder downloadBinder;    private ServiceConnection connection=new ServiceConnection() {        @Override        public void onServiceConnected(ComponentName name, IBinder service) {            downloadBinder= (MyService.DownloadBinder) service;            downloadBinder.satrtDownload();;            downloadBinder.getProgress();        }        @Override        public void onServiceDisconnected(ComponentName name) {        }    };    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_myservice);        initView();    }    private void initView() {        start = (Button) findViewById(R.id.start_service);        stop = (Button) findViewById(R.id.stop_service);        bind = (Button) findViewById(R.id.bind_service);        unbind = (Button) findViewById(R.id.unbind_service);        start.setOnClickListener(this);        stop.setOnClickListener(this);        bind.setOnClickListener(this);        unbind.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.start_service:                Intent startIntent= new Intent(this,MyService.class);                startService(startIntent);                break;            case R.id.stop_service:                Intent stopIntent= new Intent(this,MyService.class);                stopService(stopIntent);                break;            case R.id.bind_service:                //绑定的时候需要一个connection 所以创建个ServiceConnection在onServiceConnected()中来指挥服务干什么                Intent bindintent=new Intent(this,MyService.class);                bindService(bindintent,connection,BIND_AUTO_CREATE);                break;            case R.id.unbind_service:                unbindService(connection);                break;        }    }} //布局文件里面只有四个按钮
//关于服务的声明周期 一旦调用startservice()方法 服务就会启动起来并且回调onStartCommand()
//方法服务启动后会一直保持运行状态直到停止服务或者第on工了stopself()方法虽然每调用一次启动方
法onStartCommand就会执行一次 但是只要调用一次停止就行了,如果同时启动了服务和绑定了服务那么
就要同时点击停止服务和解绑 服务才回销毁

转载地址:https://blog.csdn.net/zhang_linhua/article/details/74188759 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:JAVA基础知识精华版
下一篇:安卓中关于Handler机制的浅谈

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月23日 03时02分46秒