绑定服务
Ibinder实现绑定服务
功能:主要为了实现组件与服务的交互,在绑定的组件可以调用服务端的功能函数
package service;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import HttpURL.HttpUtil;
/**
* 创建绑定服务:通过继承Binder类,来为客户端提供IBinder接口(本地应用且为单进程)
*/
public class MyService extends Service {
IBinder mIBinder = new MyBinder();
public class MyBinder extends Binder {
public BinderService getBinderService(){
return BinderService.this;
}
}
@Override
public void onCreate() {
}
@Override
public IBinder onBind(final Intent intent) {
new Thread(new Runnable() {
@Override
public void run() {
HttpUtil.downloadPicture(intent.getStringExtra("address"));
}
}).start();
return mIBinder;
}
@Override
public boolean onUnbind(Intent intent) {
return true;
}
@Override
public void onRebind(Intent intent) {
}
@Override
public void onDestroy() {
}
}
public ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
BinderService.MyBinder myBinder = (BinderService.MyBinder) service;
bind_service = myBinder.getBinderService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onStart() {
super.onStart();
String address = "https://img-blog.csdn.net/20160527205804527";
Intent intent = new Intent(this, BinderService.class);
intent.putExtra("address", address);
bindService(intent, serviceConnection, BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
unbindService(serviceConnection);
}
Messenger实现绑定服务
功能:为了实现跨进程通信,让客户端与服务端实现双向通信
效果图
配置文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.client" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MessengerService"
android:process=":remote">
</service>
</application>
</manifest>
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<EditText
android:id="@+id/num1"
android:layout_width="100dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:layout_toRightOf="@id/num1"
android:textSize="20sp"
/>
<EditText
android:id="@+id/num2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/plus"/>
<TextView
android:id="@+id/equal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:layout_toRightOf="@id/num2"
android:textSize="20sp"
/>
<EditText
android:id="@+id/sum"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/equal"
android:textSize="20sp"
android:editable="false"
/>
<Button
android:id="@+id/calculate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:text="计算" />
</RelativeLayout>
服务端
package com.android.client;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
/**
* 服务端
*/
public class MessengerService extends Service {
public static final int MSG_SUM = 1;
public static final int MSG_RET = 2;
public Messenger clientMessenger;
public Messenger serverMessenger = new Messenger(new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case MSG_SUM:
clientMessenger = msg.replyTo;
Message message = Message.obtain(null,MSG_RET);
message.arg1 = msg.arg1+msg.arg2;
try {
clientMessenger.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}
break;
default:
handleMessage(msg);
}
}
});
@Override
public IBinder onBind(Intent intent) {
return serverMessenger.getBinder();
}
@Override
public boolean onUnbind(Intent intent) {
return true;
}
@Override
public void onRebind(Intent intent) {
}
}
客户端
package com.android.client;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements View.OnClickListener {
public static final int MSG_SUM = 1;
public static final int MSG_RET = 2;
public EditText num1;
public EditText num2;
public EditText sum;
public Button btn;
//客户端信使
public Messenger clientMessenger = new Messenger(new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case MSG_RET:
sum.setText(msg.arg1+"");
break;
default:
super.handleMessage(msg);
}
}
});
//服务端信使
public Messenger serverMessenger;
public ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
serverMessenger = new Messenger(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
serverMessenger = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1 = (EditText) findViewById(R.id.num1);
num2 = (EditText) findViewById(R.id.num2);
sum = (EditText) findViewById(R.id.sum);
btn = (Button) findViewById(R.id.calculate);
Intent intent = new Intent(this,MessengerService.class);
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
btn.setOnClickListener(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(serviceConnection);
}
@Override
public void onClick(View v) {
if(num1.getText().toString().equals("") ||num2.getText().toString().equals("") ){
Toast.makeText(this,"请输入数值",Toast.LENGTH_SHORT).show();
return;
}
Message message = Message.obtain(null,MSG_SUM,Integer.parseInt(num1.getText().toString()),Integer.parseInt(num2.getText().toString()));
message.replyTo = clientMessenger;
try {
serverMessenger.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
参考文章:
[1]https://developer.android.com/guide/components/bound-services.html
[2]https://developer.android.com/reference/android/app/Service.html
[3]http://xwangly.iteye.com/blog/1109424
[4]http://blog.csdn.net/lmj623565791/article/details/47017485
package service;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import HttpURL.HttpUtil;
/**
* 创建绑定服务:通过继承Binder类,来为客户端提供IBinder接口(本地应用且为单进程)
*/
public class MyService extends Service {
IBinder mIBinder = new MyBinder();
public class MyBinder extends Binder {
public MyService getBinderService(){
return MyService.this;
}
}
@Override
public void onCreate() {
}
@Override
public IBinder onBind(final Intent intent) {
new Thread(new Runnable() {
@Override
public void run() {
HttpUtil.downloadPicture(intent.getStringExtra("address"));
}
}).start();
return mIBinder;
}
@Override
public boolean onUnbind(Intent intent) {
return true;
}
@Override
public void onRebind(Intent intent) {
}
@Override
public void onDestroy() {
}
}