设为首页 加入收藏

TOP

Bound Service的三种方式(Binder、 Messenger、 AIDL)(一)
2015-07-20 17:28:22 来源: 作者: 【 】 浏览:14
Tags:Bound Service 方式 Binder Messenger AIDL

?

首先要明白需要的情景,然后对三种方式进行选择:

(一)可以接收Service的信息(获取Service中的方法),但不可以给Service发送信息

package com.example.boundservice;

import java.util.Random;

import android.R.integer;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class LocalService extends Service {
	
	private IBinder myBinder = new MyBinder();

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return myBinder;
	}
	public class MyBinder extends Binder {
		 LocalService getService() {
	            return LocalService.this;
	        }
	}
	public int getRandomNumber() {
		return new Random().nextInt(100);
	}

}
package com.example.boundservice;

import com.example.boundservice.LocalService.MyBinder;

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.view.View;
import android.widget.TextView;

public class BindingActivity extends Activity implements ServiceConnection{
	private LocalService localService;
	private MyBinder myBinder;
	boolean mBound = false;
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Intent intent = new Intent(this,LocalService.class);
		bindService(intent, this, Context.BIND_AUTO_CREATE);
	}
	
	@Override
	protected void onStop() {
        super.onStop(); 
        // Unbind from the service
	    if (mBound) {
	    	unbindService(this);
	        mBound = false;
	    }
    }
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_binding_activity);
		findViewById(R.id.button_show_binding_text).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				((TextView)findViewById(R.id.textview)).setText(+localService.getRandomNumber());
			}
		});
	}
	
	@Override
	public void onServiceConnected(ComponentName arg0, IBinder arg1) {
		myBinder = (MyBinder)arg1;
		localService = myBinder.getService();
		mBound = true;
	}
	
	@Override
	public void onServiceDisconnected(ComponentName arg0) {
		 mBound = false;
	}
}


(二) 使用Messenger既可以接受Service消息,也可以发送Service消息。但是无法调用Service中的方法。因为利用Message,所以不用担心并发

?

Extending the Binder class
If your service is private to your own application and runs in the same process as the client (which is common), you should create your interface by extending the Binder class and returning an instance of it from onBind(). The client receives the Binder and can use it to directly access public methods available in either the Binder implementation(得到Service对象,从而获取Service中的方法) or even the Service.
This is the preferred technique when your service is merely a background worker for your own application. The only reason you would not create your interface this way is because your service is used by other applications or across separate processes.
Service代码如下:

Activity代码如下:
?
Using a Messenger
If you need your interface to work across different processes, you can create an interface for the service with a Messenger. In this manner, the service defines a Hand
首页 上一页 1 2 3 4 5 6 下一页 尾页 1/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇ZOJ 3820 Building Fire Stations.. 下一篇POJ 3984 迷宫问题

评论

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

·About - Redis (2025-12-26 08:20:56)
·Redis: A Comprehens (2025-12-26 08:20:53)
·Redis - The Real-ti (2025-12-26 08:20:50)
·Bash 脚本教程——Li (2025-12-26 07:53:35)
·实战篇!Linux shell (2025-12-26 07:53:32)