Android之Service

今天博主就为大家讲解一下,两个应用程序实现数据传递。来写一下QQ与微信共用一个登录。简单来说,我们把QQ当做一个服务器,微信就是客户端。

首先先来个布局。




然后Java类

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.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();
    }
}

接口:


/**
 * Created by Administrator on 2017/7/15 0015.
 */

public interface QQLogin {
    public boolean login(String number,String pass);
}

就这样服务器就写好了,接下来就是客户端,布局都一样直接上代码

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;


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("com.zking.administrator.g160628_android24_myqq","com.zking.administrator.g160628_android24_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();
        }
    }
}

要引用几个AIDL,就可以了


你可能感兴趣的:(Android之Service)