广播接收器未通过Xamarin

广播接收器未通过Xamarin

本文介绍了广播接收器未通过Xamarin Android中的OnReceive方法显示Toast的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试在我的项目中实现Broadcast接收器类时,我有一个扩展了BroadcastReceiver类的接收器,我打算检查是否通过单击按钮接收到Broadcast.OnReceive方法内部有一个Toast代码,该代码应显示nessage 检测到意图(如果成功发送了广播).我的代码看起来像这样...

Am trying to implement a Broadcast receiver class in my project, I have a receiver that extends class BroadcastReceiver and I intend to check if the Broadcast was received via a button click, The OnReceive method has a Toast code inside that should display the nessage Intent Detected if the broadcast was succesfully sent. My code looks like this...

 class FlashActivity : AppCompatActivity
    {
           protected override void OnCreate(Bundle savedInstanceState)
           {
         //Button definition
            Button button1 = this.FindViewById<Button>(Resource.Id.button1);
        //Clicking on this button should send the Broadcast message
            button1.Click += broadCastIntent;
           }
      //Method to send broadcast message on button click
       private void broadCastIntent(object sender,EventArgs e)
        {
            Intent intent = new Intent();
            intent.SetAction("com.Java_Tutorial.CUSTOM_INTENT");
            SendBroadcast(intent);
        }
       //BroadCast Receiver class Implementation

       [BroadcastReceiver(Name="com.Java_Tutorial.CUSTOM_INTENT")]
        public class MyReceiver: BroadcastReceiver
        {
            public override void OnReceive(Context context, Intent intent)
            {
           //Acknowledge message was received via a Toast
                Toast.MakeText(context, "Intent Detected.", ToastLength.Long).Show();
            }
        }
    }

我的Manifest.xml文件的接收方代码部分如下所示

The Receiver code section of my Manifest.xml file looks like this

<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
        <receiver android:name="MyReceiver">
            <intent-filter>
                <action android:name="com.Java_Tutorial.CUSTOM_INTENT"></action>
            </intent-filter>
        </receiver>
    </application>

当我单击按钮时,没有显示Toast,请帮助...

When i click the button, No Toast is displayed please help...

推荐答案

您没有在活动中注册 BroadcastReceiver .

请按照以下代码进行注册.

Please register it like following code.

  [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        MyReceiver receiver;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);
            Button button1 = this.FindViewById<Button>(Resource.Id.button1);

             receiver = new  MyReceiver();
            //Clicking on this button should send the Broadcast message
            button1.Click += broadCastIntent; ;

        }



        private void broadCastIntent(object sender, System.EventArgs e)
        {
            Intent intent = new Intent();
            intent.SetAction("com.Java_Tutorial.CUSTOM_INTENT");
            SendBroadcast(intent);


        }
        //BroadCast Receiver class Implementation
        protected override void OnResume()
        {
            base.OnResume();
            RegisterReceiver(receiver, new IntentFilter("com.Java_Tutorial.CUSTOM_INTENT"));
            // Code omitted for clarity
        }

        protected override void OnPause()
        {
            UnregisterReceiver(receiver);
            // Code omitted for clarity
            base.OnPause();
        }

        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }


    [BroadcastReceiver(Name = "com.Java_Tutorial.CUSTOM_INTENT")]
    public class MyReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            //Acknowledge message was received via a Toast
            Toast.MakeText(context, "Intent Detected.", ToastLength.Long).Show();
        }
    }
}

此处正在运行GIF.

这是有关广播的有用文章.您可以参考它.

Here is a helpful article about broadcast. You cna refer to it.

https://docs.microsoft.com/zh-cn/xamarin/android/app-fundamentals/broadcast-receivers#context-registering-a-broadcast-receiver

Android OS不为手电筒提供系统广播.但是我们可以检查手电筒的状态.如果手电筒的状态为打开,则可以发送广播,然后通过广播接收器来获取它.

Android OS do not provide system broadcast for flashlight. But we can check the flashlight's status.If the flashlight's status is open, we can send an broadcast, then our broadcastReceiver to get it.

// note: camera come from:  private Android.Hardware.Camera camera=Android.Hardware.Camera.Open();

 if (camera.GetParameters().FlashMode.Equals(Android.Hardware.Camera.Parameters.FlashModeOff))
            {
                switchOn = false;
            }
            else if (camera.GetParameters().FlashMode.Equals(Android.Hardware.Camera.Parameters.FlashModeTorch))
            {
                switchOn = true;
                Intent intent = new Intent();
                intent.SetAction("com.Java_Tutorial.CUSTOM_INTENT");
                SendBroadcast(intent);
            }

这篇关于广播接收器未通过Xamarin Android中的OnReceive方法显示Toast的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 01:53