通过代码更改系统警报声

时间:2013-11-13 02:17:46

标签: android

我想更改默认系统alam声音,我想使用:

Uri uri =Uri.parse("android.resource://com.aa.Uhome9110/raw/"+"alarm_buzzer");
RingtoneManager.setActualDefaultRingtoneUri(EditRingActivity.this, RingtoneManager.TYPE_ALARM, uri);

但没有效果。

我用过:

Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
                                RingtoneManager.TYPE_ALARM);
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "alarm");
                startActivityForResult(intent, 2);

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data)
 {
         super.onActivityResult(requestCode, resultCode, data);
         if (resultCode != RESULT_OK)
         {
                 return;
         }
         Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
         Log.i("uri ","uri11 "+uri.toString());

         if (uri != null)
         {
                 switch (requestCode)
                 {
                         case 1:

                                 RingtoneManager.setActualDefaultRingtoneUri(this,
                                                 RingtoneManager.TYPE_RINGTONE, uri);

                                 break;
                         case 2:
                                 RingtoneManager.setActualDefaultRingtoneUri(this,
                                                 RingtoneManager.TYPE_ALARM, uri);

                                 break;
                         case 3:
                                 RingtoneManager.setActualDefaultRingtoneUri(this,
                                                 RingtoneManager.TYPE_NOTIFICATION, uri);


                 }
         }
 }

一切都好,我认为我的uri是错的,所以我修改了:

    Uri uri =Uri.parse("content://media/internal/audio/media/4");
RingtoneManager.setActualDefaultRingtoneUri(EditRingActivity.this, RingtoneManager.TYPE_ALARM, uri);

但没用。 我不认为使用startActivityForResult(intent,2)方法,我想直接改变系统报警,你能给我一些建议吗?

1 个答案:

答案 0 :(得分:0)

请尝试以下代码,它对我来说运作正常,请您确认亲爱的朋友,您是否使用过我在此处使用的清单文件中的权限?

使用RingtoneManager.setActualDefaultRingtoneUri时需要

写设置权限

活动文件:

package com.example.testapp;

import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.AlarmClock;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String uri = null;
        // Just call the RINGTONE PICKER by using the Intent
        Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
                RingtoneManager.TYPE_NOTIFICATION);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");

        if (uri != null) {
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,
                    Uri.parse(uri));
        } else {
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,
                    (Uri) null);
        }
        startActivityForResult(intent, 0);
    }

    protected void onActivityResult(int requestCode, int resultCode,
            Intent intent) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, intent);
        if (resultCode == RESULT_OK) {
            Uri uri = intent
                    .getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
            if (uri != null) {
                @SuppressWarnings("unused")
                String ringTonePath = uri.toString();
                Log.d("Ringtone Path",""+ ringTonePath);        
                // Set the Ringtone to Alarm 
                RingtoneManager.setActualDefaultRingtoneUri(MainActivity.this, RingtoneManager.TYPE_ALARM, uri);

                // I am checking wether the selected ringtone is set for alarm or not

                Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
                i.putExtra(AlarmClock.EXTRA_HOUR, 12); // 12 pm
                i.putExtra(AlarmClock.EXTRA_MINUTES, 26); // 26 minutes
                startActivity(i);
            }
        }
    }
}

清单文件:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.testapp.MainActivity"
        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>