本文介绍了如何向清单中的接收者发送自定义广播动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
MyReceiver.java
MyReceiver.java
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
Log.i("MyReceiver", "MyAction received!");
}
}
在AndroidManifest.xml
中(在application
标记下)
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="MyAction" />
</intent-filter>
</receiver>
MainActivity.Java
MainActivity.Java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sendBroadcast(new Intent("MyAction"));
}
}
MyReceiver.onReceive
方法永远不会触发.我错过了什么吗?
MyReceiver.onReceive
method is never triggered.Did I miss something?
推荐答案
然后,您必须使用一个明确的Intent
,用于标识接收方,例如:
Then you have to use an explicit Intent
, one that identifies the receiver, such as:
sendBroadcast(new Intent(this, MyReceiver.class).setAction("MyAction"));
请参阅Android 8发行文档中的广播限制.
See Broacast limitations in Android 8 release docs.
这篇关于如何向清单中的接收者发送自定义广播动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!