Android---Service(生命周期、启动方式、服务通信、实战演练、思维导图、高级音乐播放器-源码)
目 录
一、服务的创建
二、服务的生命周期
三、服务的启动方式
(1)startService 方式 启动 服务
实战演练---startService
(2)bindService 方式 启动 服务
实战演练---bindService
四、服务的通信
实战演练---音乐播放器
Service(服务)思维导图
音乐播放器---高级实战(源码)
添加音乐-详细步骤
运行截图
菜鸟教程【Service初涉】
一、服务的创建
二、服务的生命周期
三、服务的启动方式
(1)startService 方式 启动 服务
服务的启动方式 :
startService()方法启动服务,服务会长期的在后台运行,并且服务的状态与开启者的状态没有关系,
即使启动服务的组件已经被销毁,服务会依旧运行。
实战演练---startService
relative.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg"><Buttonandroid:id="@+id/btn_start"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@id/btn_stop"android:layout_centerHorizontal="true"android:layout_marginBottom="90dp"android:background="#B0E0E6"android:onClick="start"android:text="开启服务"android:textColor="#6C6C6C"android:textSize="18sp"/><Buttonandroid:id="@+id/btn_stop"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@id/btn_start"android:layout_alignParentBottom="true"android:layout_marginBottom="25dp"android:background="#080808"android:onClick="stop"android:text="关闭服务"android:textColor="#6C6C6C"android:textSize="18sp"/>
</RelativeLayout>
MyService.java :
package cn.lwx.service;import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;public class MyService extends Service {public MyService() {}@Overridepublic IBinder onBind(Intent intent) {// TODO: Return the communication channel to the service.
// throw new UnsupportedOperationException("Not yet implemented");return null;}@Overridepublic void onCreate() {super.onCreate();Log.i("StartService","onCreate()");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.i("StartService","onStartCommand()");return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {super.onDestroy();Log.i("StartService","onDestroy()");}
}
MainActivity.java :
package cn.lwx.service;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;
import android.os.Bundle;
import android.view.View;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.relative);}// 开启服务的方法public void start(View view){Intent intent = new Intent(this, MyService.class);startService(intent);}// 关闭服务的方法public void stop(View view){Intent intent = new Intent(this, MyService.class);stopService(intent);}
}
源码【工程文件】--- 可以直接用Gitee拷贝到Android Studio 上。
https://gitee.com/lwx001/Service
筛选LogCat信息 :
运行效果图 :
(2)bindService 方式 启动 服务
实战演练---bindService
源码【工程文件】--- 可以直接用Gitee拷贝到Android Studio 上。
https://gitee.com/lwx001/Service2
MainActivity.java :
package cn.lwx.service;import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.DialogTitle;import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;public class MainActivity<btnUnbind> extends AppCompatActivity {private MyService2.MyBinder myBinder;//成员变量保存返回值private MyConn myconn; //内部类@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.relative2);}// // ---Service1---
// // 开启服务的方法
// public void start(View view){
// Intent intent = new Intent(this, MyService.class);
// startService(intent);
// }
//
// // 关闭服务的方法
// public void stop(View view){
// Intent intent = new Intent(this, MyService.class);
// stopService(intent);
// }//绑定服务public void btnBind(View view) {//避免重复创建myconnif (myconn == null) {myconn = new MyConn();}Intent intent = new Intent(this, MyService2.class);//参数1是Intent,参数2是连接对象,参数3是flags-表示:如果服务不存在就创建服务bindService(intent, myconn, BIND_AUTO_CREATE);}//解绑服务public void btnUnbind(View view) {if (myconn != null) {unbindService(myconn);myconn = null;}}//调用服务中的方法public void btnCall(View view) {myBinder.callMethodInService();}//创建内部类MyConn,用于实现连接服务private class MyConn implements ServiceConnection {//当成功绑定到服务时,调用的方法,返回MyService2里面的Ibinder对象@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {myBinder = (MyService2.MyBinder) service;Log.i("MainActivity", "服务绑定成功,内存地址为:" + myBinder.toString());}//当服务失去连接时,调用的方法@Overridepublic void onServiceDisconnected(ComponentName name) {}}}
MyService2.java :
package cn.lwx.service;import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;public class MyService2 extends Service {private static final String TAG = "MyService2"; // logt 回车public MyService2() {}//创建服务的代理,调用服务中的方法class MyBinder extends Binder {public void callMethodInService() {methodInService();}}@Overridepublic void onCreate() {Log.i("MyService2", "创建服务,调用onCreate()方法");super.onCreate();}@Overridepublic IBinder onBind(Intent intent) {// TODO: Return the communication channel to the service.
// throw new UnsupportedOperationException("Not yet implemented");Log.i("MyService2", "绑定服务,调用onBind()");return new MyBinder();}public void methodInService() {Log.i("MyService2", "自定义方法 methodInService()");}@Overridepublic boolean onUnbind(Intent intent) {Log.i("MyService2", "解绑服务,调用onUnbind()");return super.onUnbind(intent);}
}
relative.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg1"android:orientation="vertical"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@+id/btn_call"android:layout_alignLeft="@+id/btn_call"android:layout_marginBottom="50dp"android:background="#F5F5DC"android:onClick="btnBind"android:text="绑定服务"android:textSize="16sp" /><Buttonandroid:id="@+id/btn_call"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@+id/btn_unbind"android:layout_alignParentEnd="true"android:layout_alignParentRight="true"android:layout_marginEnd="30dp"android:layout_marginRight="30dp"android:layout_marginBottom="55dp"android:background="#F5F5DC"android:onClick="btnCall"android:paddingLeft="5dp"android:paddingRight="5dp"android:text="调用服务中的方法"android:textSize="16sp" /><Buttonandroid:id="@+id/btn_unbind"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@id/btn_call"android:layout_alignParentBottom="true"android:layout_marginBottom="70dp"android:background="#F5F5DC"android:onClick="btnUnbind"android:text="解绑服务"android:textSize="16sp" />
</RelativeLayout>
运行效果图 :
四、服务的通信
实战演练---音乐播放器
https://share.weiyun.com/1vVLYnlb
https://gitee.com/lwx001/MusicPlayer
运行程序,没有声音。不知道为啥,QAQ ~ Help~
Service(服务)思维导图
音乐播放器---高级实战(源码)
源码【工程文件】:可直接用gitee拷贝至个人的Android Studio上。
【文件丢失:可联系QQ:386335886】
https://gitee.com/wang_zhiguo/Course0902
添加音乐-详细步骤
运行截图
菜鸟教程【Service初涉】
菜鸟教程【Service初涉】
https://www.runoob.com/w3cnote/android-tutorial-service-1.html
Android---Service(生命周期、启动方式、服务通信、实战演练、思维导图、高级音乐播放器-源码)相关推荐
- Android复习13【广播:思维导图、音乐播放器】
音乐播放器Android代码下载:https://wws.lanzous.com/ifqzihaxvij 思维导图 https://share.weiyun.com/1vVLYnlb 音乐播放器
- Android音乐播放器源码(歌词.均衡器.收藏.qq5.0菜单.通知)
一款Android音乐播放器源码,基本功能都实现了 qq5.0菜单(歌词.均衡器.收藏.qq5.0菜单.通知) 只有向右滑动出现,菜单键和指定按钮都还没有添加. 源码下载:http://code.66 ...
- linux 音频播放器源码,Android音乐播放器源码
相当完整的Android音乐播放器,直接上效果图及源代码,自己欣赏,具体不再解释了,可以说是一个很给力的Android音乐播放器. 示例代码: /* * Copyright (C) 2009 Tele ...
- 最全的Android开源音乐播放器源码汇总
收集了很多音乐播放器类的Android项目源码,非常不错的开源项目,会让你事半功倍,希望大家补充...谢谢! Android基于经纬度切歌的冲绳音乐播放器源码 http://neast.cn/foru ...
- Android音乐播放器源码
转载自:https://blog.csdn.net/xch_yang/article/details/103916201 android开发音乐播放器,能够很好的应用Android基础知识,是个不错的 ...
- Android 编程案例-本地音乐播放器源码及使用注意事项
说一下代码在用的时候注意事项以及在运行的时候可能遇到的问题: 首先代码可以在创建相应文件后直接复制,这个案例用到了RecyclerView,所以需要先添加依赖.添加下面两个: implementati ...
- 【Android】Android开源项目(一)音乐播放器源码汇总
作为一个有追求的程序员来说,项目源码必须看,但是网上那么多资源是不让你无从下手啊,博主今天为大家推荐五个经典项目吧. 一.android-UniversalMusicPlayer 这个开源项目展示了如 ...
- android高仿酷狗音乐播放器源码下载
这是一款简单的读取SD卡音乐文件进行播放.暂停.删除.切歌等功能的高仿酷狗音乐播放器. 主要功能: 模块 简要说明 扫描SD卡音乐 扫描SD卡,并显示出本地音乐列表 提供歌词跟随音乐滚动更能 采用 ...
- 多款Android播放器源码集锦
Android传说中的3D播放器源码 Android 3D播放器 源码 170 2011-09-28 by:豆沙包 • android音乐播放器源码 android 237 2011-09-03 by ...
最新文章
- 高级转录组分析和R语言数据可视化第十三期 (线上线下同时开课)
- ssm查询一条数据并显示_高亮显示查询数据,其实很简单
- 前端学习(2510):文件目录
- I00009 用1生成回文数
- shell学习之跳出循环
- 别用Date了,Java8新特性之日期处理,现在学会也不迟!
- app上传遇到的一些问题
- windows安装软件最好使用独立的文件夹
- 装饰器结构应用与基本使用(611)
- 联想拯救者R720笔记本换三星970 EVO PLUS 500G固态硬盘与加装联想内存条16GX2,固态硬盘降温设置方法
- 南开大学c语言100题,计算机二级C语言上机(南开大学)100题.doc
- ASP.NET公司企业网站源码
- 如何把握云计算时代风口 怎么能掌握云计算技术
- linux dns劫持转发,linux的dns被劫持
- 旅游类APP-Android模块分析
- win10系统,打开word文档慢慢慢
- 回天转债,莱克转债上市价格预测
- ORACLE各种常见java.sql.SQLException归纳
- 二十三、python中操作MySQL步骤
- 数理统计基础 统计推断概述
热门文章
- 企业信息管理计算机考什么,考信息系统运行管理员要学什么?
- 八十、React中的容器组件和无状态组件
- tornado环境搭建
- 深度学习开发者的AI Party来了!WAVE SUMMIT+2021与你相约双十二
- 从会议、医患沟通和客服对话三大场景看对话文本摘要技术
- CVPR 2021 | 从理论和实验角度深入剖析对比损失中的温度超参数
- B站疯传,一整套人工智能学习资料免费送,不谢!
- 机器翻译中丢掉词向量层会怎样?
- 直播 | DSTC 8“基于Schema的对话状态追踪”竞赛冠军方案解读
- 它来了,它来了,最强目标检测算法YOLO v4,它真的来了!!!