在开发安卓的过程中,用户使用的PDA支持扫描识别,那么就需要监听厂家实现的广播事件,然后在Activity中注册广播及销毁广播是个繁琐的操作,因此封装了这个全局广播。
package com.youvigo.wms.components.broadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import com.youvigo.wms.data.backend.repository.LocalRepository;
import com.youvigo.wms.exception.ReceiverException;
import org.apache.commons.lang3.StringUtils;
import timber.log.Timber;
import java.util.function.Consumer;
public class GlobalReceiver extends BroadcastReceiver implements DefaultLifecycleObserver {
private static final String TAG = GlobalReceiver.class.getSimpleName();
private Context context;
private IntentFilter intentFilter;
private Consumer<String> successHandler;
private Consumer<Throwable> errorHandler;
@Override
public void onReceive(Context context, Intent intent) {
final String scanResult = intent.getStringExtra(LocalRepository.getInstance()
.getBroadcastData());
if (StringUtils.isBlank(scanResult)) {
if (null != errorHandler) {
errorHandler.accept(new ReceiverException("未能正确获取扫描数据,请检查扫描设置是否正确。"));
}
} else {
if (null != successHandler) {
String chompStr = StringUtils.chomp(scanResult);
String deletedWhitespace = StringUtils.deleteWhitespace(chompStr);
successHandler.accept(deletedWhitespace);
}
}
}
public GlobalReceiver setLifecycleOwner(@Nullable LifecycleOwner lifecycleOwner) {
if (null != lifecycleOwner) {
lifecycleOwner.getLifecycle()
.addObserver(this);
}
return this;
}
public GlobalReceiver setContext(Context context) {
this.context = context;
return this;
}
public GlobalReceiver setIntentFilter(IntentFilter filter) {
this.intentFilter = filter;
return this;
}
public GlobalReceiver setSuccessHandler(Consumer<String> successHandler) {
this.successHandler = successHandler;
return this;
}
public GlobalReceiver setErrorHandler(Consumer<Throwable> errorHandler) {
this.errorHandler = errorHandler;
return this;
}
@Override
public void onCreate(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onCreate(owner);
Timber.tag(TAG)
.i("onCreate");
}
@Override
public void onDestroy(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onDestroy(owner);
Timber.tag(TAG)
.i("onDestroy");
}
@Override
public void onPause(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onPause(owner);
unRegisterReceiver();
Timber.tag(TAG)
.i("onPause");
}
@Override
public void onResume(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onResume(owner);
registerReceiver();
Timber.tag(TAG)
.i("onResume");
}
@Override
public void onStart(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onStart(owner);
Timber.tag(TAG)
.i("onStart");
}
@Override
public void onStop(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onStop(owner);
Timber.tag(TAG)
.i("onStop");
}
/**
* 广播注册
*/
private void registerReceiver() {
if (null != context && null != intentFilter) {
ContextCompat.registerReceiver(context, this, intentFilter, ContextCompat.RECEIVER_EXPORTED);
}
}
private void unRegisterReceiver() {
try {
context.unregisterReceiver(this);
} catch (Exception e) {
if (null != errorHandler) {
errorHandler.accept(e);
}
}
}
}
使用
在Activity中注入即可
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new GlobalReceiver().setContext(this)
.setLifecycleOwner(this)
.setIntentFilter(new IntentFilter("广播字段"))
.setSuccessHandler(this::addCode)
.setErrorHandler(err -> showToast(this, err.getMessage()));
}
文章评论