简体   繁体   English

sendBroadcast()的用法

[英]Usage of sendBroadcast()

sendBroadcast() - Should it be called inside Activity? sendBroadcast() - 它应该在Activity里面调用吗? I am trying to call sendBroadcast() from my method of utility-class which doesn't extend Activity. 我试图从我的实用程序类方法调用sendBroadcast(),它不扩展Activity。 I am getting compilation error as below 我收到编译错误如下

The method sendBroadcast(Intent) is undefined for the type MyWrapperClass MyWrapperClass.java 对于类型MyWrapperClass MyWrapperClass.java,方法sendBroadcast(Intent)未定义

Here is the code snippet: 这是代码片段:

abstract class MyWrapperClass {

    public static void sendData()
         {
             Intent intent = new Intent ("com.proj.utility.mgr",null);

             intent.putExtra("example","Broadcasting "); 

            sendBroadcast(intent);

         }
    }

Is there any concept behind using sendBroadcast call inside my class. 在我的课程中使用sendBroadcast调用是否有任何概念。 There is no issue in using sendBroadcast() inside Activity. 在Activity中使用sendBroadcast()没有问题。 Can someone here help me to resolve it? 有人可以帮我解决吗? Or Any other suggestions are invited to return data from utility class to application asynchronously. 或者邀请任何其他建议以异步方式将数据从实用程序类返回到应用程序。 Thanks in advance. 提前致谢。

您应该将上下文从活动类传递给实用程序类,以访问特定的应用程序资源,如startActivity,sendBroadcast等。

context.sendBroadcast(intent);

如果您扩展Application对象(在第一个Activity之前调用的主条目),那么您可以为应用程序保留共享单重。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM