我制作了一个音乐播放器应用程序,并在电话被调用时暂停歌曲,我使用此代码。
但这需要 READ_PHONE_STATE 权限,这可能会吓跑一些用户,所以我想知道是否可以通过另一种方式实现同样的事情而不需要此权限?
我的代码
private void callStateListener(){
//incomingCallPause: checkbox value if user wants to pause when there is an incoming call.
incomingCallPause = storageUtil.loadSwitchOnCall();
mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
mPhoneStateListener = new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_OFFHOOK:
if (mediaPlayer != null){
pauseSong();
}
break;
case TelephonyManager.CALL_STATE_RINGING:
if (incomingCallPause && mediaPlayer != null) {
pauseSong();
NotificationBuilder(PlaybackStatus.PAUSED);
incomingCall = true;
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if (mediaPlayer != null) {
if (incomingCall) {
incomingCall = false;
NotificationBuilder(PlaybackStatus.PLAYING);
if (!mediaPlayer.isPlaying()){
mediaPlayer.start();
}
}
}
break;
}
super.onCallStateChanged(state, incomingNumber);
}
};
最佳答案
根据 Android 官方文档,您需要定义 list 权限才能使用 TelephonyManager。
需要 Manifest.permission.READ_PHONE_STATE
推荐链接: https://developer.android.com/reference/android/telephony/TelephonyManager
关于java - 无需 READ_PHONE_STATE 权限即可使用 TelephonyManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57618903/