问题
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();
}
}
}
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>
When i click the button, No Toast is displayed please help...
回答1:
When i click the button, No Toast is displayed
You do not register your BroadcastReceiver
in your Activity.
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();
}
}
}
Here is running GIF.
Here is a helpful article about broadcast. You cna refer to it.
https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/broadcast-receivers#context-registering-a-broadcast-receiver
Update
tried to find a post where you detect if flashlight is already on via a broadcast and then toggle switch accordingly in my app
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);
}
回答2:
You need to add an IntentFilter attribute
[BroadcastReceiver(Enabled = true)]
[IntentFilter(new[] { "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();
}
}
来源:https://stackoverflow.com/questions/64341648/broadcast-receiver-not-displaying-toast-via-onreceive-method-in-xamarin-android