Note: If your app targets API level 26 or higher, you cannot use the manifest to declare a receiver for implicit broadcasts (broadcasts that do not target your app specifically), except for a few implicit broadcasts that are exempted from that restriction. In most cases, you can use scheduled jobs instead.
1
intent.setPackage("com.comm.util")
Sending broadcasts方式
普通广播
有序广播
FirstOrderBroadCast
1 2 3 4 5 6 7 8 9 10
classFirstOrderBroadCast : BroadcastReceiver() { overridefunonReceive(context: Context?, intent: Intent?) { Log.e("receive", "Low"); val code = 1 valdata = "hello I am FirstOrderBroadCast" val bundle: Bundle? = Bundle() bundle?.putString("first","from FirstOrderBroadCast") setResult(code, data, bundle) } }
method sends broadcasts to receivers that are in the same app as the sender. If you don’t need to send broadcasts across apps, use local broadcasts.he implementation is much more efficient (no interprocess communication needed) and you don’t need to worry about any security issues related to other apps being able to receive or send your broadcasts.(应用内发送广播)
发送
1 2 3
val intentBroad = Intent() intentBroad.action = UpdateBroadcastReceiver.ACTION_UPDATE LocalBroadcastManager.getInstance(this).sendBroadcast(intentBroad)