呼叫记录 - 不记录来自特定号码的呼叫

时间:2012-12-29 03:52:27

标签: java android broadcastreceiver voice-recording phone-state-listener

这里我想记录来自15555215556的电话,因为我给了IF条件 - “String aa =”15555215556“; if(aa.equals(incomingNumber))”。如果incomingNumber等于aa,它应该开始录制,但不能录制。

公共类记录扩展了BroadcastReceiver {

PhoneStateListener listener; 
TelephonyManager telephony;
MediaRecorder recorder = new MediaRecorder();
boolean recordStarted;
String phonenumber;

@Override
public void onReceive(final Context context, final Intent intent) {
    // TODO Auto-generated method stub

     telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

        listener = new PhoneStateListener() {

            public void onCallStateChanged(int state, String incomingNumber) {

//want to record call from 15555215556
                String aa="15555215556";
            if(aa.equals(incomingNumber)){

               phonenumber=incomingNumber;
               System.out.println(""+phonenumber);

                try{



                switch(state)
                {
                case TelephonyManager.CALL_STATE_IDLE:
                   {
                       Log.d("DEBUG", "IDLE");        
                        StopRecording();
                      break;
                   }

                case TelephonyManager.CALL_STATE_RINGING:
                   {
                       Log.d("DEBUG", "RINGING");
                       StopRecording();
                       break;
                   }
                case TelephonyManager.CALL_STATE_OFFHOOK:
                   {
                        Log.d("DEBUG", "OFFHOOK");      

                        StartRecording();
                          break;
                   }

                }

                }

                catch (Exception e) {
                    // TODO: handle exception
                }
           }


            }

            public void StartRecording(){


                    try{

                 recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
                 recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
                 recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                 recorder.setOutputFile(this.getFullSdPath());
                 recorder.prepare();


                 recorder.start();   // Recording is now started
                    Log.i(this.getClass().getName(), "Start Recording");
                    }
                    catch (Exception e) {
                        // TODO: handle exception
                    }

            }

            public void StopRecording(){

                 recorder.stop();          
                 recorder.release();
                 recorder = null;
                 Log.i(this.getClass().getName(), "Stop Recording");
                }


            public String getFullSdPath(){
               File sdCard = new File(Environment.getExternalStorageDirectory()    + "/RecordMyVoice");
                if (!sdCard.exists()) {
                  sdCard.mkdir();
                }
                File file = new File(Environment.getExternalStorageDirectory()      +"/RecordMyVoice/",new Date().getTime()+".mp3");
                System.out.println("Full path of record sound is : "+file.getAbsolutePath());
                return file.getAbsolutePath();
            }           
        };    

        telephony.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);    


        }



}

日志

