Service 服务

是一种应用组件,可长时间后台运行,不提供用户界面。如音乐播放器/下载程序。不能自己运行。

使用Service的方式:

(一)startService():

调用者和服务之间没有联系,即使调用者退出了,服务仍然进行;调用者(Activity)无法访问服务中的方法,因为不能自己new出来服务,new出来的就不是服务了,只是普通对象。

onCreate()->onStartCommand()->服务启动->onDestroy()。注意onCreate()只执行一次,Service实例只有一个。

1) 编写Service的子类或者间接子类;

2) 重写方法:onStartCommand()、onBind()、onCreate()、onDestroy();

onBind()方法返回null即可,所需操作写在onStartCommand()方法中。

3) 使用Intent启动Service,与启动其他Activity一样,方法换成startService(),类由OtherActivity换为Service子类。Intent同时可以向Service传递数据。

4) 在manifest文件中声明服务:<service android:name=”.Service”/>,与Activity同一级别。

5) 终止Service用方法stopService()。在setting-》application-》runningService中可以查看到服务正在运行。

6) int onStartCommand(Intent intent,int flag,int startId):startId为该服务唯一标识,类似身份证号。

(二)bindService():

调用者和服务绑定在一起,调用者一旦退出,服务也就终止;调用者可以访问服务中的方法(不能直接创建对象访问,要用下面代码介绍的办法)。

onCreate()->onBind()->onUnbind()->onDestroy()

BoundService允许其他组件(如Activity)绑定到这个Service上,可以发送请求,也可以接受请求,甚至进行进程间的通话。BoundService仅仅在服务于其他组件时存在,不能独自无限期的在后台运行。

如何创建BindService:

当创建一个能提供绑定功能的服务时,我们必须提供一个IBinder对象,客户端能使用该对象与服务进行交互。IBinder对象创建的三种方式:(参见dev->Service->BoundServices)

1) 继承Binder类,步骤如下:

a. 在Service类中,定义Binder子类MyBinder,在其中定义用于返回BoundService对象的getService()方法。

b. 在Service类中,定义MyBinder对象,并在onBind()方法中返回该对象。

c. 在Service类中,可以定义其他公有方法,以便将来被Service对象调用。

a. 在Activity类中,定义ServiceConnection接口的对象,重写onServiceConnected()方法和onServiceDisconnected()方法。

b. 在Activity类中,启动服务时执行bindService(intent,ServiceConnection,flag)方法,在服务连接成功时自动调用onServiceConnected(ComponentName,IBinder)方法,此方法的参数IBinder就是Service类中onBind()方法的返回值。因此可在此方法中得到Service对象,并可以调用Service类中定义的方法。onServiceDisconnected()方法很少调用,一般是当服务突然异常终止的时候调用。

参数flag取Context类中的常量:

Context.BIND_AUTO_CREATE:绑定时自动创建Service;最常用。

BIND_DEBUG_UNBIND:包含错误解绑时调试帮助。等等,查阅帮助文档。

(三)混合开启服务   解决调用者一旦退出退出,服务仍然开启

package com.example.shiyan5;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder; public class MyService extends Service { @Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
System.out.println("绑定我的start服务");
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
System.out.println("创建我的start服务");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
System.out.println("启动我的start服务");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
System.out.println("销毁我的start服务");
super.onDestroy();
}
}

start方式启动服务

package com.example.shiyan5;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder; public class BindService extends Service { @Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
System.out.println("绑定我的Bind服务");
return new MyBinder1();
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
System.out.println("创建我的Bind服务");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
System.out.println("启动我的Bind服务");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
System.out.println("销毁我的Bind服务");
super.onDestroy();
}
public class MyBinder1 extends Binder{}
}

Bind开启服务

package com.example.shiyan5;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder; public class Music extends Service { @Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
System.out.println("Music——onBind");
return new MyBinder();
}
public class MyBinder extends Binder{
public void callLast()
{
last(); }
public void callPlay()
{
play(); }
public void callNext()
{
next(); }
public void callmusicstop()
{
musicstop(); }
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate(); System.out.println("准备音乐播放器");
}
public void last(){ System.out.println("播放上一首");
}
public void play(){
System.out.println("正在播放");
}
public void next(){
System.out.println("播放下一首");
}
public void musicstop(){ System.out.println("暂停播放");
} public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
System.out.println("启动我的Music服务");
return super.onStartCommand(intent, flags, startId);
}
public void onDestroy() {
// TODO Auto-generated method stub
System.out.println("销毁我的Bind服务");
super.onDestroy();
}
}

