0

在这个问题中,实际上我想更改通知声音。为此,我创建了一个自定义声音对话框,通过它我可以更改和设置不同的铃声。但问题是这个声音对话框存在于不同的活动中,即 SoundActivity 而我在不同的活动中编写通知代码。你能告诉我如何在通知代码中使用这个声音活动,以便通过更改铃声,通知声音也得到改变。我有这个声音活动,我根据我的需求设置铃声。但我希望这些铃声在通知到来时发出哔哔声。我在另一个活动中编写通知代码。你能告诉我如何使用这个声音活动,因此在此活动中所做的更改会更改通知声音???

protected void onCreate(Bundle savedInstanceState) 
        {
            try
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.sound);
                setTitle("Sounds");
                initUpdateView();
                isSystemRingtone=PreferencesClass.getSystemRingtoneStatus(getApplicationContext());
                textviewRingtone = (TextView)findViewById(R.id.textview_ringtone_settings);
                if(PreferencesClass.getRingtone(getApplicationContext()).substring(0, 1).equalsIgnoreCase("/")){
                    String[] son=PreferencesClass.getRingtone(getApplicationContext()).split("/");
                    String[] s=son[son.length-1].split("\\.ogg");
                    String rep=s[0].replace("_", " ");
                    if(rep.startsWith("S ")){
                        rep=rep.substring(2);
                    }
                    textviewRingtone.setText(UppercaseFirstLetters(rep));
                    mysong=son[son.length-1];
                    mysongPath=PreferencesClass.getRingtone(getApplicationContext());
                    if(mysongPath.contains("system")){
                        SelectedTone="Sounds";
                    }
                    else{
                        SelectedTone="My Music";
                    }
                }
                songURI=PreferencesClass.getUri(getApplicationContext());


                LinearLayout layoutRingtone = (LinearLayout)findViewById(R.id.linearlayout_ringtone_settings);
                layoutRingtone.setOnClickListener(new OnClickListener() {


                    public void onClick(View v) {
                        showDialog(RINGTONE_DIALOG_ID);
                    }
                });

            }catch(Exception ex)
            {
                Log.e("ERROR",ex.toString());
            }
        }

        protected Dialog onCreateDialog(int id) 
        {       
            switch(id)
            {


            case RINGTONE_DIALOG_ID:
            {

                int selectedIndex = 1;

                //if(SelectedTone.equalsIgnoreCase("My Music"))
                    //if(SelectedTone.equalsIgnoreCase("")) 
                //  selectedIndex=0;
                Dialog dialog=null;
                //final String[] ringArray=new String[]{"My Music","Sounds"};
                final String[] ringArray=new String[]{"Sounds"};
                if(ringArray.length>0){
                    AlertDialog.Builder builder = new AlertDialog.Builder(SoundActivity.this);
                    builder.setSingleChoiceItems(ringArray, selectedIndex, new DialogInterface.OnClickListener() {


                        public void onClick(DialogInterface dialog, int which) {    

                            if(which==0){
                                isSystemRingtone=1;
                                SelectedTone="Sounds";
                                PreferencesClass.setSystemRingtoneStatus(1, getApplicationContext());
                                Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
                                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Sounds");
                                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
                                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, false);

                                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,RingtoneManager.TYPE_RINGTONE);
                                String uri = songURI;
                                Log.d("uri", uri);
                                //chooses and keeps the selected item as a uri
                                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,999);
                            }

                            dialog.dismiss();
                        }
                    });
                    builder.setTitle("Ringtone");
                    //dialog=new Dialog(SettingsGroup.group, R.style.Theme_D1dialog);
                    dialog=builder.create();
                    dialog.setCanceledOnTouchOutside(true);


                    return dialog;
                }
                else
                    Toast.makeText(getApplicationContext(), "No Data is available", Toast.LENGTH_LONG).show();
            }           

            }
            return super.onCreateDialog(id); 
        }
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            Log.d("req", requestCode+"");
            if (resultCode == RESULT_OK) {
                if (requestCode == RQS_OPEN_AUDIO_MP3) {
                    audioFileUri = data.getData();
                    Log.d("uri", audioFileUri.toString());
                    String son[]=audioFileUri.toString().split("/");
                    int k=0;
                    int id=1;
                    try{
                        id=Integer.parseInt(son[son.length-1]);
                    }
                    catch(Exception e){
                        k=1;
                        mysong=son[son.length-1];
                        mysongPath=audioFileUri.getPath();
                    }
                    //Log.d("id", son[son.length-1]);
                    if(k==0){
                        Uri allaudiosong = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                        String audioID = data.getDataString();
                        Uri MyUri = Uri.parse(audioID);
                        String audioselection = MediaStore.Audio.Media.IS_MUSIC + " !=0 ";
                        Cursor cursor;
                        cursor = managedQuery(MyUri, STAR, null, null, null);
                        int i=0;
                        if (cursor != null) {
                            if (cursor.moveToFirst()) {
                                do {
                                    String song_name = cursor
                                            .getString(cursor
                                                    .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                                    System.out.println("Audio Song Name= "+song_name);
                                    //  mysong[i]=song_name;
                                    int song_id = cursor.getInt(cursor
                                            .getColumnIndex(MediaStore.Audio.Media._ID));
                                    System.out.println("Audio Song ID= "+song_id);
                                    if(id==song_id){
                                        mysong=song_name;
                                        if(SelectedTone.equalsIgnoreCase("My Music")){
                                            textviewRingtone.setText(mysong);
                                        }

                                    }
                                    String fullpath = cursor.getString(cursor
                                            .getColumnIndex(MediaStore.Audio.Media.DATA));
                                    System.out.println("Audio Song FullPath= "+fullpath);
                                    if(id==song_id){
                                        mysongPath=fullpath;
                                        Log.d("path", fullpath);
                                        if(SelectedTone.equalsIgnoreCase("My Music"))
                                            PreferencesClass.setRingtone(mysongPath, SoundActivity.this);
                                    }
                                    String album_name = cursor.getString(cursor
                                            .getColumnIndex(MediaStore.Audio.Media.ALBUM));
                                    System.out.println("Audio Album Name= "+album_name);

                                    int album_id = cursor.getInt(cursor
                                            .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
                                    System.out.println("Audio Album Id= "+album_id);
                                    Log.d("Audio Album Id= ",""+album_id);
                                    String artist_name = cursor.getString(cursor
                                            .getColumnIndex(MediaStore.Audio.Media.ARTIST));
                                    System.out.println("Audio Artist Name= "+artist_name);

                                    int artist_id = cursor.getInt(cursor
                                            .getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
                                    System.out.println("Audio Artist ID= "+artist_id);
                                    i++;
                                } while (cursor.moveToNext());
                            }
                        }



                    } 
                    Log.d("audio",audioFileUri.getPath());

                }
                if(requestCode==999){
                    Log.d("req", requestCode+"");
                    Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
                    try
                    {

                        Uri audioFileUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);

                        Log.d("uri", audioFileUri.toString());
                        String son[]=audioFileUri.toString().split("/");
                        int k=0;
                        int id=1;
                        try{
                            id=Integer.parseInt(son[son.length-1]);
                            Log.d("id", id+"");
                        }
                        catch(Exception e){
                            k=1;

                        }
                        Log.d("K", k+"");
                        if(k==0){
                            Uri allaudiosong = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;
                            Uri MyUri = uri;
                            PreferencesClass.setUri(MyUri.toString(), getApplicationContext());
                            songURI=MyUri.toString();
                            String audioselection = MediaStore.Audio.Media.IS_MUSIC + " !=0 ";
                            Cursor cursor;
                            cursor = managedQuery(MyUri, STAR,null, null, null);
                            int i=0;
                            if (cursor != null) {
                                if (cursor.moveToFirst()) {
                                    do {
                                        String song_name = cursor
                                                .getString(cursor
                                                        .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                                        System.out.println("Audio Song Name= "+song_name);
                                        //  mysong[i]=song_name;
                                        int song_id = cursor.getInt(cursor
                                                .getColumnIndex(MediaStore.Audio.Media._ID));
                                        System.out.println("Audio Song ID= "+song_id);
                                        if(id==song_id){
                                            mysong=song_name;
                                            if(SelectedTone.equalsIgnoreCase("Sounds")){
                                                String[] s=mysong.split("\\.");
                                                String rep=s[0].replace("_", " ");
                                                if(rep.startsWith("S ")){
                                                    rep=rep.substring(2);
                                                }
                                                textviewRingtone.setText(UppercaseFirstLetters(rep));
                                            }
                                        }
                                        String fullpath = cursor.getString(cursor
                                                .getColumnIndex(MediaStore.Audio.Media.DATA));
                                        System.out.println("Audio Song FullPath= "+fullpath);
                                        if(id==song_id){
                                            Log.d("Done", "Done");
                                            //mysongPath=fullpath.substring(4, fullpath.length());
                                            mysongPath=fullpath;
                                            Log.d("path", fullpath);
                                            if(SelectedTone.equalsIgnoreCase("Sounds"))
                                                PreferencesClass.setRingtone(mysongPath, SoundActivity.this);
                                        }
                                        String album_name = cursor.getString(cursor
                                                .getColumnIndex(MediaStore.Audio.Media.ALBUM));
                                        System.out.println("Audio Album Name= "+album_name);

                                        int album_id = cursor.getInt(cursor
                                                .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
                                        System.out.println("Audio Album Id= "+album_id);
                                        Log.d("Audio Album Id= ",""+album_id);
                                        String artist_name = cursor.getString(cursor
                                                .getColumnIndex(MediaStore.Audio.Media.ARTIST));
                                        System.out.println("Audio Artist Name= "+artist_name);

                                        int artist_id = cursor.getInt(cursor
                                                .getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
                                        System.out.println("Audio Artist ID= "+artist_id);
                                        i++;
                                    } while (cursor.moveToNext());
                                }
                            }
                        }
                    }
                    catch (Exception localException)
                    {

                    }
                }
            }  


        }



        public void playSample(String s){
            AssetFileDescriptor afd = null;

            Log.d("RingtoneSelected",PreferencesClass.getRingtone(getApplicationContext()));
            if(!(s.substring(0,1).equalsIgnoreCase("/"))){
                try {
                    if(s==null){
                        afd = getAssets().openFd(PreferencesClass.getRingtone(getApplicationContext())+".mp3");
                    }else{
                        afd = getAssets().openFd(s+".mp3");
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            player = new MediaPlayer();
            try {
                if(s.substring(0, 1).equalsIgnoreCase("/")){
                    player.setDataSource(s);
                }   
                else
                    player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
                player.prepare();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            float volume=(float)Float.valueOf(PreferencesClass.getVolume(getApplicationContext()));
            player.setVolume(volume,volume);
            if(!(s.substring(0, 1).equalsIgnoreCase("/"))){
                player.setLooping(true);
            }
            else
                player.setLooping(false);

            player.start();
            playing=true;

            if(!(s.substring(0, 1).equalsIgnoreCase("/"))){
                Thread thread=new Thread(new Runnable() {


                    public void run() {
                        // TODO Auto-generated method stub
                        try{
                            Thread.sleep(10000);
                        }catch(Exception e){

                        }
                        player.stop();

                    }
                });
                thread.start();
            }
        }
        public static String UppercaseFirstLetters(String str) 
        {
            char[] chars = str.toLowerCase().toCharArray();
            boolean found = false;
            for (int i = 0; i < chars.length; i++) {
                if (!found && Character.isLetter(chars[i])) {
                    chars[i] = Character.toUpperCase(chars[i]);
                    found = true;
                } else if (Character.isWhitespace(chars[i]) || chars[i]=='.' || chars[i]=='\'') { // You can add other chars here
                    found = false;
                }
            }
            return String.valueOf(chars);
        }

    I have this sound activity in which i set the ringtones according to my demand.But i want these ringtones to be beep when notifications come.I write the code for notifictaion in another activity.Can you please tell me how can i use this sound activity,so that changes made in this activity changes the notification sound??


    This is my notifcation code in which code for changing the sound has to be included.Actually in this code in notification code,i have used the mobile ringtone.But i want to use the sound from custom dialog box:

    Intent notificationIntent = new Intent(context, HomeTabActivity.class);
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

     notification.setLatestEventInfo(context, contentTitle, Utils.notiMsg, pendingIntent);
     notification.flags|=notification.FLAG_INSISTENT|notification.FLAG_AUTO_CANCEL;
     notification.defaults |= Notification.DEFAULT_LIGHTS;
     //notification.vibrate=new long[] {100L, 100L, 200L, 500L};
     notificationManager.notify(1, notification);
     Utils.notificationReceived=true;
     Uri  uri  = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

     MediaPlayer mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setDataSource(this, uri);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        final AudioManager audioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
        if(audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION)!=0){
            mediaPlayer.setLooping(false);
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
            try {
                mediaPlayer.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mediaPlayer.start();

        }
     This is the following code which contains notification class as well as sound activity.
4

1 回答 1

0

你什么时候从那个班级启动你的通知?在某些事件或...?

您可以创建一个用于发送通知的服务类,并且每当用户更改通知声音时,您都会提醒您的服务更改下次将使用的声音。

于 2013-06-11T11:35:50.230 回答