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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
网站不错 人气很旺了 加油
[***.192.178.218]2024年04月23日 03时02分46秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
go 闭包
2019-04-29
go 捕获处理error
2019-04-29
Input dispatching timed out 导致anr问题分析
2019-04-29
主线程中Thread.Sleep()是否会导致ANR
2019-04-29
Android 为什么主线程的looper 一直循环不会ANR
2019-04-29
Android View 的绘制流程
2019-04-29
ContentProvider和数据库的区别
2019-04-29
Android四大组件——ContentProvider的增删改查和优化
2019-04-29
华为入局 VR 眼镜能让 VR 早普及几年?|CSDN博文精选
2019-04-29
做好以下四点,拒做 “ 空心 ” 程序员 | CSDN 博文精选
2019-04-29
程序员为什么非得参加一场编程竞赛?
2019-04-29
V 语言强势登顶 GitHub TOP1,欲取 Go 而代之?
2019-04-29
关于RocketMQ消息拉取与重平衡的一些问题探讨
2019-04-29
不同业务场景下如何进行数据库水平切分?
2019-04-29
如何准备算法工程师面试,斩获一线互联网公司机器学习岗offer?
2019-04-29
循环、递归与魔术(一)——递归与循环的数理逻辑
2019-04-29
1030MD
2019-04-29
发布文章---状态--恢复
2019-04-29
保存测试
2019-04-29