利用java 反射 设置系统铃音
系统来电的铃音app是无法修改的
//android/frameworks/base/media/java/android/media/Ringtone.java
@UnsupportedAppUsage
private MediaPlayer mLocalPlayer;
private final MyOnCompletionListener mCompletionListener = new MyOnCompletionListener();
@UnsupportedAppUsage
private Uri mUri;
private String mTitle;
在此 尝试利用反射来对铃音设置进行修改
播放系统默认来电铃声
public static void playDefaultCallMediaPlayer(Context context){
try {
if(r== null) {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
r = RingtoneManager.getRingtone(context, notification);
r.setStreamType(AudioManager.STREAM_RING);
setRingtoneRepeat(r);
}
r.play();
}catch (Exception e){
e.printStackTrace();
}
}
设置铃音重复播放
private static void setRingtoneRepeat(Ringtone ringtone) {
Class<Ringtone> clazz =Ringtone.class;
try {
Field field = clazz.getDeclaredField("mLocalPlayer");
field.setAccessible(true);
MediaPlayer target = (MediaPlayer) field.get(ringtone);
target.setLooping(true);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
总结
遇到问题,多参考竞品