混合服务 音乐播放

package com.example.shiyan5;

import com.example.shiyan5.Music.MyBinder;

import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection; import android.view.View; public class MainActivity extends Activity { private MyConnection con;
private MyBinder control;
private MyConnection1 conn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); System.out.println("activity_onCreate"); } // start启动服务
public void start(View v){
Intent intent=new Intent(this,MyService.class);
startService(intent);
System.out.println("开始服务按钮");
}
public void closes(View v){
Intent intent=new Intent(this,MyService.class);
stopService(intent);
System.out.println("停止服务按钮");
} //绑定Service服务
public void bindStart(View v){ Intent service1=new Intent(this,BindService.class);
conn = new MyConnection1();
bindService(service1, conn, BIND_AUTO_CREATE);
System.out.println("BInd_bindStart按钮");
}
public void bindClose(View v){
unbindService(conn);
System.out.println("BInd_unbindService按钮");
} private class MyConnection1 implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) { System.out.println("Bind_连接我的服务");
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
System.out.println("BInd_onServiceDisconnected");
}
} //音乐播放服务 public void start_music(View v){
System.out.println("开始音乐服务按钮");
Intent intent1=new Intent(this,Music.class);
startService(intent1); }
public void stop_music(View v){
System.out.println("停止服务按钮");
Intent intent2=new Intent(this,Music.class);
stopService(intent2); }
public void startbind_music(View v){ Intent service =new Intent(this,Music.class);
con = new MyConnection();
bindService(service,con,BIND_AUTO_CREATE);
System.out.println("BInd_bindStart按钮");
}
public void destory_music(View v){ unbindService(con);
System.out.println("销毁音乐服务Music按钮");
}
public void last( View v){
control.callLast();
}
public void play(View v){ control.callPlay();
}
public void musicstop(View v){
control.callmusicstop();
}
public void next(View v){
control.callNext();} private class MyConnection implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
System.out.println("Music_onServiceconnected");
control = (MyBinder)arg1; } @Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
System.out.println("Music_onServiceDisconnected");
} } }

MainActivity

<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"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开启服务"
android:onClick="start"
android:id="@+id/btn_s"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关闭服务"
android:onClick="closes"
android:id="@+id/btn_c"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开启绑定服务"
android:onClick="bindStart"
android:id="@+id/btn_bs"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关闭绑定服务"
android:onClick="bindClose"
android:id="@+id/btn_bc"
/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动音乐服务"
android:onClick="start_music"
android:id="@+id/btn_sm"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止音乐服务"
android:onClick="stop_music"
android:id="@+id/btn_stm"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绑定音乐服务"
android:onClick="startbind_music"
android:id="@+id/btn_bm"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="销毁音乐服务"
android:onClick="destory_music"
android:id="@+id/btn_dm"
/> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="前一首"
android:onClick="last"
android:id="@+id/btn_b"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放"
android:onClick="play"
android:id="@+id/btn_p"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停"
android:onClick="musicstop"
android:id="@+id/btn_m"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一首"
android:onClick="next"
android:id="@+id/btn_l"
/>
</LinearLayout> </LinearLayout>

XML

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shiyan5"
android:versionCode=""
android:versionName="1.0" > <uses-sdk
android:minSdkVersion=""
android:targetSdkVersion="" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.shiyan5.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <service android:name="com.example.shiyan5.MyService"></service>
<service android:name="com.example.shiyan5.BindService"></service>
<service android:name="com.example.shiyan5.Music"></service>
</application> </manifest>

manifest.xml

