服务Service之绑定服务(远程服务)
来源:互联网 发布:北京知蜂堂上海店 编辑:程序博客网 时间:2024/05/17 09:04
在上一篇已经说过启动服务了,今天给大家,讲讲绑定服务。
绑定服务
当应用组件通过调用 bindService() 绑定到服务时,服务即处于“绑定”状态。绑定服务提供了一个客户端-服务器接口,允许组件与服务进行交互、发送请求、获取结果,甚至是利用进程间通信 (IPC) 跨进程执行这些操作。 仅当与另一个应用组件绑定时,绑定服务才会运行。 多个组件可以同时绑定到该服务,但全部取消绑定后,该服务即会被销毁。
下面举个例子,通过AIDL远程调用Service
1.MainActivity.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.zking.hgz_android_23_service_qq.MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:text="admin" android:id="@+id/et_main_name" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:text="123456" android:id="@+id/et_main_pwd"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="Login" android:text="登录" /></LinearLayout>
2.MyLonginService
package com.zking.hgz_android_23_service_qq;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.os.RemoteException;import android.support.annotation.Nullable;import android.util.Log;import android.widget.Toast;import java.util.Map;/** * Created by hgz on 2017/2/12. */public class MyLonginService extends Service { class MyBind extends MyLoginInterfaceOut.Stub{ @Override public boolean Login(String name, String pwd) throws RemoteException { if("admin".equals(name)&&"123456".equals(pwd)){ return true; } return false; } @Override public User login2(Map map) throws RemoteException { String name=map.get("name").toString(); String pwd=map.get("pwd").toString(); if("admin".equals(name)&&"123456".equals(pwd)){ User user=new User("柱子",name,pwd); return user; } return null; } } @Nullable @Override public IBinder onBind(Intent intent) { Log.i("text","onBind"); return new MyBind(); }}
3.User
package com.zking.hgz_android_23_service_qq;import android.os.Parcel;import android.os.Parcelable;/** * Created by hgz on 2017/2/13. */public class User implements Parcelable{ private String uname; private String number; private String pwd; public User() { } public User(String uname, String number, String pwd) { this.uname = uname; this.number = number; this.pwd = pwd; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(uname); dest.writeString(number); dest.writeString(pwd); } public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>(){ @Override public User createFromParcel(Parcel source) { User user=new User(); user.setUname(source.readString()); user.setNumber(source.readString()); user.setPwd(source.readString()); return user; } @Override public User[] newArray(int size) { return new User[size]; } };}
4.MyLoginInterface
package com.zking.hgz_android_23_service_qq;import java.util.Map;/** * Created by hgz on 2017/2/12. */public interface MyLoginInterface { public boolean Login(String name,String pwd); public User login2(Map<String,Object> map);}
5.MyLoginInterfaceOut.aidl
// MyLoginInterfaceOut.aidlpackage com.zking.hgz_android_23_service_qq;import com.zking.hgz_android_23_service_qq.User;interface MyLoginInterfaceOut { boolean Login(String name,String pwd); User login2(in Map map);}
6.User.aidl
// UserOut.aidlpackage com.zking.hgz_android_23_service_qq;import com.zking.hgz_android_23_service_qq.User;parcelable User;
7.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zking.hgz_android_23_service_qq"> <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=".MyLonginService" android:exported="true"></service> </application></manifest>
0 0
- 服务Service之绑定服务(远程服务)
- Service之《远程服务》
- 服务(service)-----远程服务
- Service(远程服务)
- 绑定服务&绑定远程服务
- android之服务service远程服务
- android之服务service远程服务
- android之服务service远程服务
- Service之基础(开启服务、绑定本地服务)
- Android -- service之绑定服务
- Service学习之--------绑定服务
- Android---服务(Service)的绑定服务
- Android Service(服务)绑定远程服务一个小demo
- 服务之远程服务
- service(服务绑定)
- service(服务绑定)
- Service绑定本地服务
- Service绑定服务
- 使用React和node搭建任务管理平台
- 深入理解 Java 垃圾回收机制
- iOS中使用AFN的delete请求遇到的坑
- 【c++】用c++的vector容器编写简单的通讯录
- 《你好哇,程序员》—教你如何成为一名牛X的程序员
- 服务Service之绑定服务(远程服务)
- python整数与IP地址转换
- openwrt 支持fbterm
- Raspbian 修改分辨率与旋转屏幕
- 安卓注解使用,实体类指定须使用字段, Google Gson 字段过滤
- Maven高速镜像
- 如何快速转载CSDN中的博客
- 【Python】SNMP的安装及Python的调用
- 台球瞄准