在Android开发过程中,Android API 已经有了startService方式,为什么还需要bindService呢?

答:是因为bindService可以实现Activity-->Service里面的方法并返回数据给Activity,可以理解 在自身APP过程中Service与Activity进行通讯(本地服务)

  bindService还可以实现另外强大的功能,需要结合AIDL,实现跨进程Service与Activity进行通讯(远程服务)

定义接口扩展,增强版Binder:

package liudeli.service1.service.inter;

public interface IStudent {

    /**
* 定义通过ID查询姓名
* @param id 传入ID
* @return 返回姓名
*/
public String queryNameByID(int id); }

Service具体代码处理:

package liudeli.service1.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log; import liudeli.service1.service.inter.IStudent; public class MyService2 extends Service { private static final String TAG = MyService2.class.getSimpleName(); @Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "我被绑定了");
return new MyBinder();
} class MyBinder extends Binder implements IStudent { /**
* 定义通过ID查询姓名
*
* @param id 传入ID
* @return 返回姓名
*/
@Override
public String queryNameByID(int id) {
String result;
switch (id) {
case 1:
result = "张三";
break;
case 2:
result = "李四";
break;
case 3:
result = "王五";
break;
default:
result = "赵二";
break;
}
return result;
}
} @Override
public boolean onUnbind(Intent intent) {
Log.d(TAG, "我被解绑了");
return super.onUnbind(intent);
}
}

MainActivity2调用测试:

  // ---------------------------------- 绑定服务

    public void bindService(View view) {
bindService(new Intent(MainActivity2.this, MyService2.class), connection, Context.BIND_AUTO_CREATE);
} public void unBindService(View view) {
unbindService(connection);
} private IStudent iStudent; /**
* 服务连接接口
*/
private ServiceConnection connection = new ServiceConnection() { /**
* 已经连接到服务
* @param name 服务到详细地址等
* @param service 服务返回过来到结果
*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d("MyService2", "已经连接到服务 name:" + name); // 返回到接口,需要强制转成具体定义到接口
iStudent = (IStudent) service;
} /**
* 断开与服务到连接
* @param name 服务到详细地址等
*/
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d("MyService2", "断开与服务到连接 name:" + name);
iStudent = null;
}
}; @Override
protected void onDestroy() {
super.onDestroy();
if (null != connection) {
unbindService(connection);
}
} public void test(View view) {
if (null != iStudent) {
String result = iStudent.queryNameByID(2);
Toast.makeText(this, result, Toast.LENGTH_LONG).show();
}
}

Android-bindService本地服务-初步的更多相关文章

  1. Android-bindService本地服务-初步-Service返回对象

    在Android开发过程中,Android API 已经有了startService方式,为什么还需要bindService呢? 答:是因为bindService可以实现Activity-->S ...

  2. android测试本地服务调试流程

    我今天调试的整个过程 1,安卓发现连不上本地的tomcat 2,使用浏览器直接尝试,发现可以连上 3,怀疑是安卓APP和浏览器访问有差异,后上网搜索不到APP,只有浏览器尝试       再不就是改I ...

  3. Android Service学习之本地服务

    Service是在一段不定的时间运行在后台,不和用户交互应用组件.每个Service必须在manifest中 通过来声明.可以通过contect.startservice和contect.bindse ...

  4. Android本地服务

    一.服务生命周期总结 (一).单独开启服务,并没有绑定服务Activity中调用startService(),服务的lifecycle:onCreate()→onStartCommand()→onSt ...

  5. LoadRunner+Android模所器实现抓包并调试本地服务端

    步骤就是 1:新建LR脚本.协议选择Mobile Application - HTTP/HTML 2:在record里选择第三个:Record Emulator........ 3:  选择下一步后, ...

  6. android service 本地 远程 总结

    android编写Service入门 android SDK提供了Service,用于类似*nix守护进程或者windows的服务. Service有两种类型: 本地服务(Local Service) ...

  7. Android学习笔记--服务(Service)

    1.服务概述 1.服务是Android四大组件之一,在使用上可以分为本地服务和远程服务,本地服务是指在不影响用户操作的情况下在后台默默的执行一个耗时操作,例如下载,音频播放等.远程服务是指可以供其他应 ...

  8. Android中的服务

    Android中的服务 四大组件都是运行在主线程 Android中的服务,是在后台运行 .可以理解成是在后台运行并且是没有界面的Activity. Foreground process 前台进程 ,用 ...

  9. Android四大组件--服务(Service)

    1. startService和bindService的区别 1. startService: 生命周期: onCreate---onStartCommand---onDestory 与服务的通讯: ...

随机推荐

  1. 关于OkHttp–支持SPDY协议的高效HTTP库 com.squareup.okhttp

    转载:http://liuzhichao.com/p/1707.html OkHttp–支持SPDY协议的高效HTTP库 柳志超博客 » Program » Andriod » OkHttp–支持SP ...

  2. UI“三重天”之appium(一)

    官方介绍: Appium is an open-source tool for automating native, mobile web, and hybrid applications on iO ...

  3. ubuntu 16.04安装ibus中文输入法

    1. 安装IBus sudo apt-get install ibus-pinyin system settings——>language support——>keyboard input ...

  4. varnish/squid/nginx cache 有什么不同?

    SQUID 是功能最全面的,但是架构太老,性能不咋的Varnish 是内存缓存,速度一流,但是内存缓存也限制了其容量,缓存页面和图片一般是挺好的Nginx 本来是反向代理/web服务器,用了插件可以做 ...

  5. 笔记本制作centos qcow2格式文件

    笔记本win7先通过vbox安装好centos6.5 然后打开cmd命令行在c:\Program Files\Oracle\VirtualBox下执行 vboxmanage clonehd --for ...

  6. Oracle NVL与NVL2函数

    nvl( ) 函数 从两个表达式返回一个非 null 值. 语法 NVL(eExpression1, eExpression2) 参数 eExpression1, eExpression2 如果 eE ...

  7. python 爬虫气象气象定时报 气象预警推送

    "2018-04-09 14时""长沙市""10日(明天)""多云转雷阵雨""29℃""1 ...

  8. fireDAC oracle

    copy 4 files to D:\oracleapp\Administrator\product\11.2.0\client_1\BIN setup win 64 bit client .down ...

  9. 当前触发事件的两种方式(onclick) 和 ('id') 获取

    1. <input type='text' onclick = 'Clickon(this)'> <script> function Clickon(num){ num.sty ...

  10. Java Graphics 2D绘制图片 在Liunx上乱码

    绘图的代码工具类 package com.gwzx.framework.captcha; import java.awt.Color; import java.awt.Font; import jav ...