12-30 15:48:33.973: I/incom(350): 15555215556
12-30 15:48:33.973: I/System.out(350): 15555215556
12-30 15:48:33.973: D/DEBUG(350): RINGING
12-30 15:48:33.973: E/MediaRecorder(350): stop called in an invalid state: 1
12-30 15:48:34.332: D/dalvikvm(130): GC_CONCURRENT freed 234K, 51% free 2830K/5703K, external 1006K/1038K, paused 6ms+69ms
12-30 15:48:35.042: D/dalvikvm(126): GC_EXTERNAL_ALLOC freed 277K, 50% free 3003K/5895K, external 1024K/1038K, paused 118ms
12-30 15:48:35.572: D/dalvikvm(126): GC_EXTERNAL_ALLOC freed 45K, 49% free 3039K/5895K, external 1533K/1562K, paused 128ms
12-30 15:48:35.672: W/ResourceType(126): getEntry failing because entryIndex 65 is beyond type entryCount 1
12-30 15:48:36.072: D/dalvikvm(126): GC_EXTERNAL_ALLOC freed 23K, 48% free 3105K/5895K, external 2081K/2084K, paused 58ms
12-30 15:48:37.502: I/ActivityManager(61): Displayed com.android.phone/.InCallScreen: +4s106ms
12-30 15:48:42.712: D/dalvikvm(187): GC_EXPLICIT freed 91K, 52% free 2766K/5703K, external 716K/1038K, paused 126ms
12-30 15:48:47.908: D/dalvikvm(350): GC_EXPLICIT freed 58K, 53% free 2562K/5379K, external 716K/1038K, paused 224ms
12-30 15:49:03.642: D/dalvikvm(126): GC_CONCURRENT freed 203K, 48% free 3152K/6023K, external 2387K/2604K, paused 10ms+9ms
12-30 15:49:09.102: W/ResourceType(126): Attempt to retrieve bag 0x01010041 which is invalid or in a cycle.
12-30 15:49:09.112: D/InCallTouchUi(126): onDialTrigger(whichHandle = 1)...
12-30 15:49:09.142: D/AudioHardwareInterface(34): setMode(RINGTONE)
12-30 15:49:09.172: I/phone(126): acceptCall: incoming...
12-30 15:49:09.222: D/InCallTouchUi(126): updateState: Too soon after last action; not drawing!
12-30 15:49:09.432: I/AudioService(61):  AudioFocus  requestAudioFocus() from AudioFocus_For_Phone_Ring_And_Calls
12-30 15:49:09.602: D/AudioHardwareInterface(34): setMode(IN_CALL)
12-30 15:49:13.832: I/AudioService(61):  AudioFocus  abandonAudioFocus() from AudioFocus_For_Phone_Ring_And_Calls
12-30 15:49:13.952: I/PowerManagerService(61): clearUserActivity for 10000ms from now
12-30 15:49:14.272: D/dalvikvm(126): GC_EXTERNAL_ALLOC freed 101K, 48% free 3166K/6023K, external 2594K/2604K, paused 155ms
12-30 15:49:14.503: I/ActivityManager(61): Starting: Intent { act=android.intent.action.VIEW typ=vnd.android.cursor.dir/calls flg=0x10000 cmp=com.android.contacts/.DialtactsActivity } from pid 126
12-30 15:49:14.532: D/AudioHardwareInterface(34): setMode(NORMAL)
12-30 15:49:14.722: I/ActivityManager(61): moveTaskToBack: 5
12-30 15:49:15.651: D/dalvikvm(187): GC_CONCURRENT freed 385K, 51% free 2899K/5895K, external 716K/1038K, paused 6ms+6ms
12-30 15:49:15.691: I/dalvikvm(187): Total arena pages for JIT: 11
12-30 15:49:17.161: D/dalvikvm(187): GC_EXTERNAL_ALLOC freed 61K, 50% free 2996K/5895K, external 1035K/1038K, paused 166ms
12-30 15:49:18.571: I/ActivityManager(61): Displayed com.android.contacts/.DialtactsActivity: +3s757ms

我的代码有什么问题吗?

三江源

1 个答案:

答案 0 :(得分:0)

在这里,您只能在“onCallStateChanged”中获取一次传入号码,因此您需要将该号码置于共享首选项中,最好在振铃状态下获取该号码

myprefs=context.getSharedPreferences("myprefs",001);
    final  SharedPreferences.Editor prefseditor=myprefs.edit();


    try{   

        switch(state)
        {
        case TelephonyManager.CALL_STATE_IDLE:
           { 
               try
           {
               Log.d("DEBUG", "IDLE");        
               StopRecording();  
           }
           catch (Exception e) {  
            // TODO: handle exception
        }

              break;
           }

        case TelephonyManager.CALL_STATE_RINGING:
           {
               prefseditor.putString("in",incomingNumber);
                prefseditor.commit();
               Log.d("DEBUG", "RINGING");
               StopRecording();
               break;
           }
        case TelephonyManager.CALL_STATE_OFFHOOK:
           {

               no1=myprefs.getString("in", "nothing");
                System.out.println("INCOMING"+no1);

                Log.d("DEBUG", "OFFHOOK");      


                String aa="15555215556";
                 if(aa.equals(no1)){

                 StartRecording();
                 break;
             }
           }
        }

    }
       catch (Exception e) {
            // TODO: handle exception
        }


    super.onCallStateChanged(state, incomingNumber);
}
private void StartRecording() {
    // TODO Auto-generated method stub

    try{



 recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
 recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
 recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
 recorder.setOutputFile(this.getFullSdPath());
 recorder.prepare();


 recorder.start();   // Recording is now started
    Log.i(this.getClass().getName(), "Start Recording");


    }
    catch (Exception e) {
        // TODO: handle exception
    }
    //db.execSQL("DROP TABLE IF EXISTS incomingnumber");
}
public String getFullSdPath(){
    File sdCard = new File(Environment.getExternalStorageDirectory()    + "/.RecordMyVoice");
     if (!sdCard.exists()) {
       sdCard.mkdir();
     }
     File file = new File(Environment.getExternalStorageDirectory()     +"/.RecordMyVoice/",new Date().getTime()+".mp3");
     System.out.println("Full path of record sound is : "+file.getAbsolutePath());
     return file.getAbsolutePath();
 }           


private void StopRecording() {
    // TODO Auto-generated method stub
    recorder.stop();          
    recorder.release();
    recorder = null;
    Log.i(this.getClass().getName(), "Stop Recording");
}