如何从活动外进行动态注册接收?

时间:2014-02-11 08:18:58

标签: android broadcastreceiver

我有一个类结构如下

public class Admin extends DeviceAdminReceiver 
{
 public static class PubSub extends Activity
 {
      protected void onCreate(Bundle savedInstanceState) {

        messageIntentReceiver = new MQTTMessageReceiver();  
    IntentFilter intentCFilter = new IntentFilter(MQTTService.MQTT_MSG_RECEIVED_INTENT);  
    registerReceiver(messageIntentReceiver, intentCFilter); 
      }

      @Override
        protected void onDestroy()
        {
            unregisterReceiver(messageIntentReceiver);
            super.onDestroy();
        }

         public class MQTTMessageReceiver extends BroadcastReceiver  
    {  
        @Override   
       public void onReceive(Context context, Intent intent)  
       {  
        Bundle notificationData = intent.getExtras();  
        String newTopic =                            notificationData.getString(MQTTService.MQTT_MSG_RECEIVED_TOPIC);  
        String newData  = notificationData.getString(MQTTService.MQTT_MSG_RECEIVED_MSG);
        Log.e("Received Message on",newTopic+"- "+newData);
           }
        }
 }
}

接收器是动态注册和未注册的。当PubSub活动停留在屏幕上时,广播接收器工作。如何让它在活动之外工作?

我尝试通过在manifest.xml中注册以静态方式执行此操作,但该dint工作为MQTTMessageReceiver是非静态内部类。无法实例化我得到的接收器运行时错误。

我无法更改MQTTMessageReceiver类静态,因为我需要访问外部类成员。

0 个答案:

没有答案