0

我想在我的应用程序中添加一件事。我正在尝试的是每当用户打电话或用户拨打电话时,应同时开始录制并在切断电话后记录必须存储在SD卡中。传出调用不记录

问题
1)来电记录工作有时

2)传出不工作

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    private static final int REQUEST_CODE = 0; 
    private DevicePolicyManager mDPM; 
    private ComponentName mAdminName; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     PackageManager p = getPackageManager(); 
     p.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); 

     try { 
      // Initiate DevicePolicyManager. 
      mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); 
      mAdminName = new ComponentName(this, DeviceAdminDemo.class); 

      if (!mDPM.isAdminActive(mAdminName)) { 
       Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); 
       intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName); 
       intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Click on Activate button to secure your application."); 
       startActivityForResult(intent, REQUEST_CODE); 
      } else { 
       // mDPM.lockNow(); 
       // Intent intent = new Intent(MainActivity.this, 
       // TrackDeviceService.class); 
       // startService(intent); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     if (REQUEST_CODE == requestCode) { 
      Intent intent = new Intent(MainActivity.this, TService.class); 
      startService(intent); 
     } 
    } 

} 

TService

public class TService extends Service { 
    MediaRecorder recorder; 
    File audiofile; 
    String name, phonenumber; 
    String audio_format; 
    public String Audio_Type; 
    int audioSource; 
    Context context; 

    Timer timer; 
    Boolean offHook = false, ringing = false; 
    Toast toast; 
    Boolean isOffHook = false; 
    private boolean recordstarted = false; 

    private static final String ACTION_IN = "android.intent.action.PHONE_STATE"; 
    private static final String ACTION_OUT = "android.intent.action.NEW_OUTGOING_CALL"; 
    private CallBr br_call; 




    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void onDestroy() { 
     Log.d("service", "destroy"); 

     super.onDestroy(); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     // final String terminate =(String) 
     // intent.getExtras().get("terminate");// 
     // intent.getStringExtra("terminate"); 
     // Log.d("TAG", "service started"); 
     // 
     // TelephonyManager telephony = (TelephonyManager) 
     // getSystemService(Context.TELEPHONY_SERVICE); // TelephonyManager 
     // // object 
     // CustomPhoneStateListener customPhoneListener = new 
     // CustomPhoneStateListener(); 
     // telephony.listen(customPhoneListener, 
     // PhoneStateListener.LISTEN_CALL_STATE); 
     // context = getApplicationContext(); 

     final IntentFilter filter = new IntentFilter(); 
     filter.addAction(ACTION_OUT); 
     filter.addAction(ACTION_IN); 
     this.br_call = new CallBr(); 
     this.registerReceiver(this.br_call, filter); 

     // if(terminate != null) { 
     // stopSelf(); 
     // } 
     return START_NOT_STICKY; 
    } 

    public class CallBr extends BroadcastReceiver { 
     Bundle bundle; 
     String state; 
     String inCall, outCall; 
     public boolean wasRinging = false; 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      if (intent.getAction().equals(ACTION_IN)) { 
       if ((bundle = intent.getExtras()) != null) { 
        state = bundle.getString(TelephonyManager.EXTRA_STATE); 
        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 
         inCall = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER); 
         wasRinging = true; 
         Toast.makeText(context, "IN : " + inCall, Toast.LENGTH_LONG).show(); 
        } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) { 
         if (wasRinging == true) { 

          Toast.makeText(context, "ANSWERED", Toast.LENGTH_LONG).show(); 

          String out = new SimpleDateFormat("dd-MM-yyyy hh-mm-ss").format(new Date()); 
          File sampleDir = new File(Environment.getExternalStorageDirectory(), "/TestRecordingDasa1"); 
          if (!sampleDir.exists()) { 
           sampleDir.mkdirs(); 
          } 
          String file_name = "Record"; 
          try { 
           audiofile = File.createTempFile(file_name, ".amr", sampleDir); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
          String path = Environment.getExternalStorageDirectory().getAbsolutePath(); 
          System.out.println("PATH"+path); 

          recorder = new MediaRecorder(); 
//       recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL); 

          recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION); 
          recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB); 
          recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
          recorder.setOutputFile(audiofile.getAbsolutePath()); 
          try { 
           recorder.prepare(); 
          } catch (IllegalStateException e) { 
           e.printStackTrace(); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
          recorder.start(); 
          recordstarted = true; 
         } 
        } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) { 
         wasRinging = false; 
         Toast.makeText(context, "REJECT || DISCO", Toast.LENGTH_LONG).show(); 
         if (recordstarted) { 
          recorder.stop(); 
          recordstarted = false; 
         } 
        } 
       } 
      } else if (intent.getAction().equals(ACTION_OUT)) { 
       if ((bundle = intent.getExtras()) != null) { 
        outCall = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); 
        Toast.makeText(context, "OUT : " + outCall, Toast.LENGTH_LONG).show(); 
       } 
      } 
     } 
    } 

} 

