Android平台实现卡1卡2铃声分开设置

Android从5.0之后开始支持双卡,但手机铃声不能分开设置,要实现此功能,可参考默认铃声增加一项,来电时根据sim卡获取对应的设置项即可。

文件frameworks/base/core/java/android/provider/Settings.java
中定义了铃声key以及对应的URI,铃声备份文件以及对应的URI。
参考默认铃声增加如下代码:

//卡2铃声key,保存在文件/data/system/users/0/settings_system.xml中
	public static final String RINGTONE2 = "ringtone2";	
	public static final Uri DEFAULT_RINGTONE_URI2 = getUriFor(RINGTONE2);
	
//卡2铃声备份文件,保存在/data/system_de/0/ringtones/ringtone_cache2中,
//正因为有此备份文件在,当设置T卡铃声,拔除T卡,铃声不变
        public static final String RINGTONE_CACHE2 = "ringtone_cache2";
        public static final Uri RINGTONE_CACHE_URI2 = getUriFor(RINGTONE_CACHE2);

文件frameworks/base/media/java/android/media/RingtoneManager.java
向应用层提供获取铃声的接口,定义铃声的类型,根据类型获得铃声URI等。
参考默认铃声增加如下代码:

   /**
     * Type that refers to sounds that are used for the phone ringer.
     */
    public static final int TYPE_RINGTONE = 1;
    
    /**
     * Type that refers to sounds that are used for notifications.
     */
    public static final int TYPE_NOTIFICATION = 2;
    
    /**
     * Type that refers to sounds that are used for the alarm.
     */
    public static final int TYPE_ALARM = 4;

//增加一种类型,注意取值按照位
    /**
     * Type for SIM2 ringtone
     * @hide
     * @internal
     */
    public static final int TYPE_RINGTONE2 = 8;

    private static String getSettingForType(int type) {
        if ((type & TYPE_RINGTONE) != 0) {
            return Settings.System.RINGTONE;
+        } else if ((type & TYPE_RINGTONE2) != 0) {
+            return Settings.System.RINGTONE2;
        } else if ((type & TYPE_NOTIFICATION) != 0) {
            return Settings.System.NOTIFICATION_SOUND;
        } else if ((type & TYPE_ALARM) != 0) {
            return Settings.System.ALARM_ALERT;
        } else {
            return null;
        }
    }

    public static Uri getCacheForType(int type) {
        if ((type & TYPE_RINGTONE) != 0) {
            return Settings.System.RINGTONE_CACHE_URI;
+        } else if ((type & TYPE_RINGTONE2) != 0) {
+            return Settings.System.RINGTONE_CACHE_URI2;
        } else if ((type & TYPE_NOTIFICATION) != 0) {
            return Settings.System.NOTIFICATION_SOUND_CACHE_URI;
        } else if ((type & TYPE_ALARM) != 0) {
            return Settings.System.ALARM_ALERT_CACHE_URI;
        } else {
            return null;
        }
    }

    public static int getDefaultType(Uri defaultRingtoneUri) {
        if (defaultRingtoneUri == null) {
            return -1;
        } else if (defaultRingtoneUri.equals(Settings.System.DEFAULT_RINGTONE_URI)) {
            return TYPE_RINGTONE;
        } else if (defaultRingtoneUri.equals(Settings.System.DEFAULT_NOTIFICATION_URI)) {
            return TYPE_NOTIFICATION;
        } else if (defaultRingtoneUri.equals(Settings.System.DEFAULT_ALARM_ALERT_URI)) {
            return TYPE_ALARM;
+        } else if (defaultRingtoneUri.equals(Settings.System.DEFAULT_RINGTONE_URI2)) {
+            return TYPE_RINGTONE2;
        } else {
            return -1;
        }
    }
    
    public static Uri getDefaultUri(int type) {
     
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值