Android RingtoneManager 铃声管理

    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;  
        }  
    }  


Java代码  收藏代码

    <?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>  


Java代码  收藏代码

        <?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>  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值