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"
tools:context="com.example.service.MainActivity" >
<Button
android:id="@+id/service_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="start service"
android:onClick="doClick"/>
<Button
android:id="@+id/service_stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="stop service"
android:onClick="doClick"/>
<Button
android:id="@+id/service_bind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="bind serrvice"
android:onClick="doClick"/>
<Button
android:id="@+id/music_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="start the music"
android:onClick="doClick"/>
<Button
android:id="@+id/music_pause"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="pause the music"
android:onClick="doClick"/>
<Button
android:id="@+id/service_unbind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="unbind service"
android:onClick="doClick"/> </LinearLayout>

源代码:

Myservice1:

package com.example.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log; public class MyService extends Service {
@Override//创建服务时调用,在服务没有关闭之前,只执行一次
public void onCreate() {
// TODO Auto-generated method stub
Log.i("info", "onCreate");
super.onCreate();
}
@Override//创建服务后自动调用
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.i("info", "onStartCommeand"); return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.i("info", "onDestroy");
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
} }

Myservice2:

package com.example.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 {
@Override//创建服务时调用,在服务没有关闭之前,只执行一次
public void onCreate() {
// TODO Auto-generated method stub
Log.i("info", "onCreate");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.i("info", "onStartCommeand"); return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.i("info", "onDestroy");
super.onDestroy();
}
//IBindler是一个接口,Binder是其实现类
class MyBinder extends Binder{
//返回一个service对象,给实例源
public MyService2 getService(){
return MyService2.this;
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new MyBinder();//返回一个Binder的子类对象
}
public void music_Start(){
Log.i("info", "播放音乐");
}
public void music_Pause(){
Log.i("info", "暂停播放音乐");
} }

MainActivity:

package com.example.service;

import com.example.service.MyService2.MyBinder;
import com.example.servies.R; import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
/**
* Service:(需注册)
* 定义:
* -应用程序组件
* -后台运行,不可见,没有界面
* -优先级高于Activity
* 用途:
* - 播放音乐,记录地理位置信息的改变,监听某种动作
* 注意:
* -service不是一个单独的进程,一般和应用程序运行在同一个进程中,除非进行指定
* -运行在主线程,不能用它来做耗时操作
* -可以在服务中开一个线程,在线程中做耗时动作
* start方式特点:
* -服务跟启动源没有关系
* -无法得到服务对象
*
* Bind方式特点:(可以对service对象进行数据处理)
* -通过Ibinder接口实例,返回一个ServiceConnection对象给实例源
* -通过ServiceConnection对象的相关方法可以得到service对象
* !!!注意:绑定后续解绑,否则退出程序后报错
*
* 生命周期:
* start方式 startService()-->onCreate()-->Service running-->onDestroy()-->Service shut down
* Bind方式 bindService()-->onCreate()-->Clients are bound to service-->onUnbind()-->onDestroy()-->Service shut down
*
*
* @author Administrator
*
*/
public class MainActivity extends Activity { private Intent intent1;
private MyService2 service;
ServiceConnection conn = new ServiceConnection(){ @Override//当服务跟启动源连接的时候 会自动回调
public void onServiceConnected(ComponentName name, IBinder binder) {
// TODO Auto-generated method stub
service = ((MyBinder)binder).getService();
} @Override//当服务跟启动源断开的时候会自动回调
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub } };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void doClick(View view){
switch(view.getId()){
case R.id.service_start:
intent1 = new Intent(MainActivity.this,MyService.class);
startService(intent1);
break;
case R.id.service_stop:
stopService(intent1);
break;
case R.id.service_bind:
//如果调用了startService(intent)方法,活动结束后必须关闭
Intent intent2 = new Intent(MainActivity.this,MyService2.class);
bindService(intent2,//确定连接的服务
conn,//接收服务对象,自动调用里面的方法
BIND_AUTO_CREATE); //自动创建服务
break;
case R.id.music_start:
service.music_Start();
break;
case R.id.music_pause:
service.music_Pause();
break;
case R.id.service_unbind:
unbindService(conn);//解除绑定,当没有绑定时,解除绑定会报错
break;
}
} }

Android_Service的更多相关文章

  1. Android_Service组件详解

    1.Service概述 Service服务是一个没有用户界面的在后台运行执行操作的应用组件,其它组件可以通过Intent意图启动这个Service去完成特定的功能,比如通过Service可以完成播放音 ...

  2. Android_Service详解及实例

    转自:http://blog.csdn.net/guolin_blog/article/details/11952435    http://blog.csdn.net/guolin_blog/art ...

  3. Android_Service的一些零散知识点_1

    service与线程不甚相同,service是Android提供的可供一个允许常驻后台的组件. 可通过StartService()启动Service和BindService()启动Service St ...

  4. 启动和停止Service

    activity_main <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ...

随机推荐

  1. hdu4632Palindrome subsequence

    http://acm.hdu.edu.cn/showproblem.php?pid=4632 TLE了N次 原因居然是取模次数太多了..! 这数据卡的好紧 还是我写的太搓..828ms挤过 s[i]= ...

  2. MySQL sql_slave_skip_counter

    因为mysql的主从复制是逻辑复制,所以slave在apply relay log的过程中,经常会遇到错误,而参数sql_slave_skip_counter可以设置跳过多少个event,让slave ...

  3. phpMyAdmin 'import.php'跨站脚本漏洞

    漏洞版本: phpMyAdmin phpMyAdmin 3.4.9 phpMyAdmin phpMyAdmin 3.4.8 phpMyAdmin phpMyAdmin 3.4.6 phpMyAdmin ...

  4. jquery easyui treegrid使用小结

    在实际应用中可能会碰到不同的需求,比如会根据每行不同的参数或属性设置来设置同列不同的editor类型,这时原有的例子就显的有点太过简单,不能实现我们的需求,现在应用我在项目中的操作为例,显示下实现同列 ...

  5. Azure Backup 入门

    Viswanath Tata 云 + Enterprise项目经理 Azure Backup是一款允许客户将数据备份到 Azure的强大工具.请参阅这篇文章,快速了解 Azure Backup.我 ...

  6. CSS学习笔记——定位position属性的学习

    今天学习之前剩下的一个问题:CSS的position属性.首先归纳出和position相关的问题: position作为一个属性,它一共有哪几个属性值? position常用的属性值有哪几个?分别有什 ...

  7. Shape 与 InlineShape 的区别

    Shape 对象代表文档中的图形对象,InlineShape 代表文档中的嵌入式图形对象.所谓嵌入式图形对象,是指将图像作为文字处理,在排版上以文字的方式进行排版. Shape 与 InlineSha ...

  8. Paxos算法(转)

    Paxos算法的难理解与算法的知名度一样令人敬仰,从我个人的经历而言,难理解的原因并不是该算法高深到大家智商不够,而在于Lamport在表达该算法时过于晦涩且缺乏一个完整的应用场景.如果大师能换种思路 ...

  9. MVVM Light中的Message

    比喻:像漂流瓶一样发送一个Message,任何人有兴趣就可以拾起来. MVVM Light中的Message的使用分为三个步骤: 1.创建一个类,包含要传递的Message. 2.在ViewModel ...

  10. python中的pth文件作用

    python中有一个.pth文件,该文件的用法是: 首先xxx.pth文件里面会书写一些路径,一行一个. 将xxx.pth文件放在特定位置,则可以让python在加载模块时,读取xxx.pth中指定的 ...