远程服务Services
什么时候使用远程服务:简单来说就是需要跨进程通讯的时候需要使用远程服务
qq游戏中需要使用登录或者支付功能,每个游戏都有这样的功能,使用不必要所有的游戏中的都 写一套登录或者支付的代码
这个时候就可以使用远程服务吧支付或者登录的功能提取出来
其他的应用需要使用这个功能的时候就可以绑定到远程服务
android采用了远程过程调用的方式来实现:远程过程调用:Remote Procedure Call简称PRC
通过网络从远程计算机程序上请求服务
android采用了一种接口定义语言(Interface Definition Language IDL)来公开服务的接口
因此,也可以将这种跨进程访问的服务称为AIDL android Interface Definition Language
代码是QQ登录与微信登录:
这是QQ的
package zking.com.android_24_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 QQLoginInterface qqLoginInterface;
@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","绑定成功");
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();
}
}
}
package zking.com.android_24_myqq;
/**
* Created by Administrator on 2017/7/15 0015.
*/
public interface QQLogin {
public boolean login(String number, String pass);
}
package zking.com.android_24_myqq;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;
/**
* 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();
}
}
还需要在java中new一个AIDL
// QQLoginInterface.aidl
package zking.com.android_24_myqq;
// Declare any non-default types here with import statements
interface QQLoginInterface {
boolean login(String number, String pass);
}
配置:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="zking.com.android_24_myqq">
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"
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=".QQLoginService"
android:enabled="true"
android:exported="true"
></service>
</application>
</manifest>
微信就直接调用了
需要把QQ生成的那个QQLoginInterface给创建一个main包下面放进去
package zking.com.android_24_weixin;
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 zking.com.android_24_myqq.QQLoginInterface;
public class MainActivity extends AppCompatActivity {
private EditText et_main_number;
private EditText et_main_pass;
private Intent intent;
private QQLoginInterface qqLoginInterface;
@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();
ComponentName componentName=new ComponentName("zking.com.android_24_myqq","zking.com.android_24_myqq.QQLoginService");
intent.setComponent(componentName);
}
ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
qqLoginInterface = QQLoginInterface.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@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();
try {
boolean b=qqLoginInterface.login(number,pass);
if(b){
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
然后就可以了。
谢谢