Skip to content Skip to sidebar Skip to footer

Access Method And Varaible In Mainactivity From Extern Broadcastreceiver

My MainActivity class is big. Therefor, I want to seperate the inner class BroadcastListener into normal class but I am facing the problem that I do not know how can I access the

Solution 1:

Create one interface as

 interface ReceiverInteface
{
 onBroadcastReceive();

}

let mainActivity implements this interface

    mainactivity implementsReceiverInterface

{
   @overrideonBroadcastReceive()
{ 
   //do all you task here
}

}

And BroadCastReceiverListner Class

pass refrence of interface from Mainactiivty.

   ReceiverInteface recevierListner;
    BroadcastReceiverListener(ReceiverInteface mListner)
    {
     recevierListner = mListner;
    }

privateclassBroadcastReceiverListenerextendsBroadcastReceiver {
        @OverridepublicvoidonReceive(Context context, Intent intent) {
if (intent.getAction().equals(
                    android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
recevierListner.onBroadcastReceive();
}
             }

}

Post a Comment for "Access Method And Varaible In Mainactivity From Extern Broadcastreceiver"