项目中遇到这样的需求,要求监听耳机插拔状态变化,进行埋点上报处理,把实现代码分享一下。
public class MainActivity extends AppCompatActivity { private static final String TAG = MainActivity.class.getSimpleName(); private boolean isHeadsetOn; private boolean isRegisteredAudioReceiver = false; private BroadcastReceiver INSTANCE; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override protected void onResume() { super.onResume();