Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

How do I update my activity using a BroadcastReceiver?

$
0
0

I asked a similar question earlier and received some help, but I'm still stuck and I feel like I'm pertty close. Any help is appreciated. I want to update the text of a textview when the power is plugged or unplugged.

`Here is my code:

using System; using Android.App.Admin; using Android.Graphics; using Android.Runtime; using Android.Views; using Android.App; using Android.Content; using Android.Widget; using Android.OS; using Java.Lang;

namespace PowerConnectedDetect { [Activity(Label = "Power Connected Detect", MainLauncher = true)] public class MainActivity : Activity { private int count = 1; public TextView txtNote; private DetectPowerConnectedStateRecevier _recevier;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        txtNote = FindViewById<TextView>(Resource.Id.textView1);

        Button button = FindViewById<Button>(Resource.Id.MyButton);

        button.Click += delegate
            {
                RunOnUiThread(() =>
                    {
                        button.Text = string.Format("{0} clicks!", count++);
                    });
            };

        _recevier = new DetectPowerConnectedStateRecevier(); 

        RegisterReceiver(_recevier, new IntentFilter(Intent.ActionBatteryChanged));
        RegisterReceiver(_recevier, new IntentFilter(Intent.ActionPowerConnected));
        RegisterReceiver(_recevier, new IntentFilter(Intent.ActionPowerDisconnected));
    }

    protected override void OnResume()
    {
        base.OnResume();
        RegisterReceiver(_recevier, new IntentFilter(Intent.ActionBatteryChanged));
        RegisterReceiver(_recevier, new IntentFilter(Intent.ActionPowerConnected));
        RegisterReceiver(_recevier, new IntentFilter(Intent.ActionPowerDisconnected));
    }

    protected override void OnPause()
    {
        base.OnPause();
        UnregisterReceiver(_recevier);
    }

    public void Disconnected()
    {
        RunOnUiThread(() =>
            {
                if (txtNote == null)
                    txtNote = FindViewById<TextView>(Resource.Id.textView1);

                txtNote.Text = "Disconnected";
            });
    }

    public void Connected()
    {
        RunOnUiThread(() =>
            {
                if (txtNote == null)
                    txtNote = FindViewById<TextView>(Resource.Id.textView1);

                txtNote.Text = "Connected";
            });
    }

}

[BroadcastReceiver]
[IntentFilter(new[] {Intent.ActionBatteryChanged, Intent.ActionPowerConnected, Intent.ActionPowerDisconnected})]
public class DetectPowerConnectedStateRecevier : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        if (intent.Action == Intent.ActionPowerConnected)
        {
            ((MainActivity)context).Connected();
        }

        if (intent.Action == Intent.ActionPowerDisconnected)
        {
            ((MainActivity)context).Disconnected();
        }
    }
}

}

`

and here is my AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="PowerConnectedDetect.PowerConnectedDetect" android:versionCode="1" android:versionName="1.0">
    <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="15" />
    <application android:label="PowerConnectedDetect">
    <receiver android:name=".DetectPowerConnectedStateRecevier">
      <intent-filter>
        <action android:name="android.intent.action.ACTION_BATTERY_CHANGED"></action>
        <action android:name="android.intent.action.ACTION_POWER_CONNECTED"></action>
        <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"></action>
      </intent-filter>
    </receiver>
  </application>
</manifest>

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>