public class GlobalReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("com.barcode.sendBroadcast".equals(intent.getAction())) {
String code = intent.getStringExtra("BARCODE");
if (code.endsWith("\n")) {
code = code.substring(0, code.length() - 1);
}
if (code.endsWith("\r")) {
code = code.substring(0, code.length() - 1);
}
}
}
}
<receiver android:name=".GlobalReceiver">
<intent-filter>
<action android:name="com.barcode.sendBroadcast" />
</intent-filter>
</receiver>
要注意 android8.0 及以上不允许静态注册广播,只能代码动态注册。
网友评论