• 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:

  • 相关阅读:
    Linux程序分析工具介绍—ldd,nm
    Makefile学习(三)[第二版]
    Linux下的tree命令 --Linux下目录树查看
    Makefile学习(二)[第二版]
    Makefile学习(一)[第二版]
    Linux下top命令详解
    Shell编程入门(第二版)(下)
    mysql用命令行导入sql文件
    javascript的onbeforeunload函数在IOS上运行
    mysql如何利用Navicat 导出和导入数据库
  • 原文地址:https://www.cnblogs.com/juham/p/15219755.html
Copyright © 2020-2023  润新知