Android FC尝试操作意图时\u耳机\u插头
我正在尝试下一个代码Android FC尝试操作意图时\u耳机\u插头,android,android-intent,intentfilter,Android,Android Intent,Intentfilter,我正在尝试下一个代码 IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG); Intent headsetState = MainActivity.this.registerReceiver(null, filter); int state = headsetState.getIntExtra("state", -1); String name = headsetState.getStringExtra("name"
IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
Intent headsetState = MainActivity.this.registerReceiver(null, filter);
int state = headsetState.getIntExtra("state", -1);
String name = headsetState.getStringExtra("name");
我的问题是,当我在手机刚启动时尝试该方法时,headsetState的意图值为NULL,并且应用程序关闭。但是,如果我把耳机插入/拔出手机,应用程序似乎可以工作,headsetState有一个值
我的问题是为什么这个“意图”一开始是空的,应该有一个值,不是吗
谢谢您的建议。来自,注册接收者
返回
找到的第一个粘滞意图与筛选器匹配,如果没有,则为null
因为没有找到粘性意图,所以它返回null。你没有正确使用它。返回值旨在帮助您捕获在注册接收器之前到达的任何粘性广播,并且注册接收器(您未提供)将在
onReceive()
中处理未来事件。谢谢,我不知道intent的确切功能。