语音通话开始时如何开始?
Posted
技术标签:
【中文标题】语音通话开始时如何开始?【英文标题】:How to start something when voice call starts? 【发布时间】:2017-08-20 11:21:47 【问题描述】:我已经构建了完整的录音机应用程序。
我想在电话开始语音通话时开始录音,如何检测通话状态?尝试了一些代码,但它对我不起作用。
我只需要知道语音通话开始(呼入和呼出)时开始录制的热度。
【问题讨论】:
为什么他被否决了?作者不知道它需要一个广播接收器以及需要监听哪些事件! 【参考方案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 并扩展 BroadcastReceiver 类
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();
您必须在第一次运行时允许权限:
这是工作代码的截图:
【讨论】:
很遗憾,下载示例不存在,不存在。 请用你的头脑,不要把完成的例子归咎于缺少下载链接! - 在网站上,它很好地呈现了所需的代码并且是解释器,再简单不过了。 @Tsur 我创建了一个项目,你需要的是一个复制粘贴,不要告诉我你做不到或者缺少下载按钮! 我还没评论,你在说什么?非常感谢您的帮助'您非常有帮助【参考方案2】:我找到了方法:
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();
【讨论】:
以上是关于语音通话开始时如何开始?的主要内容,如果未能解决你的问题,请参考以下文章