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:

 

posted @ 2021-09-02 17:25  juham  阅读(36)  评论(0编辑  收藏  举报