首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ACTION_HEADSET_PLUG不工作的接口棒棒糖

ACTION_HEADSET_PLUG不工作的接口棒棒糖
EN

Stack Overflow用户
提问于 2018-11-30 05:18:23
回答 1查看 322关注 0票数 0

我正在使用这个代码来检测耳机的插入/拔出,在APIAPI24中工作得很好,但是当在API21,22,23中尝试它时,崩溃或者没有检测到任何东西。你可以帮我

代码语言:javascript
复制
public class MainActivity extends Activity  {
private static final String TAG = "MainActivity";
private MusicIntentReceiver myReceiver;

@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myReceiver = new MusicIntentReceiver();
}

@Override public void onResume() {
    IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    registerReceiver(myReceiver, filter);
    super.onResume();
}

private class MusicIntentReceiver extends BroadcastReceiver {
    @Override public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
            int state = intent.getIntExtra("state", -1);
            switch (state) {
            case 0:
                Log.d(TAG, "Headset is unplugged");
                break;
            case 1:
                Log.d(TAG, "Headset is plugged");
                break;
            default:
                Log.d(TAG, "I have no idea what the headset state is");
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-12-01 03:37:51

解决问题!只是不要在android模拟器上工作,在真正的手机上工作很好,谢谢!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53547751

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档