Android--AIDL跨应用绑定和解绑Service

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

简单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;
import android.os.RemoteException;

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

    @Override
    public IBinder onBind(Intent intent) {
        //调用AIDL接口
       return new IAppServiceRemoteBinder.Stub() {
           @Override
           public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

           }
       };
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

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

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("stopService");
    }
}
IAppServiceRemoteBinder.aidl
// IAppServiceRemoteBinder.aidl
package com.example.dpl.startservicefromanotherapp;

// Declare any non-default types here with import statements

interface IAppServiceRemoteBinder {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);
}

另一个应用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.222" />

    <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.125" />

    <Button
        android:id="@+id/btnBinder"
        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_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnStop"
        app:layout_constraintVertical_bias="0.194" />

    <Button
        android:id="@+id/btnUbinder"
        android:layout_width="0dp"
        android:layout_height="38dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="32dp"
        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/btnBinder"
        app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>

MainActivity.java

package com.example.anotherapp;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection {

    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);
        findViewById(R.id.btnBinder).setOnClickListener(this);

        findViewById(R.id.btnUbinder).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;
            case R.id.btnBinder:
                bindService(anotherIntent,this, Context.BIND_AUTO_CREATE);
                break;
            case R.id.btnUbinder:
                unbindService(this);
                break;
        }
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {

        System.out.println("Binder Service");
        System.out.println(service);
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {

    }
}

运行结果:

09-17 23:08:12.226 2941-2941/com.example.dpl.startservicefromanotherapp I/System.out: startService
09-17 23:48:40.073 2941-2941/com.example.dpl.startservicefromanotherapp I/System.out: stopService
09-17 23:48:49.040 2941-2941/com.example.dpl.startservicefromanotherapp I/System.out: startService
09-17 23:48:49.043 3191-3191/com.example.anotherapp I/System.out: Binder Service
    android.os.BinderProxy@584fb01
09-17 23:48:51.803 2941-2941/com.example.dpl.startservicefromanotherapp I/System.out: stopService

猜你喜欢

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