Android(java)学习笔记231:服务(service)之混合方式开启服务
1. 前面我们已经讲过可以使用两种方式开启服务
startService----stopService:
oncreate() ---> onstartCommand() ---> onstartCommand()---> onDestory();
bindService----unbindService:
oncreate() ---> onbind() --->onUnbind() ---> onDestory();
为什么需要采用混合的方式开启服务?
答:startService服务长期后天运行,不可以调用服务里面的方式;
bindService可以调用服务的方法,但是不能长期在后台运行。(bindService和Activity同时死)
综合上面两种方式的优点,采用混合的方式开启服务(继承了上面两种的优点):
(1)服务长期后台运行。
(2)可以调用服务的方法。
2.利用案例说明混合方式开启服务:
(1)创建一个Android项目如下:
(2)我们首先看看布局文件activity_main.xml,如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity" > <Button
android:onClick="start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="开启服务" />
<Button
android:onClick="stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止服务" />
<Button
android:onClick="bind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="绑定服务" />
<Button
android:onClick="unbind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="解除绑定服务" />
<Button
android:onClick="call"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="调用服务的方法" /> </LinearLayout>
布局效果如下:
(3)其中MainActivity.java:
package com.itheima.mix; 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.View; public class MainActivity extends Activity {
private MyConn conn;
private IService iService; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//以start的方式i开启服务
public void start(View view){
Intent intent = new Intent(this,TestService.class);
startService(intent);
}
//停止服务
public void stop(View view){
Intent intent = new Intent(this,TestService.class);
stopService(intent);
}
//绑定服务u
public void bind(View view){
Intent intent = new Intent(this,TestService.class);
conn = new MyConn();
bindService(intent, conn, BIND_AUTO_CREATE);
}
private class MyConn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iService = (IService) service;
} @Override
public void onServiceDisconnected(ComponentName name) { } }
//解绑服务
public void unbind(View view){
unbindService(conn);
} public void call (View view){
iService.callMethodInService();
} }
其中使用到的Service为自定义的TestService,如下:
package com.itheima.mix; import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast; public class TestService extends Service { private class MyBinder extends Binder implements IService{
@Override
public void callMethodInService() {
methodInService();
}
} @Override
public IBinder onBind(Intent intent) {
System.out.println("服务被绑定 onBind");
return new MyBinder();
}
@Override
public boolean onUnbind(Intent intent) {
System.out.println("服务被解绑 onUnbind");
return super.onUnbind(intent);
} @Override
public void onCreate() {
System.out.println("服务被创建 oncreate");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("onStartCommand");
return super.onStartCommand(intent, flags, startId);
} @Override
public void onDestroy() {
System.out.println("服务被销毁 onDestroy");
super.onDestroy();
} public void methodInService(){
Toast.makeText(this, "我是服务里面的方法", 0).show();
}
}
还有上面使用到的接口IService,如下:
package com.itheima.mix; public interface IService {
public void callMethodInService();
}
既然上面使用到了自定义的service的TestService,所以要在AndroidMainfest.xml注册TestService这个组件,如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.mix"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.mix.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.itheima.mix.TestService"></service>
</application> </manifest>
3. 特别注意:
混合模式开启服务
推荐使用的步骤:
(1)startService() ----> 保证服务长期后天运行
(2)如果要调用服务的方法 ---->bindService()绑定服务
(3)就可以调用服务的方法
(4)unbindservice 解绑服务
(5)服务还是长期后台运行
(6)如果要停止服务,stopService();
Android(java)学习笔记231:服务(service)之混合方式开启服务的更多相关文章
- Android(java)学习笔记174:服务(service)之混合方式开启服务
1. 前面我们已经讲过可以使用两种方式开启服务 startService----stopService: oncreate() ---> onstartCommand() ---& ...
- 《Java学习笔记(第8版)》学习指导
<Java学习笔记(第8版)>学习指导 目录 图书简况 学习指导 第一章 Java平台概论 第二章 从JDK到IDE 第三章 基础语法 第四章 认识对象 第五章 对象封装 第六章 继承与多 ...
- Java学习笔记4
Java学习笔记4 1. JDK.JRE和JVM分别是什么,区别是什么? 答: ①.JDK 是整个Java的核心,包括了Java运行环境.Java工具和Java基础类库. ②.JRE(Java Run ...
- 20155234 2016-2017-2第十周《Java学习笔记》学习总结
20155234第十周<Java学习笔记>学习总结 教材学习内容总结 网络编程 在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置,或者接收到指定 ...
- Android:日常学习笔记(2)——分析第一个Android应用程序
Android:日常学习笔记(2)——分析第一个Android应用程序 Android项目结构 整体目录结构分析 说明: 除了APP目录外,其他目录都是自动生成的.APP目录的下的内容才是我们的工作重 ...
- java学习笔记之基础篇
java选择语句之switch //switch可以用于等值判断 switch (e) //int ,或则可以自动转化成int 的类型,(byte char short)枚举jdk 7中可以防止字 ...
- Android Studio 学习笔记(一)环境搭建、文件目录等相关说明
Android Studio 学习笔记(一)环境搭建.文件目录等相关说明 引入 对APP开发而言,Android和iOS是两大主流开发平台,其中区别在于 Android用java语言,用Android ...
- 0028 Java学习笔记-面向对象-Lambda表达式
匿名内部类与Lambda表达式示例 下面代码来源于:0027 Java学习笔记-面向对象-(非静态.静态.局部.匿名)内部类 package testpack; public class Test1{ ...
- Android自动化学习笔记:编写MonkeyRunner脚本的几种方式
---------------------------------------------------------------------------------------------------- ...
随机推荐
- php之分页类代码
/* 思路 1.把地址栏的URL获取 2.分析URL中的query部分--就是?后面传参数的部分 3.query部分分析成数组 4.把数组中的page单元,+1,-1,形成2个新的数组 5.再把新数组 ...
- php基础知识【函数】(3)字符串string
一.大小写转换 1.strtolower()--转换为小写. echo strtolower("Hello WORLD!"); //hello world! 2.strtouppe ...
- TRECT的使用
作为一张画布,在上面绘制各种图形或显示图像,但在CANVAS的使用过程中少不了一个特殊对象,那就是矩形RECT,灵活使用它会完成很多特殊的功能,为Delphi编制的Windows程序增加活力. REC ...
- 更换Python默认软件镜像源
限于一些众所周知的原因,在我们pip安装软件的时候出现类似报错: data = self.read(amt=amt, decode_content=decode_content) File " ...
- python中对文件、文件夹的操作需要涉及到os模块和shutil模块。
创建文件:1) os.mknod("test.txt") 创建空文件2) open("test.txt",w) 直接打开一个文件,如果文件不存在则创建文件 创建 ...
- +=与join的性能测试
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- Google测试精华文章(1) - 测试行为,而非实现
Your trusty Calculator class is one of your most popular open source projects, with many happy users ...
- J2EE开源项目
这篇文章写在我研究J2SE.J2EE近三年后.前3年我研究了J2SE的Swing.Applet.Net.RMI.Collections.IO.JNI……研究了J2EE的JDBC.Sevlet.JSP. ...
- Node.js流
什么是流? 流是可以从一个源读取或写入数据到连续的目标对象.在Node.js,有四种类型的数据流. Readable - 其是用于读操作. Writable - 用在写操作. Duplex - 其可以 ...
- unity3d中的http通信 二
转载自 http://www.cnblogs.com/88999660/archive/2013/03/11/2954279.html 如果侵权,请及时通知我删除! using System; usi ...