安卓开发,Service 服务的更多相关文章

  1. 安卓开发service

    如果把Activity比喻为前台程序,那么service可以看做是一个后台程序.Service跟Activity一样也由Intent调用. 在工程里想要添加一个Service,先新建继承Service ...

  2. Android 综合揭秘 —— 全面剖释 Service 服务

    引言 Service 服务是 Android 系统最常用的四大部件之一,Android 支持 Service 服务的原因主要目的有两个,一是简化后台任务的实现,二是实现在同一台设备当中跨进程的远程信息 ...

  3. C#编写WINNT服务,随便解决安卓开发遇到的5037被众多程序无节操占用的问题

    需求分析: 最近重新开始学习安卓开发,好久不用的ADT集成开发环境频繁遇到不能在仿真机和真机上调试的问题,也就是本人另一篇博文描述的ADB(Android Debug Bridge)监控的5037被金 ...

  4. [开发笔记]-控制Windows Service服务运行

    用代码实现动态控制Service服务运行状态. 效果图: 代码: #region 启动服务 /// <summary> /// 启动服务 /// </summary> /// ...

  5. (转)在SAE使用Apple Push Notification Service服务开发iOS应用, 实现消息推送

    在SAE使用Apple Push Notification Service服务开发iOS应用, 实现消息推送 From: http://saeapns.sinaapp.com/doc.html 1,在 ...

  6. Linux下用gSOAP开发Web Service服务端和客户端程序

    网上本有一篇流传甚广的C版本的,我参考来实现,发现有不少问题,现在根据自己的开发经验将其修改,使用无误:另外,补充同样功能的C++版本,我想这个应该更有用,因为能用C++,当然好过受限于C. 1.gS ...

  7. ubuntu下安装 gSOAP 用于C/C++开发web service服务端与客户端

    昨天在ubuntu下进行安装gSOAP,费了很多时间,没成功,今天又来找了大量教程资料,终于一次成功,这里写下自己的安装步骤和方法,供大家参考. 首先下载gsoap,我下载的是gsoap-2.8.1. ...

  8. 使用CXF开发Web Service服务

    1.使用CXF开发Web Service服务端 1.1 开发一个Web Service业务接口,该接口要用@WebService修饰 (1)创建一个Java项目MyServer (2)在MyServe ...

  9. 使用axis开发web service服务端

    一.axis环境搭建 1.安装环境 JDK.Tomcat或Resin.eclipse等. 2.到 http://www.apache.org/dyn/closer.cgi/ws/axis/1_4下载A ...

随机推荐

  1. GPG配置、命令、实例与apt-key密钥测试

    环境 Ubuntu18.04 gpg version 2.24 参考文档 GnuPG (简体中文) 例子文档 阮一峰 key Management 简介 他人用公钥来加密,自己用私钥来解密 自己用私钥 ...

  2. [编辑排版]小技巧---markdown 转 richText

    Markdown 使用markdown,可以方便地编辑富文本,VSCode自带了对markdown的支持,编辑完成后可点击右上角预览,实时查看效果. 在github上有给账号,就可以使用GithubP ...

  3. BUUCTF WEB-WP(3)

    BUUCTF WEB 几道web做题的记录 [ACTF2020 新生赛]Exec 知识点:exec命令执行 这题最早是在一个叫中学生CTF平台上看到的类似,比这题稍微要复杂一些,多了一些限制(看看大佬 ...

  4. 这个Maven依赖的问题,你敢说你没遇到过

    Maven 依赖没处理好的话经常会导致发生一些问题,非常烦.今天给大家分享一个依赖相关的问题,说不定你之前就遇到过. 问题背景 有个 ES 搜索的项目,刚开始还是好好的状态,过了一段时间,然后就发现启 ...

  5. PAT1067 试密码 (20分)——测试点4分析 一个易错点

    1067 试密码 (20分)   当你试图登录某个系统却忘了密码时,系统一般只会允许你尝试有限多次,当超出允许次数时,账号就会被锁死.本题就请你实现这个小功能. 输入格式: 输入在第一行给出一个密码( ...

  6. Alpha冲刺 —— 5.9

    这个作业属于哪个课程 软件工程 这个作业要求在哪里 团队作业第五次--Alpha冲刺 这个作业的目标 Alpha冲刺 作业正文 正文 github链接 项目地址 其他参考文献 无 一.会议内容 1.总 ...

  7. Rocket - devices - TLError

    https://mp.weixin.qq.com/s/s_6qPkT2zwdqYLw5iK7_8g 简单介绍TLError的实现. 1. 继承自DevNullDevice TLError继承自DevN ...

  8. JSON.parse() 的实现

    目录 1. JSON.parse() 2. 前置知识 2.1 JSON格式中的数据类型 2.2 转义字符的处理 2.2 判断对象是否相等 2.3 寻找匹配的字符串 2.4 基础的递归思想 3. 实现流 ...

  9. 微信小程序单选按钮radio选中的值value的获取方法,setTimeout定时器的用法

    获取radio值的方法: func:function(e){ var val=e.detail.value;//获取radio值,类型:字符串 var val2=parseInt(val);//将字符 ...

  10. JavaScript (五) js的基本语法 - - - 面向对象、工程模式、内置对象、JSON

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.编程思想 1.定义: 编程思想:把一些生活中做事的经验融入到程序中 面向过程:凡事都要亲力亲为,每件 ...