0

I'm trying to access global activity variables (which I can't make as static) from a BroadcastReceiver . For that, I create a instance of the activity this way:

class wifiReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        MainActivity activity = (MainActivity)context.ApplicationContext;
        ...

But i get System.InvalidCastException: Specified cast is not valid. in instance creation line. What am i doing wrong?

EDIT: Some code of my activity

public class MainActivity : Activity
{
    private WifiManager _manager;
    private List<string> _wifiSignals;
    private wifiReceiver _wifiReceiver;
    private TextView _Text;

    protected override void OnCreate(Bundle bundle)
    {
        ...
        _wifiReceiver = new wifiReceiver();
        _manager = (WifiManager)GetSystemService(Context.WifiService);
        _wifiSignals = new List<string>();

        if (_manager.IsWifiEnabled)
        {
            _manager.StartScan();
        }
        ...
    }

And more extensive code from BroadcastReceiver:

public override void OnReceive(Context context, Intent intent)
        {
            MainActivity activity = (MainActivity)context.ApplicationContext;
            activity._wifiSignals.Clear();
            activity._wifiSignals.Add("Lista de wifi:\n");
            IList<ScanResult> wifiScanList = activity._manager.ScanResults;
            foreach (ScanResult wifiNetwork in wifiScanList)
            {
                activity._wifiSignals.Add(wifiNetwork.Ssid + ": " + wifiNetwork.Level);
            }

            //activity.presentation(activity._wifiSignals, activity);
            activity._manager.StartScan();
        }
Esporas
  • 11
  • 5
  • Why you want to cast Context to `MainActivity ` ? – ρяσѕρєя K May 12 '16 at 08:07
  • `ApplicationContext` is not an `Activity` `Context`, and it's specifically not `MainActivity`. You cannot do what you're attempting to do. – Mike M. May 12 '16 at 08:08
  • Are you getting error for this line?? -- 'MainActivity activity = (MainActivity)context.ApplicationContext; ' – Sandeep Kushwah May 12 '16 at 08:09
  • Following this [post](http://stackoverflow.com/questions/24825684/access-to-application-class-in-broadcast-receiver?rq=1), all I want to make is access to activity global variables. I will edit the code so you can see what i pretend to do. @SandeepKushwah yes, in that line. – Esporas May 12 '16 at 08:24

1 Answers1

0

Although I remember to call MainActivity properties from another activities in previous apps I developed, I'm pretty sure you cant call a function like you try to do with the StartScan(). The option I use normally is to store the data serialized, and call it in Main. I do a class with some methods like:

 class persistence
 {
    ISharedPreferences prefs;
    ISharedPreferencesEditor editor;

    public persistence(Context cont)
    {
        prefs = PreferenceManager.GetDefaultSharedPreferences(cont);
        editor = prefs.Edit();
    }
    public void store(List<articulo> articulos)
    {
        string raw = JsonConvert.SerializeObject(articulos);
        editor.PutString("articulos", raw);
        editor.Commit();
    }

    public List<articulo> recover()
    {
        string raw = prefs.GetString("articulos", null);
        List<articulo> lista;
        if (raw == null)
            lista = new List<articulo>();
        else
            lista = JsonConvert.DeserializeObject<List<articulo>>(raw);
        return lista;
    }
 }

In your OnReceive function I call to the store function In your OnCreate function you can do directly

persistence datos;
protected override void OnCreate(Bundle bundle)
{
    _wifiReceiver = new wifiReceiver();
    _manager = (WifiManager)GetSystemService(Context.WifiService);
    datos = new persistence (this);
    _wifiSignals = datos.recover();
    if(_wifiSignals.Count>0)
         StartScan();  
}

This will also keep data from one usage to another, if you don't want just clear the persistence data after call the BroadcastReceiver;

Mulflar
  • 424
  • 9
  • 22