OPhone设置、获取振铃和短信音

public static void setRingForOPhone(ContentResolver cr, Uri uri)
	throws ClassNotFoundException, SecurityException, NoSuchMethodException, 
	IllegalArgumentException, IllegalAccessException, InvocationTargetException {
	Class cls = Class.forName("android.provider.Settings$Profile");
	Method method = cls.getMethod("setRingTone", new Class[] {ContentResolver.class, Uri.class});
	method.invoke(cls, new Object[] {cr, uri});
}

public static Uri getRingtoneForOPhone(Context c)
		throws ClassNotFoundException, SecurityException, NoSuchMethodException, 
		IllegalArgumentException, IllegalAccessException, InvocationTargetException  {
	Class cls = Class.forName("android.provider.Settings$Profile");
	Method getRingTone = cls.getMethod("getRingTone", new Class[] {ContentResolver.class, String.class});
	String colName = "line1_ringtone";
	return (Uri)getRingTone.invoke(cls, new Object[]{c.getContentResolver(), colName});
}

/***
 * 设置短信提示音
 */
public static void setNotificationForOPhone(Context c, Uri uri) 
		throws ClassNotFoundException, SecurityException, NoSuchMethodException, 
		IllegalArgumentException, IllegalAccessException, InvocationTargetException {
	String[] sProjection = { "_id", "name", "ringmode", "volume", "line1_ringtone", "line2_ringtone", "message_alert", "email_alert", "calendar_alert", "alarm_alert", "reminder_alert", "fetion_alert", "_preload", "_airplane" };
	int currentprofileid = 0;
	Class cls = Class.forName("android.provider.Settings$System");
	Method method = cls.getMethod("getInt", new Class[] {ContentResolver.class, String.class, int.class});
	currentprofileid = (Integer)method.invoke(cls, new Object[] {c.getContentResolver(), "current_profile", 1});
	Uri url = ContentUris.withAppendedId(Uri.parse("content://settings/profile"), currentprofileid);
	
	// cursor:
	Cursor cursor = c.getContentResolver().query(url, sProjection, null, null, null);
	ContentValues paramContentValues = new ContentValues(1);
	paramContentValues.put("message_alert", uri.toString());
	if (null != cursor && cursor.moveToFirst()) {
		c.getContentResolver().update(url, paramContentValues, null, null);
	}
	if (null != cursor) {
		cursor.close();
	}
}

public static Uri getSMSAlertForOPhone(Context c) 
		throws ClassNotFoundException, SecurityException, NoSuchMethodException, 
		IllegalArgumentException, IllegalAccessException, InvocationTargetException {
	String[] sProjection = { "_id", "name", "ringmode", "volume", "line1_ringtone", "line2_ringtone", "message_alert", "email_alert", "calendar_alert", "alarm_alert", "reminder_alert", "fetion_alert", "_preload", "_airplane" };
	int currentprofileid = 0;
	Class cls = Class.forName("android.provider.Settings$System");
	Method method = cls.getMethod("getInt", new Class[] {ContentResolver.class, String.class, int.class});
	currentprofileid = (Integer)method.invoke(cls, new Object[] {c.getContentResolver(), "current_profile", 1});
	Uri uri = ContentUris.withAppendedId(Uri.parse("content://settings/profile"), currentprofileid);
	
	// cursor:
	Cursor cursor = c.getContentResolver().query(uri, sProjection, null, null, null);
	if (null != cursor) {
		if (cursor.moveToFirst()) {
			String url = cursor.getString(6);
			if (null != url) {
				return Uri.parse(url);
			}
		}
		cursor.close();
	}
	return null;
}
其实android.provider.Settings$System类并不需要反射,因为系统已经提供了,只是些代码的时候使用了,后来也没有修改。
android.provider.Settings$System
android.provider.Settings$System

你可能感兴趣的:(OPhone设置、获取振铃和短信音)