zoukankan      html  css  js  c++  java
  • 09-Android 中 AIDL 的理解与使用

    01-跨应用启动 Service


    02-跨应用绑定 Service:

      anotherapp:

      activity_main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    <!--    //1.4============================================-->
    <!--    <Button-->
    <!--        android:id="@+id/btnStartAppService"-->
    <!--        android:layout_width="match_parent"-->
    <!--        android:layout_height="wrap_content"-->
    <!--        android:text="启动服务" />-->
    
    <!--    <Button-->
    <!--        android:id="@+id/btnStopAppService"-->
    <!--        android:layout_width="match_parent"-->
    <!--        android:layout_height="wrap_content"-->
    <!--        android:text="停止服务" />-->
    
    <!--    //==================================================-->
    
            //2.2============================================
            <Button
                android:id="@+id/btnStartAppService"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="启动外部服务" />
    
            <Button
                android:id="@+id/btnStopAppService"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="停止外部服务" />
            <Button
                android:id="@+id/btnBindAppService"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="绑定外部服务" />
    
            <Button
                android:id="@+id/btnUnbindAppService"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="解除绑定外部服务" />
    
            //==================================================
    
    </LinearLayout>

    MainActivity.java:

    package com.imooc.anotherapp;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.view.View;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection {
    
        private Intent serviceIntent;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //1.8=======================================================
            serviceIntent = new Intent();
            serviceIntent.setComponent(new ComponentName("com.imooc.startservicefromanotherapp","com.imooc.startservicefromanotherapp.AppService"));
    
            //1.5==============================================================
            findViewById(R.id.btnStartAppService).setOnClickListener(this);
            findViewById(R.id.btnStartAppService).setOnClickListener(this);
            //2.3=========
            findViewById(R.id.btnBindAppService).setOnClickListener(this);
            findViewById(R.id.btnUnbindAppService).setOnClickListener(this);
            //=================================================================
        }
    
        @Override
        public void onClick(View v) {
            //1.6============================================================
            switch (v.getId()){
                case R.id.btnStartAppService:
                   //1.7通过报名来启动app
                    Intent i = new Intent();
                    //通过setComponent来启动,第一个参数是包名,第二个参数是包名+类名
                    //1.9=========================================
                    startService(serviceIntent);
                    break;
                case R.id.btnStopAppService:
                    stopService(serviceIntent);
                    break;
                //2.4===========
                case R.id.btnBindAppService:
                    bindService(serviceIntent,this, Context.BIND_AUTO_CREATE);
                    break;
                case R.id.btnUnbindAppService:
                    unbindService(this);
                    break;
    
            }
        }
    
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //2.5==============
            System.out.println("Bind Service");
            System.out.println(service);
        }
    
        @Override
        public void onServiceDisconnected(ComponentName name) {
    
        }
    }

      app:

        AppService.java:

    package com.imooc.startservicefromanotherapp;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.os.RemoteException;
    
    public class AppService extends Service {
        public AppService() {
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            //2.1 build---rebuild project
            return new IAppServiceRemoteBinder.Stub() {
                @Override
                public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
    
                }
            };
        }
    
        //1.10===========
    
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            return super.onStartCommand(intent, flags, startId);
        }
    
        //1.1===================================================
        @Override
        public void onCreate() {
            super.onCreate();
    
            System.out.println("Service started");
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
    
            System.out.println("Service destory");
        }
        //===================================================
    }
    
    
    ///通过其他app启动StarServiceFromAnother这app

    MainActivity.java:

    package com.imooc.startservicefromanotherapp;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Intent;
    import android.os.Bundle;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        //1.2
            startService(new Intent(this,AppService.class));
    
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
        //1.3
            stopService(new Intent(this,AppService.class));
        }
    }
  • 相关阅读:
    达梦数据库学习(二、管理数据库实例)
    达梦数据库学习(一、linux操作系统安装及数据库安装)
    SQL Server 数据库还原进度查看
    关于索引的学习(主要是聚集索引与非聚集索引)
    SQL Server批量向表中插入多行数据语句
    最大流-前置push-relabel算法实现
    调度算法(二)
    调度算法(一)
    软件工程:提问回顾
    软件工程:个人阅读作业与总结
  • 原文地址:https://www.cnblogs.com/juham/p/15221253.html
Copyright © 2011-2022 走看看