回答

2

从TService中删除此if (wasRinging == true),这是布尔值检查布尔检查手机是否在一段时间之前响铃,即当电话接到呼叫时,如果呼出电话保持为假,则记录器部分将被跳过。只要改变它,它应该工作。

只是这个替换您的TService代码..

public class TService extends Service { 
    MediaRecorder recorder; 
    File audiofile; 
    String name, phonenumber; 
    String audio_format; 
    public String Audio_Type; 
    int audioSource; 
    Context context; 

    Timer timer; 
    Boolean offHook = false, ringing = false; 
    Toast toast; 
    Boolean isOffHook = false; 
    private boolean recordstarted = false; 

    private static final String ACTION_IN = "android.intent.action.PHONE_STATE"; 
    private static final String ACTION_OUT = "android.intent.action.NEW_OUTGOING_CALL"; 
    private CallBr br_call; 




    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void onDestroy() { 
     Log.d("service", "destroy"); 

     super.onDestroy(); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     // final String terminate =(String) 
     // intent.getExtras().get("terminate");// 
     // intent.getStringExtra("terminate"); 
     // Log.d("TAG", "service started"); 
     // 
     // TelephonyManager telephony = (TelephonyManager) 
     // getSystemService(Context.TELEPHONY_SERVICE); // TelephonyManager 
     // // object 
     // CustomPhoneStateListener customPhoneListener = new 
     // CustomPhoneStateListener(); 
     // telephony.listen(customPhoneListener, 
     // PhoneStateListener.LISTEN_CALL_STATE); 
     // context = getApplicationContext(); 

     final IntentFilter filter = new IntentFilter(); 
     filter.addAction(ACTION_OUT); 
     filter.addAction(ACTION_IN); 
     this.br_call = new CallBr(); 
     this.registerReceiver(this.br_call, filter); 

     // if(terminate != null) { 
     // stopSelf(); 
     // } 
     return START_NOT_STICKY; 
    } 

    public class CallBr extends BroadcastReceiver { 
     Bundle bundle; 
     String state; 
     String inCall, outCall; 
     public boolean wasRinging = false; 
     public boolean didMakeACall = false; 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      if (intent.getAction().equals(ACTION_IN)) { 
       if ((bundle = intent.getExtras()) != null) { 
        state = bundle.getString(TelephonyManager.EXTRA_STATE); 
        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 
         inCall = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER); 
         wasRinging = true; 
         Toast.makeText(context, "IN : " + inCall, Toast.LENGTH_LONG).show(); 
        } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) { 
         if (wasRinging || didMakeACall) { 

          Toast.makeText(context, "ANSWERED", Toast.LENGTH_LONG).show(); 

          String out = new SimpleDateFormat("dd-MM-yyyy hh-mm-ss").format(new Date()); 
          File sampleDir = new File(Environment.getExternalStorageDirectory(), "/TestRecordingDasa1"); 
          if (!sampleDir.exists()) { 
           sampleDir.mkdirs(); 
          } 
          String file_name = "Record"; 
          try { 
           audiofile = File.createTempFile(file_name, ".amr", sampleDir); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
          String path = Environment.getExternalStorageDirectory().getAbsolutePath(); 
          System.out.println("PATH"+path); 

          recorder = new MediaRecorder(); 


          recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION); 
          recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB); 
          recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
          recorder.setOutputFile(audiofile.getAbsolutePath()); 
          try { 
           recorder.prepare(); 
          } catch (IllegalStateException e) { 
           e.printStackTrace(); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
          recorder.start(); 
          recordstarted = true; 
         } 
        } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) { 
         wasRinging = false; 
         Toast.makeText(context, "REJECT || DISCO", Toast.LENGTH_LONG).show(); 
         if (recordstarted) { 
          recorder.stop(); 
          recordstarted = false; 
         } 
        } 
       } 
      } else if (intent.getAction().equals(ACTION_OUT)) { 
       if ((bundle = intent.getExtras()) != null) { 
        didMakeACall = true; 
        outCall = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); 
        Toast.makeText(context, "OUT : " + outCall, Toast.LENGTH_LONG).show(); 
       } 
      } 
     } 
    } 

} 
+0

我不力让你say..sorry什么英语 – chris

+0

让我纠正你的TService代码,你会得到它...... –

+0

我editted我的答案,只是我的更换你的TService代码。 –

0

