[英]Detect plugged & unplugged event on headphone jack in Xamarin.Android
我正在嘗試找到一種方法來檢測 Xamarin.Android 中耳機插孔上的插入/拔出事件。 有沒有辦法做到這一點? 如果是,我該如何實現該功能?
您必須在 xamarin.android 文件中編寫代碼。 使用廣播接收器來實現。 在 onReceive() 中,您將獲得 Intent.ACTION_HEADSET_PLUG 的意圖。
以下偽代碼可能會對您有所幫助,
public class MainActivity extends AppCompatActivity {
BroadcastReceiver broadcastReceiver;
boolean Microphone_Plugged_in = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
int iii;
if (Intent.ACTION_HEADSET_PLUG.equals(action)) {
iii = intent.getIntExtra("state", -1);
if (iii == 0) {
Microphone_Plugged_in = false;
Toast.makeText(getApplicationContext(), "microphone not plugged in", Toast.LENGTH_LONG).show();
}
if (iii == 1) {
Microphone_Plugged_in = true;
Toast.makeText(getApplicationContext(), "microphone plugged in",
Toast.LENGTH_LONG).show();
}
}
}
};
IntentFilter receiverFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
registerReceiver(broadcastReceiver, receiverFilter);
}
}
你可以嘗試將java代碼轉換成c#,例如:
[BroadcastReceiver(Enabled = true)]
[IntentFilter(new[] { Android.Content.Intent.ActionHeadsetPlug })]
public class MyBroadcastReceiver : BroadcastReceiver
{
bool Microphone_Plugged_in = false;
public override void OnReceive(Context context, Intent intent)
{
String action = intent.Action;
int iii;
if (Intent.ActionHeadsetPlug.Equals(action))
{
iii = intent.GetIntExtra("state", -1);
if (iii == 0)
{
Microphone_Plugged_in = false;
Toast.MakeText(context, "microphone not plugged in", ToastLength.Long).Show();
}
if (iii == 1)
{
Microphone_Plugged_in = true;
Toast.MakeText(context, "microphone plugged in", ToastLength.Long).Show();
}
}
}
}
和用法:
BroadcastReceiver broadcastReceiver;
IntentFilter receiverFilter;
並在您的活動的OnCreate
方法中初始化值:
broadcastReceiver = new MyBroadcastReceiver();
receiverFilter = new IntentFilter(Intent.ActionHeadsetPlug);
並通過重寫方法OnResume
和OnPause
來RegisterReceiver
和UnregisterReceiver
protected override void OnResume()
{
base.OnResume();
RegisterReceiver(broadcastReceiver, receiverFilter);
}
protected override void OnResume()
{
base.OnResume();
IntentFilter receiverFilter = new IntentFilter(Intent.ActionHeadsetPlug);
RegisterReceiver(broadcastReceiver, receiverFilter);
}
有關更多信息,您可以查看線程: 檢測耳機是否插入 Android 設備。
https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/broadcast-receivers
[BroadcastReceiver(Enabled = true)]
[IntentFilter(new[] { Android.Content.Intent.ActionHeadsetPlug })]
public class HeadsetPlugBroadcastReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
String action = intent.Action;
if (Intent.ActionHeadsetPlug.Equals(action))
{
var state = intent.GetIntExtra("state", -1);
if (state == 0)
{
// Plugged out
}
if (state == 1)
{
// Plugged in
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.