界面:

<?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. Windows phone 中一些实用的控件

    一.TextBlock:这个控件其实就是Label控件. <TextBlock x:Name="PageTitle" Text="page name" M ...

  2. 为什么要用ajax

    Ajax应用程序的优势在于:1. 通过异步模式,提升了用户体验2. 优化了浏览器和服务器之间的传输,减少不必要的数据往返,减少了带宽占用3. Ajax引擎在客户端运行,承担了一部分本来由服务器承担的工 ...

  3. js中的数组

    上网查了一下,js中的数组包含的内容还真不少.先给出两个学习的链接: w3school链接:http://www.w3school.com.cn/js/js_obj_array.asp 博客园链接:h ...

  4. Oracle计算两个整数的和与这两个整数的差与商

    PL/SQL(Procedural Language/SQL)是一种过程化语言. PL/SQL都是以(BLOCK)块为基本单位,整个PL/SQL块分为三部分 1.声明(Declare) 2.执行(以B ...

  5. Sql Server 常用自定义函数

    -- select * from [dbo].[SplitToTable]('ADSF','|') -- 分解字符串 ALTER FUNCTION [dbo].[SplitToTable] ( @Sp ...

  6. Linux内核目录

    linux目录结构 目录 1.树状目录结构图 2./目录 3./etc/目录 4./usr/目录 5./var/目录 6./proc/目录 7./dev/目录 该文章主要来自于网络进行整理. 目录结构 ...

  7. [转]宏的高级使用--##,__VA_ARGS__, __FILE__, __FUNCTION__等

    [转]宏的高级使用--##,__VA_ARGS__, __FILE__, __FUNCTION__等 http://blog.csdn.net/yiya1989/article/details/784 ...

  8. Legacy安装win7和Ubuntu14.04双系统

    Legacy安装win7和Ubuntu14.04双系统 安装环境 Legacy启动模式(传统引导) 笔记本已安装win7 硬盘启动顺序为: U盘 硬盘 光驱 安装方法 制作U盘启动盘 在Ubuntu官 ...

  9. GitHub error “Failed to get HEAD”

    cd /要提交的文件的文件夹下    比如要提交一个名为  demo的  程序, 那么先进入demo 的文件夹里面  然后  进行以下两步 git init  (有时这个是不必要的,因为xcode 自 ...

  10. Careercup - Facebook面试题 - 5998719358992384

    2014-05-02 00:22 题目链接 原题: Given a matrix consisting of 's. 题目:给定一个01矩阵,找出由1构成的连通分量中最大的一个. 解法:四邻接还是八邻 ...