界面:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="开启服务"
android:onClick="start"
/> <Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="关闭服务"
android:onClick="close"
/> <Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="绑定服务"
android:onClick="bind"
/> <Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消绑定服务"
android:onClick="unbind"
/> <Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="调用服务的方法function()"
android:onClick="callFunction"
/>
</LinearLayout>

Activity

package com.example.serviceTest;

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 MyActivity extends Activity { private IService myBinder;
private ServiceConnection myConn = new MyConn(); @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
} public void start(View view) {
startService(new Intent(this, TestService.class));
} /**
* 多次调用停止服务,没有出现问题(不能多次解绑)
*/
public void close(View view) {
stopService(new Intent(this, TestService.class));
} public void bind(View view) {
Intent intent = new Intent(this, TestService.class);
bindService(intent, myConn, BIND_AUTO_CREATE);
} /**
* 多次解绑服务会报出错误
*/
public void unbind(View view) {
unbindService(myConn);
} public void callFunction(View view) {
if (myBinder != null) {
myBinder.callFunction();
}
} //绑定的时候,回调的一些方法
class MyConn implements ServiceConnection { @Override
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("获取到binder");
myBinder = (IService) service;
} @Override
public void onServiceDisconnected(ComponentName name) {
System.out.println("com.example.serviceTest.MyActivity.MyConn.onServiceDisconnected");
}
}
}

Service

package com.example.serviceTest;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast; /**
* Created by Heyiyong on 14-5-16.
*/
public class TestService extends Service { public IBinder onBind(Intent intent) {
System.out.println("服务被绑定了!");
return new MyBinder();
} //中间人(service的代理)
private class MyBinder extends Binder implements IService{
public void callFunction() {
function();
}
} @Override
public void onCreate() {
System.out.println("com.example.serviceTest.TestService.onCreate");
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("com.example.serviceTest.TestService.onStartCommand");
return super.onStartCommand(intent, flags, startId);
} @Override
public void onDestroy() {
System.out.println("com.example.serviceTest.TestService.onDestroy");
} @Override
public boolean onUnbind(Intent intent) {
System.out.println("com.example.serviceTest.TestService.onUnbind");
return super.onUnbind(intent);
} public void function() {
Toast.makeText(getApplicationContext(), "function()方法被调用了!", 1).show();
System.out.println("com.example.serviceTest.TestService.function");
}
}

Android service的开启和绑定,以及调用service的方法的更多相关文章

  1. [android] 代码注册广播接收者&利用广播调用服务的方法

    利用广播调用服务里面的方法,间接的方式调用服务内部的方法,与现实中差不多,请媒体曝光 主界面里面 在界面创建的时候开启一下服务普通的startService()方法 发送一条广播出去 获取Intent ...

  2. Android(java)学习笔记228:服务(service)之绑定服务调用服务里面的方法

    1.绑定服务调用服务里面的方法,图解: 步骤: (1)在Activity代码里面绑定 bindService(),以bind的方式开启服务 :                     bindServ ...

  3. Android(java)学习笔记171:服务(service)之绑定服务调用服务里面的方法

    1.绑定服务调用服务里面的方法,图解: 步骤: (1)在Activity代码里面绑定 bindService(),以bind的方式开启服务 :                     bindServ ...

  4. Android--绑定服务调用服务的方法

    Service依照其启动的方式,可分为两种: 1.Started Started的Service.通过在Application里用startService(Intent intent)方法来启动.这样 ...

  5. Android -- service的开启方式, start开启和绑定开启服务,调用服务的的方法, aidl调用远程服务

    1. 概述 bindService() 绑定服务  可以得到服务的代理人对象,间接调用服务里面的方法. 绑定服务: 间接调用服务里面的方法.           如果调用者activity被销毁了, ...

  6. Android(java)学习笔记229:服务(service)之绑定服务调用服务里面的方法 (采用接口隐藏代码内部实现)

    1.接口 接口可以隐藏代码内部的细节,只暴露程序员想暴露的方法 2.利用上面的思想优化之前的案例:服务(service)之绑定服务调用服务里面的方法,如下: (1)这里MainActivity.jav ...

  7. Android(java)学习笔记172:服务(service)之绑定服务调用服务里面的方法 (采用接口隐藏代码内部实现)

    1. 接口 接口可以隐藏代码内部的细节,只暴露程序员想暴露的方法 2. 利用上面的思想优化之前的案例:服务(service)之绑定服务调用服务里面的方法,如下: (1)这里MainActivity.j ...

  8. Android -- Service的开启关闭与生命周期

    Service是Android 系统中的四大组件之一,是在一段不定的时间运行在后台,不和用户交互应用组件. service可以在很多场合的应用中使用,比如播放多媒体的时候用户启动了其他Activity ...

  9. 关于Android中的四大组件(Service的开启与关闭)

    前言 服务(Service)是Android系统中的四大组件之中的一个.服务主要用于两个目的:后台执行和跨进程訪问. 通过启动 一个服务.能够在不显示界面的前提下在后台执行指定的任务,这样能够不影响用 ...

随机推荐

  1. jQuery遮罩层的实现

    遮罩层其实就是一个占据整个页面的半透明效果的页面元素,一般用div实现.页面中实现遮罩层,无非就是为了让用户只能操作弹出窗口的内容,而不允许操作弹出窗口外的内容. 在实现时,我使用了两个div,一个遮 ...

  2. C#inSSIDer强大的wifi无线热点信号扫描器源码

    一个完整的无线信号扫描工具源码,包含了从热点扫描到强度绘制以及信号变换曲线图.源码基于Managed Wifi实现基础功能,Managed Wifi也是开源项目,这个可以在本站搜索到. 指定网卡信号扫 ...

  3. LINQ技巧:如何通过多次调用GroupBy实现分组嵌套

    问题如上,解决如下,目标在最下面:结果: using System; using System.Linq; using System.Collections.Generic; namespace Co ...

  4. 修改Win7远程桌面端口

    Win7与XP不同,在开启远程桌面修改端口后是无法直接访问的,原因是还未修改远程桌面在防火墙入站规则中的端口号. 修改远程桌面端口: [HKEY_LOCAL_MACHINE/SYSTEM/Curren ...

  5. 笨办法学 Python (第三版)(转载)

    笨办法学 Python (第三版) 原文地址:http://blog.sina.com.cn/s/blog_72b8298001019xg8.html   摘自https://learn-python ...

  6. 失败经历--在windows下安装meld

    缘起 在linux下,最早用的比较工具是vim,这是作为一个vimer的自尊(其实没有关系吧).终于有一天,在比较同一个项目的两个版本的时候,比较了两三个文件后,看着vim里面花花绿绿的颜色,实在是受 ...

  7. C# 链接Sql和Access数据库语句

    1.sql数据库: 1.1.链接数据语句:server=localhost;database=Data; uid=sa;pwd=123; 或 Data Source=localhost;DataBas ...

  8. C#——中文转化成拼音

    在KS系统中用到了中文转化成拼音的功能.通过查阅资料为下面是代码. /// <summary> /// MyConvert 的摘要说明 /// </summary> publi ...

  9. IO流的异常处理

    在IO流的异常处理时应该注意以下几点: 1.在外边建立引用,在Try内进行初始化(FileWriter fw = null;) 2.文件的路径使用必须是双斜杠,转义(fw = new FileWrit ...

  10. AJAX如何接收JSON数据

    简介 在我们了解如何使用AJAX返回JSON数据的时候要先明白下列几点 1. JSON如何来表示对象的 2. JSON如何来表示数组的 var object = { "labId" ...