简体   繁体   中英

Passing a string from an event to MainActivity in Android Xamarin Visual Studio

The project is done in Visual Studio with Xamarin and targeted for Android.

In my simple project I have 1 activity (MainActivity). Here I create an instance of a Scanner object that will sit in background and listen for iBeacons. Once the scanner has been created I call a Start method on the scanner object.

In the constructor of the Scanner object, there will be setup an instance of a Listener object. This instance is called when ever a beacon is detected.

Cut short – my main activity makes an instance of a scanner object. On creation of the scanner object a listener object is created. This listener object will be activated when a beacon is detected.

In my MainActivity I have a multiline TextView. I want it to show the beacons found in the listener object.

What is the best way to pass this beacon ID (string) to the TextView in the main activity?

I'm new to programming in Android, so all the different concepts with blocks, intents and what have you, are a bit confusing. I would have thought it was straight forward passing data from a listener event to the TextView but this has proven more difficult then expected.

I don't mind going slow – so please feel free to elaborate and consider me the novice I am :-D

UPDATE: I have edited the code to make it as short as possible, and pasted it below. Hope this gives an idea.

[Activity(Label = "DeviceScanSample", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity
{

    KontaktScanner scanner;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.Main);

        // Initialize Scanner
        scanner = new KontaktScanner(this);

        // Button actions
        startScanButton.Click += delegate
        {
            if (CheckPermission(Manifest.Permission.AccessCoarseLocation))
            {
                // Start devices scan
                scanner.Start();
            }
            else
            {
                // Ask for permissions if needed
                ...
            }
        };
    }
}

public class KontaktScanner : Java.Lang.Object, IOnServiceReadyListener
{

    IProximityManager proximityManager;

    public KontaktScanner(Context context)
    {
        // Set Space listener
        proximityManager.SetSpaceListener(new KontaktSimpleSpaceListener());
    }

    public void Start()
    {
        proximityManager.Connect(this);
    }
}


class KontaktSimpleSpaceListener : SimpleSpaceListener
{

    public void OnRegionEntered(IBeaconRegion beaconRegion)
    {
        Log.Info(TAG, string.Format("Entered {0} region", beaconRegion.Identifier));
    }

    public void OnRegionAbandoned(IBeaconRegion beaconRegion)
    {
        Log.Info(TAG, string.Format("Abandoned {0} region", beaconRegion.Identifier));
    }
}

Try something like this:

Pass the context here:

public KontaktScanner(Activity activity)
{
    // Set Space listener
    proximityManager.SetSpaceListener(new KontaktSimpleSpaceListener(activity));
}

Then:

class KontaktSimpleSpaceListener : SimpleSpaceListener
{
    Activity context
    public KontaktSimpleSpaceListener(Activity activity)
    {
        this.context = activity; 
    }

    public void OnRegionEntered(IBeaconRegion beaconRegion)
    {
        Log.Info(TAG, string.Format("Entered {0} region", beaconRegion.Identifier));
        MainActivity myActivity = (MainActivity) context;
        myActivity.updateTextView("My Data");// pass the string here.
    }

    public void OnRegionAbandoned(IBeaconRegion beaconRegion)
    {
        Log.Info(TAG, string.Format("Abandoned {0} region", beaconRegion.Identifier));
        MainActivity myActivity = (MainActivity) context;
        myActivity.updateTextView("My another data");// pass the string here.
    }
}

Then create method in MainActivity :

public void updateTextView(string s) 
{
    RunOnUiThread(() =>
    {
        yourTextView.Text = s;//set your TextView here
    });
}

I have not checked the syntax but something like this should work.

If found an answer in this video: https://forums.xamarin.com/discussion/comment/308314#Comment_308314

Thanks to everyone trying to help out. Highly appreciate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM