2017-08-20 52 views
-3

我已经构建了完整的录音机应用程序。语音通话开始时如何开始?

我想在语音通话开始时开始录音,如何检测通话状态?尝试了一些代码,它不适合我。

我只需要知道热开始录音时开始语音通话(传入和传出)。

+0

他为什么低估?作者不知道它需要广播接收器以及哪些事件需要聆听! – matheszabi

+0

谢谢matheszabi –

回答

1

Here是你需要的一个例子。在AndroidManifest

申报接收机

<receiver android:name=".IncomingCall"> 
      <intent-filter> 
       <action android:name="android.intent.action.PHONE_STATE" /> 
      </intent-filter> 
    </receiver> 

给读取手机状态许可,不得以AndroidManifest

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 

创建一个类IncomingCall与扩展广播接收器类

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.telephony.PhoneStateListener; 
import android.telephony.TelephonyManager; 
import android.util.Log; 
import android.widget.Toast; 

/** 
* Created by matheszabi on Aug/20/2017 0020. 
*/ 

public class IncomingCall extends BroadcastReceiver { 

    private Context context; 

    public void onReceive(Context context, Intent intent) { 

     this.context = context; 
     try { 
      // TELEPHONY MANAGER class object to register one listner 
      TelephonyManager tmgr = (TelephonyManager) context 
        .getSystemService(Context.TELEPHONY_SERVICE); 

      //Create Listner 
      MyPhoneStateListener PhoneListener = new MyPhoneStateListener(); 

      // Register listener for LISTEN_CALL_STATE 
      tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE); 

     } catch (Exception e) { 
      Log.e("Phone Receive Error", " " + e); 
     } 

    } 

    private class MyPhoneStateListener extends PhoneStateListener { 

     public void onCallStateChanged(int state, String incomingNumber) { 

      Log.d("MyPhoneListener",state+" incoming no:"+incomingNumber); 

      if (state == 1) { 

       String msg = "New Phone Call Event. Incomming Number : "+incomingNumber; 
       int duration = Toast.LENGTH_LONG; 
       Toast toast = Toast.makeText(context, msg, duration); 
       toast.show(); 

      } 
     } 
    } 
} 

更高版本的Android 6.0,你需要处理有点不同的权限:

import android.Manifest; 
import android.content.pm.PackageManager; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.widget.Toast; 

public class MainActivity extends AppCompatActivity { 

    private static final int MY_REQUEST_CODE = 1234; 

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



    @Override 
    protected void onResume() { 
     super.onResume(); 

     if (checkSelfPermission(Manifest.permission.READ_PHONE_STATE) 
       != PackageManager.PERMISSION_GRANTED) { 

      requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, 
        MY_REQUEST_CODE); 
     } 
    } 


    public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) { 
     if (requestCode == MY_REQUEST_CODE) { 
      if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       // Now user should be able to use camera 
       Toast.makeText(this, "I have access", Toast.LENGTH_SHORT).show(); 
      } else { 
       Toast.makeText(this, "I DON'T have access", Toast.LENGTH_SHORT).show(); 
      } 


     } 
    } 
} 

您必须允许在第一次运行的权限:

allow permission

这里的工作代码的截图:

phone ring

+0

我会试试看,谢谢 –

+0

不幸的是下载的例子不是foud,不存在。 –

+0

请使用你的头,而不是完成的例子,并责怪失踪的下载链接! - 在网站上它提供了非常好的必需的代码并且是解释者,不可能比这更容易。 – matheszabi

0

我发现了怎么办所以:

package com.example.tsuryohananov.mycallrecorder; 


import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.telephony.TelephonyManager; 
import android.util.Log; 
import android.widget.Toast; 

/** 
* Created by tsuryohananov on 20/08/2017. 
*/ 

public class MyPhoneReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Bundle extras = intent.getExtras(); 

     if (extras != null) { 
      String state = extras.getString(TelephonyManager.EXTRA_STATE); 
      Log.d("MY_DEBUG_TAG", state); 
      if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 
       String phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER); 
       Log.d("MY_DEBUG_TAG", phoneNumber); 
       // here i need to save the number for the listview. 
      } 
      if ((state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))){ 
       String phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER); 
       Toast.makeText(context,"Answered" + phoneNumber, Toast.LENGTH_SHORT).show(); 
       MainActivity.recordStart(); 
      } 
      if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){ 
       Toast.makeText(context,"Idle State", Toast.LENGTH_SHORT).show(); 
       MainActivity.stopRecord(); 
      } 
     } 
    } 
}