Android -- 绑定服务

绑定本地服务

布局文件

<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" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:text="绑定服务" />
    <ToggleButton
        android:id="@+id/toggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:textOff="开启服务"
        android:textOn="关闭服务" />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toggleButton"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:text="开启服务中的方法" />
</RelativeLayout>
服务
package com.example.localbinde;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;

public class MyLocalService extends Service {
	Context context;

	@Override
	public void onCreate() {
		context = this;
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public IBinder onBind(Intent intent) {
		return new MyBinder();
	}

	class MyBinder extends Binder implements MyInterface {
		public void method() {
			System.out.println("");
			Toast.makeText(context, "服务里的方法", 0).show();
		}
	}
}
服务提供的方法接口
package com.example.localbinde;

public interface MyInterface {
	void method();
}
绑定服务的页面
package com.example.localbinde;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends ActionBarActivity {

	ToggleButton toggleButton;
	Button button;

	Intent intent;
	MyConn myConn;
	MyInterface myInterface;

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

		toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
		button = (Button) findViewById(R.id.button);

		toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					bind();
				} else {
					unbind();
				}
			}
		});
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (myConn != null) {
					myInterface.method();
				} else {
					Toast.makeText(getApplicationContext(), "服务还未绑定", 0).show();
				}
			}
		});
		intent = new Intent(this, MyLocalService.class);
	}

	private void bind() {
		myConn = new MyConn();
		bindService(intent, myConn, Service.BIND_AUTO_CREATE);
	}
	private void unbind() {
		unbindService(myConn);
		myConn = null;
		Toast.makeText(getApplicationContext(), "解除绑定服务", 0).show();
	}

	class MyConn implements ServiceConnection {
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			Toast.makeText(getApplicationContext(), "绑定服务成功", 0).show();
			myInterface = (MyInterface) service;
		}
		@Override
		public void onServiceDisconnected(ComponentName name) {
		}
	}

}

-------------------------------------------------------------------------------------------------------------------------------------------------

绑定远程服务AIDL

  1. 参考绑定本地服务的流程
  2. 远程服务应用利用隐式意图激活 配置action。
  3. 远程服务的接口文件 .java ---> .aidl文件 删除public 访问修饰符
  4. MyBinder extends IAliPay.Stub
  5. 复制aidl文件到调用远程服务的项目中,注意包名跟远程服务中的保持一致
  6. 本地服务获取到接口对象后,IAliPay.Stub.asInterface(service);得到远程服务的代理对象

远程服务
package com.example.remoteService;

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

public class MyRemoteService extends Service {
	@Override
	public IBinder onBind(Intent intent) {
		return new MyBinder();
	}

	class MyBinder extends MyInterface.Stub {
		public boolean method(int num) {
			System.out.println("服务里的方法");
			if (num == 0) {
				return true;
			} else {
				return false;
			}
		}
	}
}

aidl文件
package com.example.remoteService;

 interface MyInterface {
	boolean method(int num);
}

本地:
布局文件:与绑定本地服务一样
绑定服务页面
package com.example.callremoteservice;

import android.app.Service;
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.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.example.remoteService.MyInterface;

public class MainActivity extends ActionBarActivity {
	ToggleButton toggleButton;
	Button button;

	Intent intent;
	MyConn myConn;
	MyInterface myInterface;

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

		toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
		button = (Button) findViewById(R.id.button);

		toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					bind();
				} else {
					unbind();
				}
			}
		});
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (myConn != null) {
					try {
						boolean result = myInterface.method(1);
						if (result) {
							System.out.println("result =" + result);
						} else {
							System.out.println("result =" + result);
						}
					} catch (RemoteException e) {
						e.printStackTrace();
					}
				} else {
					Toast.makeText(getApplicationContext(), "服务还未绑定", 0).show();
				}
			}
		});
		intent = new Intent("com.example.my.remeto.service");
	}

	private void bind() {
		myConn = new MyConn();
		bindService(intent, myConn, Service.BIND_AUTO_CREATE);
	}

	private void unbind() {
		unbindService(myConn);
		myConn = null;
		Toast.makeText(getApplicationContext(), "解除绑定服务", 0).show();
	}

	class MyConn implements ServiceConnection {
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			Toast.makeText(getApplicationContext(), "绑定服务成功", 0).show();
			myInterface = MyInterface.Stub.asInterface(service);
		}
		@Override
		public void onServiceDisconnected(ComponentName name) {
		}
	}
}































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值