XML代码:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MyQQ"
android:textSize="30sp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名:"
android:id="@+id/et_main_number"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码:"
android:id="@+id/et_main_pass"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:onClick="login"
/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MyQQ"
android:textSize="30sp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名:"
android:id="@+id/et_main_number"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码:"
android:id="@+id/et_main_pass"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:onClick="login"
/>
JAVA:
MainActvivty:类
package com.zking.administrator.g160628_android24_myqq;
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;
public class MainActivity extends AppCompatActivity {
private EditText et_main_number;
private EditText et_main_pass;
private Intent intent;
private QQLogin qqLogin;
private QQLoginInterface qqLoginInterface;
//private QQLoginService.MyIBinder myIBinder;
@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_pass = (EditText) findViewById(R.id.et_main_pass);
intent = new Intent(this,QQLoginService.class);
}
ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("test","绑定成功");
// myIBinder = (QQLoginService.MyIBinder) service;
//qqLogin = (QQLogin) service;
qqLoginInterface = QQLoginInterface.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("test","绑定失败");
}
};
@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 pass=et_main_pass.getText().toString();
boolean flag= false;
try {
flag = qqLoginInterface.login(number,pass);
} catch (RemoteException e) {
e.printStackTrace();
}
if(flag){
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
}
}
}
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;
public class MainActivity extends AppCompatActivity {
private EditText et_main_number;
private EditText et_main_pass;
private Intent intent;
private QQLogin qqLogin;
private QQLoginInterface qqLoginInterface;
//private QQLoginService.MyIBinder myIBinder;
@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_pass = (EditText) findViewById(R.id.et_main_pass);
intent = new Intent(this,QQLoginService.class);
}
ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("test","绑定成功");
// myIBinder = (QQLoginService.MyIBinder) service;
//qqLogin = (QQLogin) service;
qqLoginInterface = QQLoginInterface.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("test","绑定失败");
}
};
@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 pass=et_main_pass.getText().toString();
boolean flag= false;
try {
flag = qqLoginInterface.login(number,pass);
} catch (RemoteException e) {
e.printStackTrace();
}
if(flag){
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
}
}
}
QQLogin:类
package com.zking.administrator.g160628_android24_myqq;
/**
* Created by Administrator on 2017/7/15 0015.
*/
public interface QQLogin {
public boolean login(String number,String pass);
}
/**
* Created by Administrator on 2017/7/15 0015.
*/
public interface QQLogin {
public boolean login(String number,String pass);
}
QQLoginService:类
package com.zking.administrator.g160628_android24_myqq;
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;
/**
* Created by Administrator on 2017/7/15 0015.
*/
public class QQLoginService extends Service {
class MyIBinder extends QQLoginInterface.Stub{
@Override
public boolean login(String number, String pass) throws RemoteException {
if("10000".equals(number) && "123456".equals(pass)){
return true;
}
return false;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i("test","onBind");
return new MyIBinder();
}
}
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;
/**
* Created by Administrator on 2017/7/15 0015.
*/
public class QQLoginService extends Service {
class MyIBinder extends QQLoginInterface.Stub{
@Override
public boolean login(String number, String pass) throws RemoteException {
if("10000".equals(number) && "123456".equals(pass)){
return true;
}
return false;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i("test","onBind");
return new MyIBinder();
}
}