Android -- 绑定服务

来源:互联网 发布:s7edge怎么关闭4g网络 编辑:程序博客网 时间:2024/06/10 06:22

绑定本地服务

布局文件

<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;@Overridepublic void onCreate() {context = this;super.onCreate();}@Overridepublic void onDestroy() {super.onDestroy();}@Overridepublic 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;@Overrideprotected 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() {@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {if (isChecked) {bind();} else {unbind();}}});button.setOnClickListener(new OnClickListener() {@Overridepublic 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 {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {Toast.makeText(getApplicationContext(), "绑定服务成功", 0).show();myInterface = (MyInterface) service;}@Overridepublic 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 {@Overridepublic 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;@Overrideprotected 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() {@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {if (isChecked) {bind();} else {unbind();}}});button.setOnClickListener(new OnClickListener() {@Overridepublic 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 {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {Toast.makeText(getApplicationContext(), "绑定服务成功", 0).show();myInterface = MyInterface.Stub.asInterface(service);}@Overridepublic void onServiceDisconnected(ComponentName name) {}}}































0 0
原创粉丝点击