Service的学习代码
1. startService(new Intent(MainActivity.this, MyService.class))------->stopService(new Intent(MainActivity.this, MyService.class))
2.bindService(new Intent(MainActivity.this, MyService.class), mServiceConnection,Context.BIND_AUTO_CREATE)---->
if(mServiceConnection != null){unbindService(mServiceConnection);mServiceConnection = null;
}----->一般放在onDestory()函数里面
MainActivity.java
private MyService myService;
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Log.i("jxy",this.getClass().getName() + "--->onServiceConnected");
myService = ((MyService.MyBinder)iBinder).getService();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.i("jxy",this.getClass().getName() + "--->onServiceDisconnected");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onDestroy() {
super.onDestroy();
if(mServiceConnection != null){
unbindService(mServiceConnection);
mServiceConnection = null;
}
}
public void startService(View view) {
startService(new Intent(MainActivity.this, MyService.class));
}
public void stopService(View view) {
stopService(new Intent(MainActivity.this, MyService.class));
}
public void bindService(View view) {
bindService(new Intent(MainActivity.this, MyService.class), mServiceConnection,
Context.BIND_AUTO_CREATE);
}
public void mp3player(View view){
myService.mp3Play((String) view.getTag());
}
MyService.java
class MyBinder extends Binder{
public MyService getService(){
return MyService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
// Log.i("jxy",this.getClass().getName() + "--->onBind");
// Binder binder = new Binder();
// Log.i("jxy",this.getClass().getName() + "--->binder:" + binder);
return new MyBinder();
}
@Override
public boolean onUnbind(Intent intent) {
Log.i("jxy",this.getClass().getName() + "--->onUnbind");
return super.onUnbind(intent);
}
@Override
public void onCreate() {
super.onCreate();
Log.i("jxy",Thread.currentThread().getName() + ":MyService --> onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("jxy",Thread.currentThread().getName() + "MyService --> onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("jxy",Thread.currentThread().getName() + "MyService --> onDestroy");
}
// 此方法完成MP3播放的功能
public void mp3Play(String name){
Log.i("jxy",this.getClass().getName() + "--->正在播放的歌曲名称为:" + name);
}
activity.xml
<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"
android:padding="16dp"> <Button
android:onClick="startService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="启动服务" /> <Button
android:onClick="stopService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止服务" />
<Button
android:onClick="bindService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="绑定服务" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="解绑服务" /> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="调用系统服务挂断电话" /> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试黑名单拦截(调用系统服务)" /> <Button
android:tag="相信自己"
android:onClick="mp3player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="音乐播放测试" /> </LinearLayout> Manifest.xml文件的service的注册
<service android:name=".MyService" />
Service的学习代码的更多相关文章
- 如何免费使用GPU跑深度学习代码
从事深度学习的研究者都知道,深度学习代码需要设计海量的数据,需要很大很大很大(重要的事情说三遍)的计算量,以至于CPU算不过来,需要通过GPU帮忙,但这必不意味着CPU的性能没GPU强,CPU是那种综 ...
- java反射机制学习代码
根据 http://www.iteye.com/topic/137944 文档进行学习 代码如下: package reflectTest; import java.lang.reflect.*; i ...
- 常用统计分析python包开源学习代码 numpy pandas matplotlib
常用统计分析python包开源学习代码 numpy pandas matplotlib 待办 https://github.com/zmzhouXJTU/Python-Data-Analysis
- Web Service循序渐进学习
1.Web service 是什么? (WebService 是可以通过互联网远程访问调用的应用程序,实现数据传输共享.这种应用程序理解为不同服务提供帮助.能够支撑实现应用程序发布访问的技术可以称为W ...
- swift2.0 字符串,数组,字典学习代码
swift 2.0 改变了一些地方,让swift变得更加完善,这里是一些最基本的初学者的代码,里面涉及到swift学习的最基本的字符串,数组,字典和相关的操作.好了直接看代码吧. class View ...
- 420小时学习代码之后:如何教你免费自学Python
原文地址:learning-to-code-420-hours-later-how-to-teach-yourself-python-for-free 说明:有些网址需要FQ. 大约在1.5年前,我开 ...
- WEB前端学习代码片段记录
1.JS设计模式片段 Function.prototype.addMethod = function (name,fn) { this.prototype[name] = fn; return thi ...
- OpenCV学习代码记录——人脸检测
很久之前学习过一段时间的OpenCV,当时没有做什么笔记,但是代码都还在,这里把它贴出来做个记录. 代码放在码云上,地址在这里https://gitee.com/solym/OpenCVTest/tr ...
- Service Fabric学习-从helloworld开始(无状态服务)
原先做服务器程序, 都是部署在xx云上, 也没理解云是个啥, 不就是个服务器(虚拟机)租赁商吗? 好吧, 其实这个是IaaS, 而接下来要学习的ServiceFabric(以下简称SF)是PaaS. ...
随机推荐
- linux系统挂载ISO文件
1 上传iso文件使用xftp上传系统oracleLinux7.3.iso光盘镜像到/toolsPackage 为方便使用,重命名光盘镜像文件[root@rhel64 software]# mv rh ...
- 对于某些浏览器不支持placeholder的解决办法
$(function () { if (!placeholderSupport()) { $('[placeholder]').focus(function () { var input = $(th ...
- POJ 2585.Window Pains 拓扑排序
Window Pains Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1888 Accepted: 944 Descr ...
- 【Sikuli】Sikuli 文档
http://sikulix-2014.readthedocs.io/en/latest/index.html
- Fiddler的钩子hook导致电脑无法连上网络
今天,电脑怎么都无法连上网络,重启了几次电脑也不行,网络环境是没有问题的,后来同事告诉我,Fiddler有一个BUG,就是Fiddler获取钩子之后没有释放掉,必须启动Fiddler,再关闭Fiddl ...
- smrtlink
SMRT Link is the web-based end-to-end workflow manager for the Sequel™ System. It includes software ...
- 521. Longest Uncommon Subsequence I
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- java实现网站paypal支付功能并且异步修改订单的状态
java实现网站paypal支付功能并且异步修改订单的状态:步骤如下 第一步:去paypal的官网https://www.paypal.com注册一个个人账号,在创建沙箱测试账号时需要用到 第二步:p ...
- 2018.07.06 POJ1698 Alice's Chance(最大流)
Alice's Chance Time Limit: 1000MS Memory Limit: 10000K Description Alice, a charming girl, have been ...
- 功能强大的文件上传插件带上传进度-WebUploader
WebUploader是由Baidu WebFE(FEX)团队开发的一个以HTML5/FLASH构建的现代文件上传组件.在现代的浏览器里面能充分发挥HTML5的优势,同时又不摒弃主流IE浏览器,沿用老 ...