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

简单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





    

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

 

你可能感兴趣的:(Android,Native,基础)