Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android FC尝试操作意图时\u耳机\u插头_Android_Android Intent_Intentfilter - Fatal编程技术网

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的确切功能。