sendBroadcast的使用()sendBroadcast

2023-09-06 00:14:43 作者:你如妖精般勾去我的魂╰

sendBroadcast() - 它是否应叫里面的活动? 我想打电话给sendBroadcast()从我的公用事业类,它不会扩展活动的方法。我收到编译错误如下

sendBroadcast() - Should it be called inside Activity? I am trying to call sendBroadcast() from my method of utility-class which doesn't extend Activity. I am getting compilation error as below

该方法sendBroadcast(意向)是   未定义的类型MyWrapperClass   MyWrapperClass.java

The method sendBroadcast(Intent) is undefined for the type MyWrapperClass MyWrapperClass.java

下面是code片断:

abstract class MyWrapperClass {

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

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

            sendBroadcast(intent);

         }
    }

有没有用我的班级里sendBroadcast电话后面的任何概念。有一个在使用sendBroadcast()内的活动没有任何问题。 有人可以在这里帮我解决这个问题? 或任何其他建议,请从实用工具类数据返回给应用程序异步。 先谢谢了。

Is there any concept behind using sendBroadcast call inside my class. There is no issue in using sendBroadcast() inside Activity. 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等。

You should pass the context from activity class to utility class to access the specific application resources like startActivity, sendBroadcast, etc.

context.sendBroadcast(intent);