Broadcastreciever not working when app is closed

BrunoVTBrunoVT BEUniversity ✭✭
edited March 2015 in Xamarin.Android

Hi,

I have a broadcastreciever that listens to incoming text messages and flashes the cameraflash when there is a new text. it works when the app is active but not when the app is closed.

    [BroadcastReceiver(Enabled = true, Label = "SmsListener")]
    [IntentFilter(new [] { "android.provider.Telephony.SMS_RECEIVED" }, Priority = Int32.MaxValue)]

    public class SmsListener : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            //PowerManager.WakeLock sWakeLock;
            //var pm = PowerManager.FromContext(context);
            //sWakeLock = pm.NewWakeLock(WakeLockFlags.Partial, "SMSListener");
            //sWakeLock.Acquire();

            Toast.MakeText(context, "recieved", ToastLength.Long).Show();

            if (Telephony.Sms.Intents.SmsReceivedAction == intent.Action)
            {
                new Thread(new Runnable(() => {
                    Flash(500);
                    Thread.Sleep(250);
                    Flash(500);
                })).Start();
            }

            //sWakeLock.Release();
        }

        public void Flash(int flashDuration)
        {
            try
            {
                var camera = Camera.Open();
                var parameters = camera.GetParameters();
                var supportedFlashModes = camera.GetParameters().SupportedFlashModes;

                var flashMode = string.Empty;
                if (supportedFlashModes.Contains(Camera.Parameters.FlashModeTorch))
                {
                    flashMode = Camera.Parameters.FlashModeTorch;
                }
                else if (supportedFlashModes.Contains(Camera.Parameters.FlashModeOn))
                {
                    flashMode = Camera.Parameters.FlashModeOn;
                }

                if (!string.IsNullOrEmpty(flashMode))
                {
                    parameters.FlashMode = flashMode;
                    camera.SetParameters(parameters);
                    camera.StartPreview();

                    Thread.Sleep(flashDuration);
                }

                parameters.FlashMode = Camera.Parameters.FlashModeOff;
                camera.SetParameters(parameters);
                camera.StopPreview();
                camera.Release();
                camera.Dispose();
            }
            catch (System.Exception ex)
            {
                if (Debugger.IsAttached)
                {
                    Debug.WriteLine(ex.ToString());
                }
            }
        }
    }

<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" package="PackageName" android:versionCode="1" android:versionName="0.1">
    <uses-sdk android:minSdkVersion="14" />
    <application android:label="Appname" android:icon="@drawable/launcher"></application>
    <uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" />
    <uses-permission android:name="android.permission.FLASHLIGHT" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_MMS" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <uses-feature android:name="android.hardware.camera" android:required="false" />
    <uses-feature android:name="android.hardware.camera.flash" android:required="true" />
</manifest>

Posts

  • CheesebaronCheesebaron DKInsider, University mod
    edited March 2015

    You need a service for that. Also the priority is wrong.

    The value must be an integer, such as "100". Higher numbers have a higher priority. The default value is 0. The value must be greater than -1000 and less than 1000.

    Source: <intent-filter> element documentation

Sign In or Register to comment.