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


    跨应用启动 Service:

    app:

    AppService.java:

    package com.example.startservicefromanotherapp;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    
    public class AppService extends Service {
        public AppService() {
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO: Return the communication channel to the service.
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
        //1.1-----------------------------------------
        @Override
        public void onCreate() {
            super.onCreate();
    
            System.out.println("Service Started");
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
    
            System.out.println("Service destory");
        }
        //--------------------------------------------------
    }

    MainActivity.java:

    package com.example.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();
    
            stopService(new Intent(this,AppService.class));
        }
        //------------------------------------------------------------
    }

    anotherapp:


    mainActivity.java:

    package com.example.anotherapp;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.ComponentName;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
        //1
        private Intent serviceIntent;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //1.5
            serviceIntent = new Intent();
            //被启动的服务的类的名字:
            serviceIntent.setComponent(new ComponentName("com.example.startservicefromanotherapp","com.example.startservicefromanotherapp.AppService"));
    
            //1.3-------------------------------------------------------
    
            findViewById(R.id.btnStartAppService).setOnClickListener(this);
            findViewById(R.id.btnStopAppService).setOnClickListener(this);
            //------------------------------------------------------------
        }
        //1.4-------------------------------------------------
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.btnStartAppService:
                //1.6----------------------------------
                    startService(serviceIntent);
                //-------------------------------------
                    break;
                case R.id.btnStopAppService:
                    stopService(serviceIntent);
    
                    break;
            }
    
        } //----------------------------------------------------
    }


    02-跨应用绑定 Service:

  • 相关阅读:
    .net core consul
    numpy
    Cordova各个插件使用介绍系列(七)—$cordovaStatusbar手机状态栏显示
    ionic 的缓存 和局部刷新
    ionic 项目中添加modal的步骤流程
    ionic 项目中创建侧边栏的具体流程分4步简单学会
    Cordova各个插件使用介绍系列(八)—$cordovaCamera筛选手机图库图片并显示
    python-16: time 模块 之一
    python-16:模块 包
    c-3:位运算:位运算基本用法
  • 原文地址:https://www.cnblogs.com/juham/p/15219755.html
Copyright © 2011-2022 走看看