一般来说,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的方法的更多相关文章

  1. ssh框架,工具类调用service层方法

    解决方法: @Component//声明为spring组件 public class CopyFileUtil{ @Autowired private DataFileManager dataFile ...

  2. Android service的开启和绑定,以及调用service的方法

    界面: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android= ...

  3. 在Java filter中调用service层方法

    在项目中遇到一个问题,在 Filter中注入 Serivce失败,注入的service始终为null.如下所示: public class WeiXinFilter implements Filter ...

  4. intent 启动activity、service的方法

    1.通过intent启动service. 通过传递一个Intent对象至Context.startService()将启动一个服务(或给予正在运行的服务以一个新的指令).Android调用服务的onS ...

  5. ssh框架中,工具类调用service层方法(参考https://www.cnblogs.com/l412382979/p/8526945.html)

    代码如下: package common.dataService; import javax.annotation.PostConstruct; import org.springframework. ...

  6. android90 bind方式启动服务service调用service里的方法

    package com.itheima.banzheng; import com.itheima.banzheng.LeaderService.ZhouMi; import android.os.Bu ...

  7. Android Service 通知Activity更新界面的方法研究

    Android Service 通知Activity更新界面的方法研究   Android的最重要的组件式service和activity,那么在使用的过程中,我们最常遇到的问题是他们之间的通信问题. ...

  8. Android -- service 利用广播调用服务的方法

    1. 实现原理,在Service里面注册一个广播接收者, 想要调用的时候app发送出广播, 后台的service里面的广播接收者接收到广播,并调用service里面的方法. 2. 示例代码 MainA ...

  9. Activity调用isDestroyed()方法报出,java.lang.NoSuchMethodError

    在測试App的过程中,Activity调用了isDestroyed()方法,报出了java.lang.NoSuchMethodError错误. 自己手机MI 2S,版本号4.1.1. 事实上原因就是i ...

随机推荐

  1. Android学习笔记之详细讲解画圆角图片

    package xiaosi.RoundConcer; import android.app.Activity; import android.graphics.Bitmap; import andr ...

  2. yarn的安装和使用

    yarn的简介: Yarn是facebook发布的一款取代npm的包管理工具. yarn的特点: 速度超快. Yarn 缓存了每个下载过的包,所以再次使用时无需重复下载. 同时利用并行下载以最大化资源 ...

  3. 今天看到可以用sqlalchemy在python上访问Mysql

    from sqlalchemy import create_engine, MetaData, and_ 具体的还没有多看.

  4. vanzo-代码共享平台地址

    网页编辑.烧录代码 1.登录服务器 192.168.1.52 2.选择modules 3.选择builder 4.在 Project Name:填入要拉的项目名 选择版本:user,eng,userd ...

  5. 1.1 Introduction中 Producers官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Producers 生产者(Producers) Producers publish ...

  6. C#操作session的类实例

    本文实例讲述了C#操作session的类.分享给大家供大家参考.具体分析如下: 这个C#类对session操作进行了再次封装,可以大大简化session的常用操作,同时这个类可以将session值设置 ...

  7. Android RecyclerView嵌套RecyclerView

    原理 RecyclerView嵌套RecyclerView的条目,项目中可能会经常有这样的需求,但是我们将子条目设置为RecyclerView之后,却显示不出来.自己试了很久,终于找到了原因:必须先设 ...

  8. swiper轮播控件配置项

    var mySwiper = new Swiper ('.swiper-container', {    direction: 'horizontal',    loop: true,    auto ...

  9. BZOJ——T2190: [SDOI2008]仪仗队

    Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 3216  Solved: 2075[Submit][Status][Discuss] http://w ...

  10. SQLite基础学习

    SQLite是一款轻量级数据库,集成于android中,以下从分享一下自己学习的. 在查阅资料时有一些好的说明就直接用了: 主要的curd语句 以下SQL语句获取5条记录,跳过前面3条记录 selec ...