Android RingtoneManager铃声管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 package com.Aina.Android; import java.io.File; import android.app.Activity; import android.content.Intent; import android.media.RingtoneManager; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.widget.Button; public class Test extends Activity { /** Called when the activity is first created. */ private Button btn1 = null ; private Button btn2 = null ; private Button btn3 = null ; private static final int Ringtone = 0 ; private static final int Alarm = 1 ; private static final int Notification = 2 ; private static final String FileRingtone = Environment .getExternalStorageDirectory() + "/music/ringtones" ; private static final String FileAlarm = Environment .getExternalStorageDirectory() + "/music/alarms" ; private static final String FileNotification = Environment .getExternalStorageDirectory() + "/music/notifications" ; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); btn1 = (Button) this .findViewById(R.id.Button01); btn2 = (Button) this .findViewById(R.id.Button02); btn3 = (Button) this .findViewById(R.id.Button03); btn1.setOnClickListener( new Button.OnClickListener() { public void onClick(View arg0) { // TODO Auto-generated method stub if (isFile(FileRingtone)) { // 打开系统铃声设置 Intent intent = new Intent( RingtoneManager.ACTION_RINGTONE_PICKER); // 设置类型为来电 intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE); // 设置显示的标题 intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "设置来电铃声" ); startActivityForResult(intent, Ringtone); } } }); btn2.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub if (isFile(FileAlarm)) { Intent intent = new Intent( RingtoneManager.ACTION_RINGTONE_PICKER); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_ALARM); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "设置闹钟铃声" ); startActivityForResult(intent, Alarm); } } }); btn3.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub if (isFile(FileNotification)) { Intent intent = new Intent( RingtoneManager.ACTION_RINGTONE_PICKER); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "设置通知铃声" ); startActivityForResult(intent, Notification); } } }); } /** * 设置铃声之后的回调函数 */ @Override protected void onActivityResult( int requestCode, int resultCode, Intent data) { super .onActivityResult(requestCode, resultCode, data); if (resultCode != RESULT_OK) { return ; } else { // 得到我们选择的铃声 Uri uri = data .getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); if (uri != null ) { switch (requestCode) { case Ringtone: RingtoneManager.setActualDefaultRingtoneUri( this , RingtoneManager.TYPE_RINGTONE, uri); break ; case Alarm: RingtoneManager.setActualDefaultRingtoneUri( this , RingtoneManager.TYPE_ALARM, uri); break ; case Notification: RingtoneManager.setActualDefaultRingtoneUri( this , RingtoneManager.TYPE_NOTIFICATION, uri); break ; default : break ; } } } } /** * 判断文件是否存在,不存在则创建. * * @param path * @return */ private boolean isFile(String path) { boolean b = false ; File f = new File(path); if (f.exists()) { b = true ; } else { if (f.mkdirs()) { b = true ; } else { b = false ; } } return b; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <? xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> < TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> < Button android:text="设置来电铃声" android:id="@+id/Button01" android:layout_width="fill_parent" android:layout_height="wrap_content"> </ Button > < Button android:text="设置闹钟铃声" android:id="@+id/Button02" android:layout_width="fill_parent" android:layout_height="wrap_content"> </ Button > < Button android:text="设置通知铃声" android:id="@+id/Button03" android:layout_width="fill_parent" android:layout_height="wrap_content"> </ Button > </ LinearLayout > 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <? xml version="1.0" encoding="utf-8"?> < manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.Aina.Android" android:versionCode="1" android:versionName="1.0"> < application android:icon="@drawable/icon" android:label="@string/app_name"> < activity android:name=".Test" android:label="@string/app_name"> < intent-filter > < action android:name="android.intent.action.MAIN" /> < category android:name="android.intent.category.LAUNCHER" /> </ intent-filter > </ activity > </ application > < uses-permission android:name="android.permission.INTERNET" /> < uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> </ manifest > 本文转自java豆子博客园博客,原文链接:http://www.cnblogs.com/error404/archive/2011/08/04/2127233.html,如需转载请自行联系原作者