Android service的开启和绑定,以及调用service的方法
界面:
<?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的方法的更多相关文章
- [android] 代码注册广播接收者&利用广播调用服务的方法
利用广播调用服务里面的方法,间接的方式调用服务内部的方法,与现实中差不多,请媒体曝光 主界面里面 在界面创建的时候开启一下服务普通的startService()方法 发送一条广播出去 获取Intent ...
- Android(java)学习笔记228:服务(service)之绑定服务调用服务里面的方法
1.绑定服务调用服务里面的方法,图解: 步骤: (1)在Activity代码里面绑定 bindService(),以bind的方式开启服务 : bindServ ...
- Android(java)学习笔记171:服务(service)之绑定服务调用服务里面的方法
1.绑定服务调用服务里面的方法,图解: 步骤: (1)在Activity代码里面绑定 bindService(),以bind的方式开启服务 : bindServ ...
- Android--绑定服务调用服务的方法
Service依照其启动的方式,可分为两种: 1.Started Started的Service.通过在Application里用startService(Intent intent)方法来启动.这样 ...
- Android -- service的开启方式, start开启和绑定开启服务,调用服务的的方法, aidl调用远程服务
1. 概述 bindService() 绑定服务 可以得到服务的代理人对象,间接调用服务里面的方法. 绑定服务: 间接调用服务里面的方法. 如果调用者activity被销毁了, ...
- Android(java)学习笔记229:服务(service)之绑定服务调用服务里面的方法 (采用接口隐藏代码内部实现)
1.接口 接口可以隐藏代码内部的细节,只暴露程序员想暴露的方法 2.利用上面的思想优化之前的案例:服务(service)之绑定服务调用服务里面的方法,如下: (1)这里MainActivity.jav ...
- Android(java)学习笔记172:服务(service)之绑定服务调用服务里面的方法 (采用接口隐藏代码内部实现)
1. 接口 接口可以隐藏代码内部的细节,只暴露程序员想暴露的方法 2. 利用上面的思想优化之前的案例:服务(service)之绑定服务调用服务里面的方法,如下: (1)这里MainActivity.j ...
- Android -- Service的开启关闭与生命周期
Service是Android 系统中的四大组件之一,是在一段不定的时间运行在后台,不和用户交互应用组件. service可以在很多场合的应用中使用,比如播放多媒体的时候用户启动了其他Activity ...
- 关于Android中的四大组件(Service的开启与关闭)
前言 服务(Service)是Android系统中的四大组件之中的一个.服务主要用于两个目的:后台执行和跨进程訪问. 通过启动 一个服务.能够在不显示界面的前提下在后台执行指定的任务,这样能够不影响用 ...
随机推荐
- 一款类似塔防类的保卫羊村游戏android源码
一款类似塔防类的保卫羊村游戏源码,这个游戏很像我们平时玩的塔防游戏的,游戏的源码比较完整的,大家可以修改一下或者添加一些广告就可以上线到应用商店了,而且这个游戏目前已经上线国内的一些应用商店了,360 ...
- 《shell脚本if..then..elif..then.if语句的总结》
第一种: #!/bin/bash service vsftpd start &> /dev/null if [ $? -eq 0 ] then echo "ftp is sta ...
- Java中提供的工具类
System.arraycopy介绍 (1).System.arraycopy用于拷贝数组 arraycopy(Object src, int srcPos, Object dest, int des ...
- HTML5-Video & Audio
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- js----深入理解闭包
闭包算是js里面比较不容易理解的点,尤其是对于没有编程基础的人来说. 其实闭包要注意的就那么几条,如果你都明白了那么征服它并不是什么难事儿.下面就让我们来谈一谈闭包的一些基本原理. 闭包的概念 一个闭 ...
- thymeleaf 局部变量、属性优先级、注释
九.局部变量(local variable) 之前在th:each中遇到过局部变量 <tr th:each="prod : ${prods}"> ... </tr ...
- linux中nodejs后台运行工具forever
forever让nodejs应用后台执行 命令如下: forever start './bin/www' nodejs一般是当成一条用户命令执行的,当用户断开客户连接,运用也就停了,很烦人.如何让no ...
- jQuery基础选择器
attr()方法的功能是设置或获取元素的某项属性值. attr("disabled", "true”)表示使该功能不可用. #id 选择器 $("#my_id& ...
- mac os使用homebrew来管理后台服务
在linux下我们经常通过 service 或者 /etc/init.d/来管理我们的后台服务软件,并使用包管理器安装这些软件. 在mac下有homebrew这个好用的工具来安装软件,但是一直没有找到 ...
- N个顶点构成多边形的面积
Input 输入数据包含多个测试实例,每个测试实例占一行,每行的开始是一个整数n(3<=n<=100),它表示多边形的边数(当然也是顶点数),然后是按照逆时针顺序给出的n个顶点的坐标(x1 ...