• 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));
        }
    }
  • 相关阅读:
    Transfer-Encoding: chunked
    使用Kubeadm搭建Kubernetes集群
    连载二:Oracle迁移文章大全
    今晚直播:WLS/WAS故障基本分析介绍
    判断用户是否登录
    row_number() over (partition by a.sql_id order by a.id desc ) r
    Django admin添加用户
    从数据仓库到百万标签库,精细化数据管理,这么做就够了
    用 C 语言开发一门编程语言 — 更好的语言
    ubuntu下 全然卸载火狐浏览器
  • 原文地址:https://www.cnblogs.com/juham/p/15221253.html
Copyright © 2020-2023  润新知