我这里是模仿QQ登录的底层做法服务带对象传递
先来一个布局文件
相对应的activity
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private EditText et_main_number;
private EditText et_main_pwd;
private Intent intent;
private QQLoginService.MyIBinder myIBinder;
private QQLoginInterface qqLoginInterface;
private QQLoginInterfaceOut qqLoginInterfaceOut;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_main_number = (EditText) findViewById(R.id.et_main_number);
et_main_pwd = (EditText) findViewById(R.id.et_main_pwd);
intent = new Intent(this,QQLoginService.class);
}
//绑定服务
ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
qqLoginInterfaceOut = QQLoginInterfaceOut.Stub.asInterface(iBinder);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
@Override
protected void onResume() {
super.onResume();
//绑定服务
bindService(intent,connection, Service.BIND_AUTO_CREATE);
}
public void login(View view){
//获取用户名和密码
String number=et_main_number.getText().toString();
String pwd=et_main_pwd.getText().toString();
boolean flag= false;
try {
Map map=new HashMap();
map.put("number",number);
map.put("pwd",pwd);
QQUser qqUser=qqLoginInterfaceOut.login2(map);
Toast.makeText(MainActivity.this, " "+qqUser.getQqName()+" "+qqUser.getQqSex(), Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
e.printStackTrace();
}
//判断
}
}
import java.util.Map;
/**
* @author Zking-Snail
* @time 2017/2/12 15:54
* @Version ${REV}
*/
public interface QQLoginInterface {
public boolean login(String number,String pwd);
public QQUser login2(Map map);
}
在写一个来实现服务的类
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 java.util.Map;
/**
* @author Zking-Snail
* @Version ${REV}
*/
public class QQLoginService extends Service {
//实现QQLoginInterfaceOut接口里面的方法
class MyIBinder extends QQLoginInterfaceOut.Stub{
@Override
public boolean login(String number, String pwd) throws RemoteException {
if("10000".equals(number)&&"123456".equals(pwd)){
return true;
}
return false;
}
@Override
public QQUser login2(Map map) throws RemoteException {
String number=map.get("number").toString();
String pwd=map.get("pwd").toString();
if("10000".equals(number)&&"123456".equals(pwd)){
return qqUser;
}
return null;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i("test","onBind");
return new MyIBinder();
}
}
实体类:
import android.os.Parcel;
import android.os.Parcelable;
/**
* @author Zking-Snail
* @Version ${REV}
*/
public class QQUser implements Parcelable{
private String qqName;
private String qqPwd;
private String qqNumber;
private String qqSex;
public QQUser() {
}
public QQUser(String qqName, String qqPwd, String qqNumber, String qqSex) {
this.qqName = qqName;
this.qqPwd = qqPwd;
this.qqNumber = qqNumber;
this.qqSex = qqSex;
}
public String getQqName() {
return qqName;
}
public void setQqName(String qqName) {
this.qqName = qqName;
}
public String getQqPwd() {
return qqPwd;
}
public void setQqPwd(String qqPwd) {
this.qqPwd = qqPwd;
}
public String getQqNumber() {
return qqNumber;
}
public void setQqNumber(String qqNumber) {
this.qqNumber = qqNumber;
}
public String getQqSex() {
return qqSex;
}
public void setQqSex(String qqSex) {
this.qqSex = qqSex;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(qqName);
parcel.writeString(qqPwd);
parcel.writeString(qqNumber);
parcel.writeString(qqSex);
}
public static final Parcelable.Creator CREATOR=new Parcelable.Creator(){
@Override
public QQUser createFromParcel(Parcel parcel) {
QQUser qqUser=new QQUser();
qqUser.setQqName(parcel.readString());
qqUser.setQqPwd(parcel.readString());
qqUser.setQqNumber(parcel.readString());
qqUser.setQqSex(parcel.readString());
return qqUser;
}
@Override
public QQUser[] newArray(int i) {
return new QQUser[i];
}
};
}
清单文件里面一定要配置
从页面获取值----activity的onClick事件----调用绑定服务的方法ServiceConnection ---实现接口QQLoginInterface 里的方法--调用QQLoginService 中MyIBinder的方法
我再来模拟一个用微信登录的微信
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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:layout_height="wrap_content"
android:text=“这是用QQ登录的微信”"
android:textSize="30sp"
/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="10000" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="123456" />
一定要把QQ那边的QQLoginInterfaceOut与实体类复制过来!!
这边的activity
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.zking.g150831_android23_qq.QQLoginInterfaceOut;
import com.zking.g150831_android23_qq.QQUser;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private EditText et_main_number;
private EditText et_main_pwd;
private Intent intent;
private QQLoginInterfaceOut qqLoginInterfaceOut;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_main_number = (EditText) findViewById(R.id.et_main_number);
et_main_pwd = (EditText) findViewById(R.id.et_main_pwd);
intent = new Intent();
ComponentName componentName=new ComponentName("com.zking.g150831_android23_qq","com.zking.g150831_android23_qq.QQLoginService");
intent.setComponent(componentName);
}
ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
qqLoginInterfaceOut = QQLoginInterfaceOut.Stub.asInterface(iBinder);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
@Override
protected void onResume() {
super.onResume();
//绑定服务
bindService(intent,connection, Service.BIND_AUTO_CREATE);
}
public void login(View view){
String number=et_main_number.getText().toString();
String pwd=et_main_pwd.getText().toString();
try {
Map map=new HashMap();
map.put("number",number);
map.put("pwd",pwd);
QQUser qqUser=qqLoginInterfaceOut.login2(map);
Toast.makeText(MainActivity.this, ""+qqUser.getQqName(), Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
微信这边就用QQ那边的的方法实现模拟登录与传值