设为首页 加入收藏

TOP

Bound Service的三种方式(Binder、 Messenger、 AIDL)(六)
2015-07-20 17:28:22 来源: 作者: 【 】 浏览:23
Tags:Bound Service 方式 Binder Messenger AIDL
.IBinder.FIRST_CALL_TRANSACTION + 0); static final int TRANSACTION_basicTypes = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1); } /** Request the process ID of this service, to do evil things with it. */ public int getPid() throws android.os.RemoteException; /** Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, java.lang.String aString) throws android.os.RemoteException; }

?

?

AIDLService.java:

?

package com.example.boundservice;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

public class AIDLService extends Service {

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return mBinder;
	}
	private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {

		@Override
		public int getPid() throws RemoteException {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public void basicTypes(int anInt, long aLong, boolean aBoolean,
				float aFloat, double aDouble, String aString)
				throws RemoteException {
			// TODO Auto-generated method stub
			
		}
	   
	};

}

AIDLActivity.java:

?

?

package com.example.boundservice;

import com.example.boundservice.IRemoteService.Stub;

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.os.RemoteException;
import android.view.View;
import android.widget.Toast;

public class AIDLActivity extends Activity implements ServiceConnection{
	
	private Boolean mBound = false;
	private IRemoteService iRemoteServe;
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		bindService(new Intent(AIDLActivity.this,AIDLService.class), this, Context.BIND_AUTO_CREATE);
		
	}
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		if(!mBound) {
			unbindService(this);
		}
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_aidl_activity);
		findViewById(R.id.button_show_aidl_result).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				try {
					Toast.makeText(getApplicationContext(), +iRemoteServe.getPid(), Toast.LENGTH_SHORT).show();
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
	@Override
	public void onServiceConnected(ComponentName arg0, IBinder arg1) {
		iRemoteServe = Stub.asInterface(arg1);
		mBound = true;
	}

	@Override
	public void onServiceDisconnected(ComponentName arg0) {
		mBound = false;
		
	}

}
AIDLActivity Toast一个“0”,为AIDLService中getPid return 的0。

?

下面介绍如何传递对象。

Passing Objects over IPC




?

首页 上一页 3 4 5 6 下一页 尾页 6/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇ZOJ 3820 Building Fire Stations.. 下一篇POJ 3984 迷宫问题

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·微服务 Spring Boot (2025-12-26 18:20:10)
·如何调整 Redis 内存 (2025-12-26 18:20:07)
·MySQL 数据类型:从 (2025-12-26 18:20:03)
·Linux Shell脚本教程 (2025-12-26 17:51:10)
·Qt教程,Qt5编程入门 (2025-12-26 17:51:07)