Android服务Services

今天有时间来分享一个android中的Services(服务)写一个类似可以用QQ号登录微信的功能

什么是服务:
Service 是一个可以在后台执行长时间运行操作而不提供用户界面的应用组件。服务可由其他应用组件启动,而且即使用户切换到其他应用,服务仍将在后台继续运行。 此外,组件可以绑定到服务,以与之进行交互,甚至是执行进程间通信 (IPC)。 例如,服务可以处理网络事务、播放音乐,执行文件 I/O 或与内容提供程序交互,而所有这一切均可在后台进行。
服务基本上分为两种形式:启动、绑定
官网API:https://developer.android.google.cn/guide/components/services.html

本次牵涉到进程 进程通讯
AIDL(Android 接口定义语言)与您可能使用过的其他 IDL 类似。 您可以利用它定义客户端与服务使用进程间通信 (IPC) 进行相互通信时都认可的编程接口。 在 Android 上,一个进程通常无法访问另一个进程的内存。 尽管如此,进程需要将其对象分解成操作系统能够识别的原语,并将对象编组成跨越边界的对象。 编写执行这一编组操作的代码是一项繁琐的工作,因此 Android 会使用 AIDL 来处理。
官网API:https://developer.android.google.cn/guide/components/aidl.html

不多bibi上代码吧
首先我们要兴建两个项目:
anjie_android_services2_1(QQ)
anjie_android_services2_2(微信 使用QQ的登录)

需要在anjie_android_services2_1(QQ)mainfests中配置

  <!--配置服务-->
 <service android:name=".QQLoginService"
            android:exported="true"
            ></service>

客户端 anjie_android_services2_2(微信 使用QQ的登录)
需要将服务器中AIDL自动生成的接口类以及包copy导客户端项目的Java包中(需要重新编译),开始在Activity中onREsume()方法绑定服务,
方法bindService(intent,serviceConnection, Service.BIND_AUTO_CREATE);
里面有三个参数,第一个参数:Intent(new一个Intent),intent = new Intent(this,QQLoginServices.class);
多进程间启动Services时,Android 5.0 之后,启动其他应用程序的服务,不允许使用隐式。
ComponentName 有两个参数,第一个是放置包名(你所需要的服务在哪个项目),第二个是放置服务类名(你所需要的服务在哪个项目的服务类名)

ComponentName componentName=new ComponentName
(“cn.veryedu.g0305_android18_services”,”cn.veryedu.g0305_android18_services.MyServices”);
intent.setComponent(componentName);
第二个参数:ServiceConnection,new一个ServiceConnection,实现接口类中的两个方法。onServiceConnected() onServiceDisconnected()。
第三个参数:Service.BIND_AUTO_CREATE(一般都是用这个)。
然后在onServiceConnected()方法里面得到接口的方法:QQLoginInface.Stub.asInterface(iBinder);调用它

传递数据(IBinder),通过调用方法取得服务器的数据(传递对象是需要序列化实现远程服务)。

服务器 anjie_android_services2_1(QQ)
Services

public class QQLoginServices extends Service {
    //新建一个IBinder类继承aidl

    class MyIBinder extends QQLoginInface.Stub{

        @Override
        public boolean Login(String num, String pwd) throws RemoteException {
            if("10010".equals(num)&&"123456".equals(pwd)){
                return true;
            }
            return false;
        }
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.i("tt","onBind");
        return new MyIBinder();
    }
}

Activity(简易的登录)

public class MainActivity extends AppCompatActivity {
    private EditText main_num;
    private EditText main_pwd;
    private Intent intent;
    private QQLoginInface qqLoginInface;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        main_num = (EditText) findViewById(R.id.main_num);
        main_pwd = (EditText) findViewById(R.id.main_pwd);
        intent = new Intent();
        ComponentName componentName=new ComponentName("com.example.yang_servicesqq","com.example.yang_servicesqq.QQLoginServices");
        intent.setComponent(componentName);
    }
        ServiceConnection serviceConnection=new ServiceConnection() {




      @Override
      public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
          //连接成功
          Log.i("tt","服务绑定成功了!");
          //得到QQLoginInface.aidl
          qqLoginInface = QQLoginInface.Stub.asInterface(iBinder);
           }

      @Override
      public void onServiceDisconnected(ComponentName componentName) {

      }

  };
    @Override
    protected void onResume() {
        super.onResume();
        //绑定服务
        bindService(intent,serviceConnection, Service.BIND_AUTO_CREATE);
    }

    //登录
    public void login(View view) throws Exception {
        String num=main_num.getText().toString();
        String pwd=main_pwd.getText().toString();
        boolean f=qqLoginInface.Login(num,pwd);
        Toast.makeText(this,""+f,Toast.LENGTH_SHORT).show();
    }
}

AIDL

package com.example.yang_servicesqq;
interface QQLoginInface {
   boolean Login(String num,String pwd);
}

配置服务

<service android:name=".QQLoginServices"
            android:exported="true"
            android:enabled="true">

        </service>

客户端Activity

public class MainActivity extends AppCompatActivity {
    private EditText main_num;
    private EditText main_pwd;
    private Intent intent;
    private QQLoginInface qqLoginInface;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        main_num = (EditText) findViewById(R.id.main_num);
        main_pwd = (EditText) findViewById(R.id.main_pwd);
        intent = new Intent();
        ComponentName componentName=new ComponentName("com.example.yang_servicesqq","com.example.yang_servicesqq.QQLoginServices");
        intent.setComponent(componentName);
    }
        ServiceConnection serviceConnection=new ServiceConnection() {     @Override
      public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
          //连接成功
          Log.i("tt","服务绑定成功了!");
          //得到QQLoginInface.aidl
          qqLoginInface = QQLoginInface.Stub.asInterface(iBinder);
           }

      @Override
      public void onServiceDisconnected(ComponentName componentName) {

      }

  };
    @Override
    protected void onResume() {
        super.onResume();
        //绑定服务
        bindService(intent,serviceConnection, Service.BIND_AUTO_CREATE);
    }
登录
 public void login(View view) throws Exception {
        String num=main_num.getText().toString();
        String pwd=main_pwd.getText().toString();
        boolean f=qqLoginInface.Login(num,pwd);
        Toast.makeText(this,""+f,Toast.LENGTH_SHORT).show();
    }

差不多就这样,有疑问请留言。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值