【发布时间】:2020-10-13 19:06:45
【问题描述】:
我正在尝试在我的项目中实现广播接收器类,我有一个扩展类 BroadcastReceiver 的接收器,我打算检查是否通过单击按钮接收到广播,OnReceive 方法内部有一个 Toast 代码,应该显示nessage Intent Detected 如果广播成功发送。我的代码看起来像这样...
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 文件的 Receiver 代码部分如下所示
<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 请帮助...
【问题讨论】:
标签: android xamarin button broadcast