界面:

<?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. no permissions fastboot

    no permissions  fastboot 获取fastboot文件 1.编译后得android源码会在目录: andsource2/out/host/linux-x86/bin 产生fastb ...

  2. 重写DataGridViewColumn

    做个项目需要用到DataGridView,这个控件还是挺好用的,但是今天却发现无法实现自己想要的功能.主要是DataGridViewCheckBoxColumn这个列虽然是提供了复选框,但是却未能在复 ...

  3. 【转】理解JavaScript之闭包

    闭包(closure)是掌握Javascript从人门到深入一个非常重要的门槛,它是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现.下面写下我的学习笔记~ 闭包-无处不 ...

  4. AngularJS(16)-路由

    AngularJS 路由 本章节我们将为大家介绍 AngularJS 路由. AngularJS 路由允许我们通过不同的 URL 访问不同的内容. 通过 AngularJS 可以实现多视图的单页Web ...

  5. RHEL7 Ansible

    [root@promote tt]# rpm -iUvh http://dl.Fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-8.noarch ...

  6. mongodb学习之路1

    第一节 MongoDB介绍及下载与安装 引言 MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.他支持的数据结构非常松散,是类似 json的b ...

  7. PHP CodeIgniter(CI)去掉 index.php

    去掉CodeIgniter(CI)默认url中的index.php的步骤: 1.打开apache的配置文件,conf/httpd.conf : LoadModule rewrite_module mo ...

  8. Python LOGGING使用方法

    Python LOGGING使用方法 1. 简介 使用场景 场景 适合使用的方法 在终端输出程序或脚本的使用方法 print 报告一个事件的发生(例如状态的修改) logging.info()或log ...

  9. C# 生成简单验证码

    网站登录总是会用到验证码,生成验证码对于C#来说很简单.因为有专门封装好的GDI+类可以直接调用使用具体代码如下 using System; using System.Collections.Gene ...

  10. oracle中的loop与while循环

    Oracle中loop语句会先执行一次循环,然后再判断“exit when”关键字后面的条件表达式的值是true还是false,如果是true,那么将退出循环,否则继续循环. LOOP循环 语法如下l ...