【问题标题】:Usage of sendBroadcast()sendBroadcast() 的使用
【发布时间】:2011-06-20 11:08:31
【问题描述】:

sendBroadcast() - 应该在 Activity 中调用它吗? 我正在尝试从我的实用程序类方法调用 sendBroadcast(),该方法不扩展 Activity。我收到如下编译错误

方法 sendBroadcast(Intent) 是 未定义类型 MyWrapperClass MyWrapperClass.java

这里是sn-p的代码:

abstract class MyWrapperClass {

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

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

            sendBroadcast(intent);

         }
    }

在我的课堂上使用 sendBroadcast 调用背后有什么概念吗?在 Activity 中使用 sendBroadcast() 没有问题。 这里有人可以帮我解决吗? 或者邀请任何其他建议将数据从实用程序类异步返回到应用程序。 提前致谢。

【问题讨论】:

  • 您需要访问应用程序上下文才能调用 sendbroadcast。你能不能从 android 文档中得到这些信息。

标签: java android class android-intent


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    context.sendBroadcast(intent);
    

    【讨论】:

      猜你喜欢
      • 2020-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多