Activity 调用Service的方法
一般来说,Activity调用Service 分为两种:进程内调用和进程间调用。进程内调用时比较常用的一种,在进程内调用中我们常常使用的是bindService来启动Service(关于这种启动方式的好处,才疏学浅就不再这卖弄了)。下面就这两种调用方式分别进行简单介绍:
1.通过bindService来启动Service,并调用Service中的方法。
1.一个简单的Service:
package gu.chuan.hang; import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder; public class MyService extends Service { @Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new MyBinder();
} public class MyBinder extends Binder{
public MyService getMyService(){
return MyService.this;
}
}
/**
* 在Service中定义这个方法,用于测试
* @return
*/
public String getAuthorName(){
return "guchuanhang";
}
}
2.在Activity中绑定Service并测试效果:
package gu.chuan.hang; import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent myServiceIntent = new Intent(MainActivity.this, MyService.class);
bindService(myServiceIntent, mServiceConnection,
Context.BIND_AUTO_CREATE);
} ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MyService myService = ((MyService.MyBinder) service).getMyService();
String authorName = myService.getAuthorName();
Toast.makeText(MainActivity.this, "author name is: " + authorName,
Toast.LENGTH_LONG).show();
} @Override
public void onServiceDisconnected(ComponentName name) {
} };
}
3.截个图吧,更清楚一点:
2.进程间启动Service并调用Service中的方法
1.首先定义一个aidl文件(扩展名为.aidl):
package gu.chuan.hang.remoteservice.aidl;
interface AuthorInfo{
String getAuthorName();
}
2.定义一个实现了aidl的服务:
package gu.chuan.hang.remoteservice.aidl; import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException; public class MyService extends Service{ @Override
public IBinder onBind(Intent intent) {
return new MyStub();
}
private class MyStub extends AuthorInfo.Stub{ @Override
public String getAuthorName() throws RemoteException {
return "guchuanhang";
} } }
3.在同一个app中模拟进程间调用的效果,给Service设置remote属性:
<service android:name="gu.chuan.hang.remoteservice.aidl.MyService"
android:process=":remote"
android:exported="false"
>
</service>
4.启动并调用Service中的方法:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Intent intent=new Intent(this,MyService.class);
startService(intent);
bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
}
private ServiceConnection mServiceConnection=new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
AuthorInfo authorInfo=AuthorInfo.Stub.asInterface(service);
try {
String authorName=authorInfo.getAuthorName();
Toast.makeText(MainActivity.this, "author name is: "+authorName, Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
}
5.附上效果截图:
6.关于aidl进程间调用是参考了http://www.cnblogs.com/zhangdongzi/archive/2012/01/09/2317197.html
大神之作,进行的精简修改。
3.附上Demo下载地址:
http://download.csdn.net/detail/guchuanhang/9155121
Activity 调用Service的方法的更多相关文章
- ssh框架,工具类调用service层方法
解决方法: @Component//声明为spring组件 public class CopyFileUtil{ @Autowired private DataFileManager dataFile ...
- Android service的开启和绑定,以及调用service的方法
界面: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android= ...
- 在Java filter中调用service层方法
在项目中遇到一个问题,在 Filter中注入 Serivce失败,注入的service始终为null.如下所示: public class WeiXinFilter implements Filter ...
- intent 启动activity、service的方法
1.通过intent启动service. 通过传递一个Intent对象至Context.startService()将启动一个服务(或给予正在运行的服务以一个新的指令).Android调用服务的onS ...
- ssh框架中,工具类调用service层方法(参考https://www.cnblogs.com/l412382979/p/8526945.html)
代码如下: package common.dataService; import javax.annotation.PostConstruct; import org.springframework. ...
- android90 bind方式启动服务service调用service里的方法
package com.itheima.banzheng; import com.itheima.banzheng.LeaderService.ZhouMi; import android.os.Bu ...
- Android Service 通知Activity更新界面的方法研究
Android Service 通知Activity更新界面的方法研究 Android的最重要的组件式service和activity,那么在使用的过程中,我们最常遇到的问题是他们之间的通信问题. ...
- Android -- service 利用广播调用服务的方法
1. 实现原理,在Service里面注册一个广播接收者, 想要调用的时候app发送出广播, 后台的service里面的广播接收者接收到广播,并调用service里面的方法. 2. 示例代码 MainA ...
- Activity调用isDestroyed()方法报出,java.lang.NoSuchMethodError
在測试App的过程中,Activity调用了isDestroyed()方法,报出了java.lang.NoSuchMethodError错误. 自己手机MI 2S,版本号4.1.1. 事实上原因就是i ...
随机推荐
- Uniform Server
Uniform Server http://www.uniformserver.com/ https://sourceforge.net/projects/miniserver/files/ Unif ...
- 37.Intellij IDEA解决GBK乱码
转自:https://blog.csdn.net/myspacedemen/article/details/38401047 今天尝鲜装完Intellij IDEA以后,打开一个GBK编码的页面,华丽 ...
- Atcoder At Beginner Contest 068 D - Decrease (Contestant ver.)
D - Decrease (Contestant ver.) Time limit : 2sec / Memory limit : 256MB Score : 600 points Problem S ...
- Django路由分配以及模版渲染
路由上: 在网络上区分不同的电脑通过IP.端口和网卡的MAC地址等,在web框架中怎么区分不同的请求呢,就是通过 ‘url(路由)’ ,url 学名叫做全球统一资源定位符,其实就是一个网址 一个url ...
- 【hdu 1533】Going Home
[链接]http://acm.hdu.edu.cn/showproblem.php?pid=1533 [题意] 一个N*M地图上有相同数量的字符H和字符m,m代表一个 人,H代表一个房子.人到房子的花 ...
- css的三种表现形式
1.行内样式(内嵌样式):结构的内部,即写在标签内的样式:写在标签的开始部分内部,style属性当中:<标记 style="样式的属性名1:样式的属性值1:属性名2:属性值2:.... ...
- Python 极简教程(七)列表 list
由于列表过于重要,请认真看完并保证所有代码都敲过一遍. 什么是列表 列表是 Python 中最常用的数据结构,也是一种数据类型,其样式如下: li = [1, 2, 3, 'a', 'b'] 列表是一 ...
- j2ee,jsp,servlet文件下载server端
1.getOutputStream() has already been called for this response 报错的原因: 使用tomcat容器调用response.getOutputS ...
- 【CodeForces】Gargari and Bishops
依据贪心能够知道,放置的教主必须不能相互攻击到(也就是不在一条对角线上)才干够使得结果最大化. 依据观察能够得到教主相互不攻击的条件是他的坐标和互为奇偶(x + y) 之后直接暴力,处理每一个坐标对角 ...
- amazeui学习笔记--js插件(UI增强)--警告框Alert
amazeui学习笔记--js插件(UI增强)--警告框Alert 一.总结 1.警告框基本样式:用am-alert声明div容器, <div class="am-alert" ...