Activity调用Service的方法

本文详细介绍了Android应用中Service的两种主要调用方式:进程内调用和进程间调用,特别是通过AIDL实现进程间Service的交互。文章包含示例代码和运行结果截图,帮助开发者掌握关键概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一般来说,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.youkuaiyun.com/detail/guchuanhang/9155121

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值