首页 > 移动平台 > 详细

09-Android 中 AIDL 的理解与使用

时间:2021-09-02 20:31:00      阅读:22      评论:0      收藏:0      [点我收藏+]

 

跨应用启动 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:

 

09-Android 中 AIDL 的理解与使用

原文:https://www.cnblogs.com/juham/p/15219755.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!