Android--AIDL跨应用启动和关闭Service

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dpl12/article/details/82753325

前言

AIDL是一个缩写,全称是Android Interface Definition Language,翻译为Android接口定义语言。主要用于线程之间的通信,本文主要以不同应用之间使用AIDL通信为例介绍AIDL。

AIDL的使用按照AIDL文件类型分类,一种是序列化数据类,需要实现Parcelable,另一种是定义方法接口,以供系统使用来完成跨进程通信的。

AIDL默认支持JAVA的八种基本数据类型、String、CharSequence、List类型、Map类型。

简单Demo

应用一app:

MainActivity.java

package com.example.dpl.startservicefromanotherapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intent=new Intent(this,MyService.class);
        startService(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        intent=new Intent(this,MyService.class);
        stopService(intent);
    }
}

MyService.java

package com.example.dpl.startservicefromanotherapp;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class MyService extends Service {
    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("startService");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("stopService");
    }
}

另一个应用anotherApp

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/btnStart"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="开启另一个app的服务"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.383" />

    <Button
        android:id="@+id/btnStop"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="停止另一个 App服务"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnStart"
        app:layout_constraintVertical_bias="0.204" />
</android.support.constraint.ConstraintLayout>

MainActivity.java

package com.example.anotherapp;

import android.content.ComponentName;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Intent anotherIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btnStart).setOnClickListener(this);
        findViewById(R.id.btnStop).setOnClickListener(this);
        anotherIntent=new Intent();
        //调用setComponent方法启动外部应用程序(获取ComponentName对象)
        //ComponentName类中初始化外部应用的包名和类名
        anotherIntent.setComponent(new ComponentName("com.example.dpl.startservicefroman" +
                "otherapp","com.example.dpl.startservicefromanotherapp.MyService"));
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.btnStart:
                startService(anotherIntent);
                break;
            case R.id.btnStop:
                stopService(anotherIntent);
                break;
        }
    }
}

运行结果:

09-17 21:54:16.694 2160-2160/? I/System.out: startService
09-17 21:55:36.901 2160-2160/com.example.dpl.startservicefromanotherapp I/System.out: stopService
09-17 21:56:00.000 2160-2160/com.example.dpl.startservicefromanotherapp I/System.out: startService
09-17 21:56:12.384 2160-2160/com.example.dpl.startservicefromanotherapp I/System.out: stopService
09-17 22:27:34.129 2160-2160/com.example.dpl.startservicefromanotherapp I/System.out: startService
09-17 22:27:36.091 2160-2160/com.example.dpl.startservicefromanotherapp I/System.out: stopService

猜你喜欢

转载自blog.csdn.net/dpl12/article/details/82753325