android 8.0中将静态广播删除了,不再生效,改用动态注册广播方式,电话监听方式
//权限: <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> //android.permission.READ_PHONE_STATE权限需要申请用户通过。 /** * @author xuyan QQ:1213236113 * @name eng100 * @class name:com.wanhe.eng100.listening.pro.common * @class describe * @time 2018/5/22 12:33 */ public class PhoneReceiver extends BroadcastReceiver { private OnPhoneListener onPhoneListener; public PhoneReceiver() { } public PhoneReceiver(OnPhoneListener onPhoneListener) { this.onPhoneListener = onPhoneListener; } /** * @param context The Context in which the receiver is running. * @param intent The Intent being received. */ @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (Intent.ACTION_NEW_OUTGOING_CALL.equals(action)) { //电话监听 if (onPhoneListener != null) onPhoneListener.onPhoneResume(); } else { TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE); tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE); } } public interface OnPhoneListener { void onPhoneResume(); void onPhoneIdle(); } PhoneStateListener listener = new PhoneStateListener() { @Override public void onCallStateChanged(int state, String incomingNumber) { //方法必须写在super方法后面,否则incomingNumber无法获取到值。 super.onCallStateChanged(state, incomingNumber); switch (state) { case TelephonyManager.CALL_STATE_IDLE: LogUtils.i("电话挂断了"); if (onPhoneListener != null) onPhoneListener.onPhoneIdle(); break; case TelephonyManager.CALL_STATE_OFFHOOK: LogUtils.i("电话接听"); break; case TelephonyManager.CALL_STATE_RINGING: //输出来电号码 LogUtils.i("电话响铃:来电号码" + incomingNumber); if (onPhoneListener != null) onPhoneListener.onPhoneResume(); break; } } }; } /** * 注册电话监听 */ private void registerPhoneReceiver(){ if(phoneReceiver==null) { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("android.intent.action.PHONE_STATE"); intentFilter.addAction(Intent.ACTION_NEW_OUTGOING_CALL); phoneReceiver = new PhoneReceiver(new PhoneReceiver.OnPhoneListener() { @Override public void onPhoneResume() { //暂停音乐 pause(); } @Override public void onPhoneIdle() { //开始播放音乐 start(); } }); registerReceiver(phoneReceiver, intentFilter); } } /** * 解注册电话监听 */ private void unRegisterPhoneReceiver(){ if(phoneReceiver!=null){ unregisterReceiver(phoneReceiver); } }
锁屏监听
/** * @author xuyan QQ:1213236113 * @name eng100 * @class name:com.wanhe.eng100.listening.pro.common * @class describe * @time 2018/5/14 21:19 */ public class ScreenListener { private Context mContext; private ScreenBroadcastReceiver mScreenReceiver; private ScreenStateListener mScreenStateListener; public ScreenListener(){} public ScreenListener(Context context) { mContext = context; mScreenReceiver = new ScreenBroadcastReceiver(); } /** * screen状态广播接收者 */ private class ScreenBroadcastReceiver extends BroadcastReceiver { private String action = null; @Override public void onReceive(Context context, Intent intent) { action = intent.getAction(); if (Intent.ACTION_SCREEN_ON.equals(action)) { // 开屏 mScreenStateListener.onScreenOn(); } else if (Intent.ACTION_SCREEN_OFF.equals(action)) { // 锁屏 mScreenStateListener.onScreenOff(); } else if (Intent.ACTION_USER_PRESENT.equals(action)) { // 解锁 mScreenStateListener.onUserPresent(); } } } /** * 开始监听screen状态 * * @param listener */ public void begin(ScreenStateListener listener) { mScreenStateListener = listener; registerListener(); getScreenState(); } /** * 获取screen状态 */ private void getScreenState() { PowerManager manager = (PowerManager) mContext .getSystemService(Context.POWER_SERVICE); if (manager.isScreenOn()) { if (mScreenStateListener != null) { mScreenStateListener.onScreenOn(); } } else { if (mScreenStateListener != null) { mScreenStateListener.onScreenOff(); } } } /** * 停止screen状态监听 */ public void unregisterListener() { mContext.unregisterReceiver(mScreenReceiver); mScreenStateListener = null; } /** * 启动screen状态广播接收器 */ private void registerListener() { IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_USER_PRESENT); mContext.registerReceiver(mScreenReceiver, filter); } public interface ScreenStateListener {// 返回给调用者屏幕状态信息 public void onScreenOn(); public void onScreenOff(); public void onUserPresent(); } } //开启屏幕常亮 powerManager = (PowerManager) this.getSystemService(POWER_SERVICE); if (powerManager != null) { wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, this.getClass().getName()); } //注册锁屏监听 screenListener = new ScreenListener(UIUtils.getContext()); screenListener.begin(new ScreenListener.ScreenStateListener() { @Override public void onScreenOn() { //开屏,播放音频,被回调多次 } @Override public void onScreenOff() { //锁屏,暂停音频 pauseActionAudio(); } @Override public void onUserPresent() { //解屏,被回调多次 } });