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. css样式优先级和权重问题

    内联样式: <div style="font-size: 12px;">姓名</div> 外部样式: <link rel="styleshe ...

  2. mRemoteNG

    mRemoteNG 1.摆脱了mstsc那种一个程序一个界面的模式,采用了左边树+右边Tab页的显示形式,让你在一个mRemote界面中,可以连接多个远程桌面,再也不用为切来切去而烦恼了(如上图). ...

  3. [python] can not find app ,module

    can not find module 1 startapp appname 而不是 startproject 2 不要自己创建项目根目录,要用mamage.py生成 can not find app ...

  4. [Automation] 自动化测试工具和测试框架大集合

    8 Open Source Test Automation Tools for Desktop Applications written in WinForms/ WPF: Tool Comment ...

  5. 2018.07.18 HAOI2009 逆序对数列(线性dp)

    传送门 目前只会n2" role="presentation" style="position: relative;">n2n2的dp" ...

  6. 2018.07.08 hdu5316 Magician(线段树)

    Magician Problem Description Fantasy magicians usually gain their ability through one of three usual ...

  7. SIM900 AT来电显示开启,一些代码

    /*Note: this code is a demo for how to using gprs shield to send sms message, dial a voice call and ...

  8. 类内初始值(c++11)

    1.概念 1)可以为数据成员提供一个类内初始值,创建对象时,类内初始值用于初始化数据成员,没有初始值的成员将被默认初始化 2)类内初始值和赋值类似,或者放在花括号里(如数组),或者放在等号右边,不能使 ...

  9. 表格边框重复合并属性: border-collapse:collapse;

    table { border-collapse:collapse; }

  10. 20155226 2016-2017-2 《Java程序设计》第7周学习总结

    20155226 2016-2017-2 <Java程序设计>第7周学习总结 教材学习内容总结 认识时间与日期 六个时间基准: 1.格林威治标准时间 2.世界时 3.国际原子时 4.世界协 ...