一般来说,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. shell-手机屏幕录制

    今天在做android联系的时候,想要把自己写的demo效果记录下来.在网上发现了录制手机屏幕的方法,如下 adb shell screenrecord /sdcard/demo.mp4 解释 adb ...

  2. 基于WebSphere与Domino的电子商务网站构架分析

    650) this.width=650;" border="0" alt="174812596.jpg" src="http://img1. ...

  3. 使用C#对XML进行增删改查操作

    xml文件格式 <?xml version="1.0" encoding="utf-8"?> <messageList> <mes ...

  4. POJ 3009 Curling 2.0 {深度优先搜索}

    原题 $On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules ...

  5. [Javascript] Classify text into categories with machine learning in Natural

    In this lesson, we will learn how to train a Naive Bayes classifier or a Logistic Regression classif ...

  6. HTTP请求报文、响应报文

    HTTP请求报文 HTTP请求报文由3部分组成(请求行+请求头+请求体): 请求行:①是请求方法,GET和POST是最常见的HTTP方法,除此以外还包括DELETE.HEAD.OPTIONS.PUT. ...

  7. 【例题 7-10 UVA - 11212】Editing a Book

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 迭代加深搜. 很容易想到,最多只要搜8层就可以得到答案了 ->最多8下肯定可以还原. 则枚举一下最大层数.然后做个深搜就好. ...

  8. java与javax的区别分析

    Java是一种受C语言影响的编程语言.Java和Javax本质上是与Java编程语言的上下文一起使用的包.实际上Java和Javax没有区别.这只是不同的名字. Java是一种编程语言,受到C语言的影 ...

  9. 彩票案例-frame,center和bounds属性

    控件的属性: 二.frame.center和bounds属性 " 在iOS中,每一个控件都是继承于UIView的.都会有视图的属性存在,控制这个视图的位置就有Frame和Bounds两个属性 ...

  10. HDOJ 5409 CRB and Graph 无向图缩块

    无向图缩块后,以n所在的块为根节点,dp找每块中的最大值. 对于每一个桥的答案为两块中的较小的最大值和较小的最大值加1 CRB and Graph Time Limit: 8000/4000 MS ( ...