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:

  • 相关阅读:
    重学数据库
    小炒肉
    地三鲜
    mac安装使用visualbox不能启动
    idea Services dashboard
    C# 使用Linq问题
    IDEA 修改页面不重启
    idea提交代码失败
    Idea git 强制同步fork的项目,会覆盖本地上传的文件吗?
    了解swagger遇到的问题
  • 原文地址:https://www.cnblogs.com/juham/p/15219755.html
Copyright © 2011-2022 走看看