Android Service远程调用 代码示例

Server端:

1、在AndroidManifest注册远程服务

 <service
            android:name=".NormalService"
            android:enabled="true"
            android:exported="true"
            android:label="NormalService"
            android:process=":remote" >
            <intent-filter>
                <action android:name="com.mars.NormalService" />
            </intent-filter>
        </service>

注意process要设置:remote才能运行在独立的进程

android:exported这个属性用于指示该服务是否能够被其他应用程序组件调用或跟它交互。如果设置为true,则能够被调用或交互,否则不能。设置为false时,只有同一个应用程序的组件或带有相同用户ID的应用程序才能启动或绑定该服务。


2、AIDL接口创建

</pre></p><p><span style="widows: auto;"></span><pre name="code" class="java">package com.mars.aidl;
interface IServiceAIDL {  
    int plus(int a, int b);  
    String toUpperCase(String str);  
}  


3、Service创建

package com.example.servicedemo;

import com.mars.aidl.IServiceAIDL;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class NormalService extends Service {

	private String TAG = "NormalService";
	private int count = 1;

	MyBinder mBinder = new MyBinder();

	@Override
	public IBinder onBind(Intent intent) {
		Log.d(TAG, "onBind");
		return mServiceBinder;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		int from = intent.getIntExtra("from", 0);
		Log.d(TAG, "onStartCommand count " + count++ + " from " + from);
		if (count == 10) {
			stopSelf();
			Log.d(TAG, "onStartCommand stopSelf");
		}
		return START_STICKY;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "onDestroy");
	}

	@Override
	public void onCreate() {
		super.onCreate();
		Log.d(TAG, "onCreate() executed");
		Log.e(TAG, "process id is " + android.os.Process.myPid()+" pacageName "+getApplication().getPackageName());  
	}

	@Override
	public void onRebind(Intent intent) {
		super.onRebind(intent);
		Log.d(TAG, "onRebind");
	}

	@Override
	public boolean onUnbind(Intent intent) {
		Log.d(TAG, "onUnbind");
		return super.onUnbind(intent);
	}

	class MyBinder extends Binder {
		public void startWork() {
			Log.d(TAG, "MyBinder startWork");
		}
	}
	
	IServiceAIDL.Stub mServiceBinder = new IServiceAIDL.Stub(){

		@Override
		public int plus(int a, int b) throws RemoteException {
			return a+b;
		}

		@Override
		public String toUpperCase(String str) throws RemoteException {
			if(str==null)
				return null;
			return str.toUpperCase();
		}
		
	};
}


Client端:

1、创建同包名的AIDL接口

2、调用远程Service

package com.mxw.client;

import com.mars.aidl.IServiceAIDL;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;

public class MainActivity extends Activity {


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	public void startNormalService(View v) {
	}

	public void stopNormalService(View v) {
	}

	public void goToB(View v) {
	}

	public void bindService(View v) {
		Intent intent = new Intent();
		intent.setAction("com.mars.NormalService");
		intent.setPackage("com.example.servicedemo");
        bindService(intent, connection, BIND_AUTO_CREATE);  
	}

	public void unBindService(View v) {
		 unbindService(connection); 
	}

	private IServiceAIDL myBinder;
	private ServiceConnection connection = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName name) {
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			myBinder =  IServiceAIDL.Stub.asInterface(service);
			try {
				int res = myBinder.plus(1, 2);
				String str = myBinder.toUpperCase("mxw");
				Log.e("onServiceConnected", "plus-res "+res+" toUpperCase-str "+str);
				Log.e("onServiceConnected", "plus-res "+myBinder.plus(2, 2)+" toUpperCase-str "+myBinder.toUpperCase("mddTw"));
			} catch (RemoteException e) {
				e.printStackTrace();
			}
			
		}
	};
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值