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 ...
随机推荐
- 当Java代码遇上抽象、重载加重写,一切都不美好了
当Java代码遇上抽象.重载加重写.一切都不美好了 前几天调程序遇上个奇怪的bug.一直没找到问题,今天最终发现问题所在了,不说了先上代码(下面代码是演示样例代码,经測试,Java不存在这问题,安卓存 ...
- Android判断App是否在前台运行
版权声明:本文为博主原创文章,未经博主允许不得转载. //当前应用是否处于前台 private boolean isForeground(Context context) { if (context ...
- 关于hive里安装mysql出现错误,如何删除指定的主机或用户?(解决Access denied)
前期博客 你可以按照我写的这篇博客去,按照hive的mysql. 1 复习ha相关 + weekend110的hive的元数据库mysql方式安装配置(完全正确配法)(CentOS版本)(包含卸载系统 ...
- python序列中是否包含某个元素
http://outofmemory.cn/code-snippet/9098/python-list-contains-with-in-not-in theList = ['a','b','c'] ...
- 16、cgminer学习之:pthread_mutex_init和pthread_cond_init
1.原理 假设有两个线程同时访问一个全局变量 n,这个全局变量的初始值等于0. Int n = 0 ; 消费者线程 A 进入临界区,访问 n,A 必须等到 n 大于 0 才能接着往下执行,如果 n= ...
- tensorflow compile
bazel build --spawn_strategy=standalone tensorflow/examples/label_image/...
- 11. Spring Boot JPA 连接数据库
转自:https://blog.csdn.net/catoop/article/details/50508397
- SQl 事务增加数据
连SQl简单地事务处理多表的情况 begin transaction declare @error int declare @QID int set @error = 0 insert into HW ...
- Android Warning not all local changes may be shown due to an error
idea使用svn出现Warning not all local changes may be shown due to an error,如下图所示: 解决方案: 1.File > Setti ...
- 芯片SA58672(功放芯片)
1::下面的中文图可能不准: 针对上面的图,数据手册中有一些参数的推导: 这个式子是电压增益的 这个式子是关于截止频率的 典型原理图: 需要电源去耦,能够提高效率. PVDD引脚处的电 ...