Service的浅谈
发布日期:2021-06-29 04:56:05
浏览次数:4
分类:技术文章
本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
不错!
[***.144.177.141]2024年04月16日 05时58分23秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
python实验os.walk的用法
2019-04-29
tensorflow的四个交叉熵函数写法区别
2019-04-29
解决TensorFlow多次运行神经网络冲突的问题
2019-04-29
Ubuntu中安装了GPU—相关查看指令
2019-04-29
《尚硅谷数据结构》-二叉树的遍历
2019-04-29
shell和python交互(循环版本)
2019-04-29
Chrome的插件
2019-04-29
为什么randn生成的向量均值不为0
2019-04-29
梯度下降算法结束条件
2019-04-29
深度学习相关好的博客分享(持续更新)
2019-04-29
tensorboard可视化使用问题解决
2019-04-29
深度学习中张量(Tensor)的理解
2019-04-29
CNN卷积神经网络简单介绍与详细搭建注释-识别mnist手写黑白数字
2019-04-29
pip相关问题解决(2)-修改源码
2019-04-29
关于安装qtpands最新版(1.04)
2019-04-29
解决ModuleNotFoundError: No module named 'matplotlib.pyplot'; 'matplotlib' is not a package
2019-04-29
关于Python继承类中的super用法
2019-04-29
经典卷积神经网络结构——LeNet-5、AlexNet、VGG-16
2019-04-29