如何向清单中的接收器发送自定义广播操作? [英] How to send a custom broadcast action to receivers in manifest?

查看:41
本文介绍了如何向清单中的接收器发送自定义广播操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

推荐答案

我使用 Android 8.

I use Android 8.

然后你必须使用一个明确的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.

这篇关于如何向清单中的接收器发送自定义广播操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