关于设置为Android铃声的问题

时间:2014-02-20 11:21:36

标签: android listview ringtone

第一。我有一个代码设置为铃声。有一点我不明白。我的应用程序中有64个声音文件。当我点击某个按钮或者我必须为所有64个文件编写完整的代码时,有没有办法获得类似ID的东西?

第二。我的应用程序中有一个列表视图,我希望长项目单击将该文件设置为铃声。 现在。我是否需要将整个代码写入案例,或者我可以进行另一项活动,然后使用这种方式设置铃声?像这样的东西

case 1:
    RingtoneManager.setActualDefaultRingtoneUri(
          myActivity,
          Activity name.TYPE_RINGTONE,
          newUri
break;

那就是它,是否有可能从文件中获取ID,复制该文件并在长按时将其设置为铃声,或者我需要为所有64个文件编写代码? 编辑:如果第一个问题不可能,我应该如何为所有64个文件编写代码?把它们放在哪里?如何在单击某个按钮时定义要复制的文件。或列表视图

我的代码是:

l.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                String selectedName = ((ListView) view.findViewById(R.id.listView1)).getContext().toString();

                AssetManager assetManager = getAssets();

                File file = new File(Environment.getExternalStorageDirectory(),
                        "/myRingtonFolder/Audio/");
                if (!file.exists()) {
                    file.mkdirs();
                }

                String path = Environment.getExternalStorageDirectory()
                        .getAbsolutePath() + "/MyRingtones/Audio/";

                File out = new File(path + "/", selectedName);     
                if(!out.exists()){
                    copyFile(assetManager, "Yeah.mp3", out);
                }           

                ContentValues values = new ContentValues();
                values.put(MediaStore.MediaColumns.DATA, out.getAbsolutePath());
                values.put(MediaStore.MediaColumns.TITLE, "yeah");
                values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
                values.put(MediaStore.MediaColumns.SIZE, out.length());
                values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
                values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
                values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
                values.put(MediaStore.Audio.Media.IS_ALARM, true);
                values.put(MediaStore.Audio.Media.IS_MUSIC, true);

                Uri uri = MediaStore.Audio.Media.getContentUriForPath(out.getAbsolutePath());
                ContentResolver mCr = getContentResolver();
                Uri newUri = mCr.insert(uri, values);

                try {
                    RingtoneManager.setActualDefaultRingtoneUri(
                             getApplicationContext(), RingtoneManager.TYPE_RINGTONE, newUri);
                    Settings.System.putString(mCr, Settings.System.RINGTONE,
                            newUri.toString());
                } 
                catch (Throwable t) 
                {
                    //TODO Handle exception
                }
                switch (position){
                case 0:
                     if(mp!=null)
                        {
                            mp.release();
                            mp=null;
                        }

                     MediaPlayer.create(MainActivity.this, R.raw.aint);
                        mp.start();
                    break;
                case 1:
                     if(mp!=null)
                        {
                            mp.release();
                            mp=null;
                        }

                        mp = MediaPlayer.create(MainActivity.this,
                                R.raw.airh);
                        mp.start();

logcat的:

02-25 09:21:34.064: W/dalvikvm(10221): threadid=1: thread exiting with uncaught exception (group=0x41aac8b0)
02-25 09:21:34.074: E/AndroidRuntime(10221): FATAL EXCEPTION: main
02-25 09:21:34.074: E/AndroidRuntime(10221): java.lang.NullPointerException
02-25 09:21:34.074: E/AndroidRuntime(10221):    at funnysounds.com.MainActivity$1.onItemClick(MainActivity.java:63)
02-25 09:21:34.074: E/AndroidRuntime(10221):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
02-25 09:21:34.074: E/AndroidRuntime(10221):    at android.widget.AbsListView.performItemClick(AbsListView.java:1104)
02-25 09:21:34.074: E/AndroidRuntime(10221):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2792)
02-25 09:21:34.074: E/AndroidRuntime(10221):    at android.widget.AbsListView$1.run(AbsListView.java:3468)
02-25 09:21:34.074: E/AndroidRuntime(10221):    at android.os.Handler.handleCallback(Handler.java:730)
02-25 09:21:34.074: E/AndroidRuntime(10221):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-25 09:21:34.074: E/AndroidRuntime(10221):    at android.os.Looper.loop(Looper.java:213)
02-25 09:21:34.074: E/AndroidRuntime(10221):    at android.app.ActivityThread.main(ActivityThread.java:5225)
02-25 09:21:34.074: E/AndroidRuntime(10221):    at java.lang.reflect.Method.invokeNative(Native Method)
02-25 09:21:34.074: E/AndroidRuntime(10221):    at java.lang.reflect.Method.invoke(Method.java:525)
02-25 09:21:34.074: E/AndroidRuntime(10221):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
02-25 09:21:34.074: E/AndroidRuntime(10221):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
02-25 09:21:34.074: E/AndroidRuntime(10221):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

我不会使用开关,而是放置一个点击监听器:

listview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

            // selected item
           String selectedName = ((TextView) view.findViewById(R.id.your_textView_item_id)).getText().toString();

            AssetManager assetManager = getAssets();

            File file = new File(Environment.getExternalStorageDirectory(),
                    "/myRingtonFolder/Audio/");
            if (!file.exists()) {
                file.mkdirs();
            }

            String path = Environment.getExternalStorageDirectory()
                    .getAbsolutePath() + "/MyRingtones/Audio/";

            File out = new File(path + "/", selectedName);     
            if(!out.exists()){
                try {
                    copyFile(assetManager, "Yeah.mp3", out);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }           

            ContentValues values = new ContentValues();
            values.put(MediaStore.MediaColumns.DATA, out.getAbsolutePath());
            values.put(MediaStore.MediaColumns.TITLE, "yeah");
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
            values.put(MediaStore.MediaColumns.SIZE, out.length());
            values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
            values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
            values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
            values.put(MediaStore.Audio.Media.IS_ALARM, true);
            values.put(MediaStore.Audio.Media.IS_MUSIC, true);

            Uri uri = MediaStore.Audio.Media.getContentUriForPath(out.getAbsolutePath());
            ContentResolver mCr = getContentResolver();
            Uri newUri = mCr.insert(uri, values);

            try {
                RingtoneManager.setActualDefaultRingtoneUri(this,    RingtoneManager.TYPE_RINGTONE, newUri);
                Settings.System.putString(mCr, Settings.System.RINGTONE,
                        newUri.toString());
            } 
            catch (Throwable t) 
            {
                //TODO Handle exception
            }

        }
      });