铃声到特定的联系

时间:2013-12-03 19:16:45

标签: java android audio ringtone

编辑:1

我正在创建一个带声音的应用程序并实现一些代码我遇到以下错误将声音设置为特定联系人

这是代码:

       Button b7 = (Button)findViewById(R.id.b7);
        b7.setOnLongClickListener(new View.OnLongClickListener() {
        public boolean onLongClick(View v) {
         {

        if (click){
        cp.showAtLocation(v, Gravity.CENTER, 0, 0);
        cp.update(0,0,500,500);
        click=false;
        }else{
        cp.dismiss();
        click=true;
        }}
      return click;}});
       b7.setOnClickListener(new Button.OnClickListener() { 


         public void onClick(View v){




       File folder = new File(Environment.getExternalStorageDirectory() + "/Bizzsound/");
         boolean success = false;
         if (!folder.exists()) {
          success = folder.mkdirs();
         }
         if (!success) {
        } else {
      }



      File direct = new File("/sdcard/Bizzsound/");

      if (!direct.exists()) {
      direct.mkdirs();
        }
       byte[] buffer=null;  
       InputStream fIn = getBaseContext().getResources().openRawResource(R.raw.b1); 
       File dir1 = new File ("/sdcard/Bizzsound/");
         File f1 = new File(dir1, String.format("b1.mp3"));
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int size=0;  

        try {  
        size = fIn.available();  
         buffer = new byte[size];  
         fIn.read(buffer);  
       fIn.close();  
        } catch (IOException e) {  
      // TODO Auto-generated catch block
            }
     try {
    FileOutputStream fos = new FileOutputStream(f1);
        fos.write(buffer);  
       fos.flush();
      fos.close();
       bos.close();


   } catch (IOException e1) {
    // TODO Auto-generated catch block
       e1.printStackTrace();
      }


      File k = new File("/sdcard/Bizzsound/", "b1.mp3");

        ContentValues values = new ContentValues();
      values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
       values.put(MediaStore.MediaColumns.TITLE, "b1.mp3");
       values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
         values.put(AudioColumns.ARTIST, "artist");
        values.put(AudioColumns.IS_RINGTONE, true);
         values.put(AudioColumns.IS_NOTIFICATION, true);
         values.put(AudioColumns.IS_ALARM, true);
         values.put(AudioColumns.IS_MUSIC, false);

        Intent intent = new Intent(Intent.ACTION_PICK,
        ContactsContract.Contacts.CONTENT_URI);
        startActivityForResult(intent, PICK_CONTACT);





         mpButton1 = MediaPlayer.create(getBaseContext(),R.raw.b1);
         mpButton1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
         @Override
         public void onPrepared(MediaPlayer mpButton1) {
        // TODO Auto-generated method stub

       mpButton1.start();

       mpButton1.setOnCompletionListener(new OnCompletionListener() {
        public void onCompletion(MediaPlayer mpButton1) {
        mpButton1.release();

        };
        });
          }
       });



   }});










            }






 @Override
   public void onActivityResult(int reqCode, int resultCode, Intent data) {
  super.onActivityResult(reqCode, resultCode, data);

   switch (reqCode) {
   case (PICK_CONTACT):
   if (resultCode == Activity.RESULT_OK) {

    Uri contactData = data.getData();
     Cursor c = managedQuery(contactData, null, null, null, null);
      if (c.moveToFirst()) {

       String id = c
        .getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

     Uri localUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, id);

     File k = new File("/sdcard/Bizzsound/", "b1.mp3");
    ContentValues localContentValues = new ContentValues();

String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));



    if (hasPhone.equalsIgnoreCase("1")) {

       Cursor phones = getContentResolver().query  
(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id, null, null);
     phones.moveToFirst();

String cNumber = phones.getString(phones //error at this line
.getColumnIndex("b1.mp3"));
 System.out.println("number is:" + cNumber);
     }

 String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String SDCardRoot1 = (Environment.getExternalStorageDirectory() + "/b1.mp3");


localContentValues.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
                                localContentValues.put(MediaStore.MediaColumns.TITLE,"b1.mp3");
                                localContentValues.put(MediaStore.MediaColumns.MIME_TYPE,"audio/*");
localContentValues.put(MediaStore.Audio.Media.ARTIST, "");
                                localContentValues.put(MediaStore.Audio.Media.IS_RINGTONE, true);
localContentValues.put(AudioColumns.IS_NOTIFICATION, true);
localContentValues.put(AudioColumns.IS_ALARM, true);
localContentValues.put(AudioColumns.IS_MUSIC, false);
                                localContentValues.put(ContactsContract.Data.RAW_CONTACT_ID, id);
                                localContentValues.put(ContactsContract.Data.CUSTOM_RINGTONE, SDCardRoot1);
 getContentResolver().update(localUri, localContentValues,
null, null);
 Toast.makeText(this, "Ringtone assigned to: " + name, 0).show();
                            }
                        }
                        break;
                    }
                }

        }

现在,当接到来自联系人的电话并且收到此错误时,手机会冻结: 进程com.android.phone已经停止

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.bizzsound1"
    android:versionCode="1"
    android:versionName="1.0" >

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

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.GET_TASKS"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:largeHeap="true" >



        <activity
            android:name="org.bizzsound1.MainActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.SEND"/>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>




        <activity     
            android:name="org.bizzsound1.menu"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >

            <intent-filter>
                <action android:name="org.bizzsound1.MENU" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>











             <activity android:name="org.bizzsound1.Disclamer"
                 android:screenOrientation="portrait">
             </activity>


 <activity android:name="com.startapp.android.eula.EULAActivity"
android:theme="@android:style/Theme.Translucent"
android:configChanges="keyboard|keyboardHidden|orientation" />

         <activity android:name="com.startapp.android.publish.list3d.List3DActivity"
android:taskAffinity="altervista.org.gapplication1.AppWall"
android:theme="@android:style/Theme" />
<activity android:name="com.startapp.android.publish.AppWallActivity"
android:theme="@android:style/Theme.Translucent" android:taskAffinity="altervista.org.gapplication1.AppWall"
android:configChanges="orientation|keyboardHidden" />




<activity android:name="com.google.ads.AdActivity"
                  android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>






    </application>





</manifest>

0 个答案:

没有答案