界面:

<?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. 压缩html 减小存储空间

    压缩html 减小存储空间 方法一.php代码,清除换行符,清除制表符,去掉注释标记 /** * 压缩html : 清除换行符,清除制表符,去掉注释标记 * @param $string * @ret ...

  2. centos中安装chromium和flash

    安装环境:centos 6.5 64位 在centos中安装chromium 安装Google源 cd /etc/yum.repos.d/ sudo wget http://people.CentOS ...

  3. Linux性能监控top及vmstat命令

    监控的工具---top 第一行: 03:07:27 当前系统时间 3 days, 18:58 系统已经运行了3天18小时58分钟(在这期间没有重启过) 4 users load average: 0. ...

  4. Python-Day7 面向对象进阶/异常处理/Socket

    一.面向对象高级语法部分 1.静态方法     通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法,什么是静态方法呢?其实不难理解,普通的方法,可以在实例化后直接调用,并且在方法里 ...

  5. SQLserver Delete from where 与Oracle delete from where 的差异

    1.SQLserver 版本: select @@version; Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) Dec 28 2012 20 ...

  6. .NET书籍推荐

    任何语言的学习,要快速掌握,不在看书,而在实践.——题记 .NET技术从1.1发展到2.0,内核基本完善,从.NET 2.0开始学习是个明智的选择.而NET 3.5以及即将推出的.NET 4.0所新加 ...

  7. ALTER TABLE causes auto_increment resulting key 'PRIMARY'

    修改表为主键的自动增长值时,报出以下错误:mysql> ALTER TABLE YOON CHANGE COLUMN id id INT(11) NOT NULL AUTO_INCREMENT ...

  8. ASP.NET MVC NonActionAttribute使用说明

    默认情况下,MVC 框架将 controller 类的所有公共方法都视为操作方法. 如果您的 controller 类包含公共方法,并且您不希望它成为操作方法,则必须用 NonActionAttrib ...

  9. return的用法

    1.一般的就是用在有返回值的方法中,用来返回方法指定类型的值,同时结束方法执行: 2.可以用在返回值为void的方法中,用来终止方法运行:

  10. 一个简单的脚本让你的ubuntu14.04记忆屏幕亮度

    小弟献丑,发个没技术含量的.本人电脑是联想y400,无论是安装32位的ubuntu还是64位的ubuntu,无论是13.04还是现在的 14.04开发者版本,都无法让我的小y实现记忆屏幕亮度这个简单的 ...