极简实现android跨进程通信

首先,在AndroidManifest中将service置于独立进程:

        <service
             android:name=".service.downloadService"
             android:enabled="true"
             android:exported="true"
             android:process=":service"/>  <!-- 此语句将service置于独立进程-->

1,在service内发送广播

 Intent mIntent = new Intent("dukerDev");    //添加广播过滤标识
        mIntent.putExtra("msg", "Download completed" );  //要传输的数据
        sendBroadcast(mIntent);  //发送广播

2,在activity内接收广播

private IBpService mService;

private final IntentFilter intentFilter = new IntentFilter(); //new一个过滤器,注册广播需要
private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
    
    
        //new一个广播接收器,注册广播需要,以及在此处加入收到广播后的逻辑代码
        @Override
        public void onReceive(Context context, Intent intent) {
    
    
        String action = intent.getAction();
        if(action.equals("dukerDev")){
    
    //根据过滤标识区分广播
             Log.e(TAG,"broadcast rec " + intent.getStringExtra("msg"));  //取数据
       }
   }
};

final ServiceConnection mConnection = new ServiceConnection() {
    
      //new一个服务连接实例
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
    
    
        mService = IDownloadService.Stub.asInterface(service);
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
    
    
        mService = null;
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    
    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    //绑定并启动服务
    bindService(new Intent(this, downloadService.class), mConnection, Context.BIND_AUTO_CREATE);

    intentFilter.addAction("dukerDev");//添加要接收的广播过滤标识
    registerReceiver(mBroadcastReceiver, intentFilter);//注册广播接收
}


           
        
@Override
protected void onDestroy() {
    
    
   try {
    
    
     unregisterReceiver(mBroadcastReceiver);  //activity销毁时,记得注销广播接收器,防止内存泄漏
   }catch (Exception ex){
    
    
     //异常处理
   }
   super.onDestroy();//此句放在自己逻辑的后面,因为这是系统在检查及进行注销销毁等操作,需要放到最后
}

日志输出:
在这里插入图片描述

完整代码

service:

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

import androidx.annotation.Nullable;

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

    @Override
    public void onCreate() {
    
    
        super.onCreate();

        Intent mIntent = new Intent("dukerDev");    //添加广播过滤标识
        mIntent.putExtra("msg", "Download completed" );  //要传输的数据
        sendBroadcast(mIntent);  //发送广播

    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
    
    
        return null;
    }
}

activity:

import androidx.viewbinding.ViewBinding;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;

import com.L0106.ble.base.BaseActivity;

import com.L0106.ble.R;
import com.L0106.ble.databinding.ActivityBroadcastBinding;
import com.L0106.ble.databinding.ActivityScanBinding;
import com.L0106.ble.service.downloadService;

public class broadcastActivity extends BaseActivity {
    
    
    private ActivityBroadcastBinding binding;
    private final String TAG = "broadcastActivityLogX";

    private final IntentFilter intentFilter = new IntentFilter(); //new一个过滤器,注册广播需要
    private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
    
    
        //new一个广播接收器,注册广播需要,以及在此处加入收到广播后的逻辑代码
        @Override
        public void onReceive(Context context, Intent intent) {
    
    
            String action = intent.getAction();
            if(action.equals("dukerDev")){
    
    //根据过滤标识区分广播
                Log.e(TAG,"broadcast rec " + intent.getStringExtra("msg"));  //取数据
            }
        }
    };

    final ServiceConnection mConnection = new ServiceConnection() {
    
      //new一个服务连接实例
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
    
    
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
    
    
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        binding = ActivityBroadcastBinding.inflate(getLayoutInflater());
        View rootView = binding.getRoot();
        setContentView(rootView);
        setTitle("broadcast test",true, null);

        //绑定并启动服务
        bindService(new Intent(this, downloadService.class), mConnection, Context.BIND_AUTO_CREATE);

        intentFilter.addAction("dukerDev");//添加要接收的广播过滤标识
        registerReceiver(mBroadcastReceiver, intentFilter);//注册广播接收
    }

    @Override
    protected ViewBinding getViewBinding() {
    
    
        return ActivityBroadcastBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), false);
    }

    @Override
    protected void onDestroy() {
    
    
        try {
    
    
            unregisterReceiver(mBroadcastReceiver);  //activity销毁时,记得注销广播接收器,防止内存泄漏
        }catch (Exception ex){
    
    
            //异常处理
        }
        super.onDestroy();
    }

    @Override
    protected void doRight() {
    
    

    }
}
站在痛苦之外规劝受苦的人,是件很容易的事。       ------《被缚的普罗米修斯》

猜你喜欢

转载自blog.csdn.net/Duker_tec/article/details/122564866