恐怕社会不会像外部链接在这里,所以我在这里张贴我的快速演示项目...

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/> 
<uses-permission android:name="android.permission.RECORD_AUDIO"/> 


<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <service android:name=".TService"/> 
</application> 

MainActivity.java

我在onCreate方法开始TService,请注意,这可能是项目中的任何活动,或任何情况下你就可以开始从服务。

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     startService(new Intent(this, TService.class)); 
    } 
} 

现在终于, TService.java 我在奇巧和棒棒糖测试这和它的工作,你需要在运行时,明确要求清单权限新版本,它应该只是工作一样好。

public class TService extends Service { 
    MediaRecorder recorder; 
    File audiofile; 
    String name, phonenumber; 
    String audio_format; 
    public String Audio_Type; 
    int audioSource; 
    Context context; 

    Timer timer; 
    Boolean offHook = false, ringing = false; 
    Toast toast; 
    Boolean isOffHook = false; 
    private boolean recordstarted = false; 

    private static final String ACTION_IN = "android.intent.action.PHONE_STATE"; 
    private static final String ACTION_OUT = "android.intent.action.NEW_OUTGOING_CALL"; 
    private CallBr br_call; 




    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void onDestroy() { 
     Log.d("service", "destroy"); 

     super.onDestroy(); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     // final String terminate =(String) 
     // intent.getExtras().get("terminate");// 
     // intent.getStringExtra("terminate"); 
     // Log.d("TAG", "service started"); 
     // 
     // TelephonyManager telephony = (TelephonyManager) 
     // getSystemService(Context.TELEPHONY_SERVICE); // TelephonyManager 
     // // object 
     // CustomPhoneStateListener customPhoneListener = new 
     // CustomPhoneStateListener(); 
     // telephony.listen(customPhoneListener, 
     // PhoneStateListener.LISTEN_CALL_STATE); 
     // context = getApplicationContext(); 

     final IntentFilter filter = new IntentFilter(); 
     filter.addAction(ACTION_OUT); 
     filter.addAction(ACTION_IN); 
     this.br_call = new CallBr(); 
     this.registerReceiver(this.br_call, filter); 

     // if(terminate != null) { 
     // stopSelf(); 
     // } 
     return START_NOT_STICKY; 
    } 

    public class CallBr extends BroadcastReceiver { 
     Bundle bundle; 
     String state; 
     String inCall, outCall; 
     public boolean wasRinging = false; 
     boolean didMakeACall = false; 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      if (intent.getAction().equals(ACTION_IN)) { 
       if ((bundle = intent.getExtras()) != null) { 
        state = bundle.getString(TelephonyManager.EXTRA_STATE); 
        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 
         inCall = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER); 
         wasRinging = true; 
         Toast.makeText(context, "IN : " + inCall, Toast.LENGTH_LONG).show(); 
        } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) { 
         if (wasRinging || didMakeACall) { 

          Toast.makeText(context, "ANSWERED", Toast.LENGTH_LONG).show(); 

          String out = new SimpleDateFormat("dd-MM-yyyy hh-mm-ss").format(new Date()); 
          File sampleDir = new File(Environment.getExternalStorageDirectory(), "/MyRecorder"); 
          if (!sampleDir.exists()) { 
           sampleDir.mkdirs(); 
          } 
          String file_name = "Record"; 
          try { 
           audiofile = File.createTempFile(file_name, ".amr", sampleDir); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
          String path = Environment.getExternalStorageDirectory().getAbsolutePath(); 
          System.out.println("PATH"+path); 

          recorder = new MediaRecorder(); 
//       recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL); 

          recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION); 
          recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB); 
          recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
          recorder.setOutputFile(audiofile.getAbsolutePath()); 
          try { 
           recorder.prepare(); 
          } catch (IllegalStateException e) { 
           e.printStackTrace(); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
          recorder.start(); 
          recordstarted = true; 
         } 
        } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) { 
         wasRinging = false; 
         Toast.makeText(context, "REJECT || DISCO", Toast.LENGTH_LONG).show(); 
         if (recordstarted) { 
          recorder.stop(); 
          recordstarted = false; 
         } 
        } 
       } 
      } else if (intent.getAction().equals(ACTION_OUT)) { 
       if ((bundle = intent.getExtras()) != null) { 
        didMakeACall = true; 
        outCall = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); 
        Toast.makeText(context, "OUT : " + outCall, Toast.LENGTH_LONG).show(); 
       } 
      } 
     } 
    } 

} 
+0

您使用了哪个设备? – chris

+0

htc desire 820g runing kitkat和模拟器运行棒棒糖,如果你有问题我可以做一个github项目并与你分享 –

+0

我正在使用模拟器kitkat..google nexus 7 api level 4.4.4。在这不工作 – chris