在 android 通话期间将铃声静音
Mute the ringer volume during a call in android
我的目标是让用户知道 calling.I 在服务中使用 TextToSpeech 完成此任务的人。
所以我决定在通话期间将铃声静音:
mAudioManager=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
volume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_MUTE, 0);
} else {
mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, true);
}
我知道这是要添加的代码,但是在哪里呢?它在扩展广播接收器的 class 中吗?
我的问题是每当有来电时,phone 都会正常响铃。我想将铃声静音并让我的 TextToSpeech 对象说话
我的清单文件如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sairaman63yahoo.callerannouncer">
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver android:name=".CalReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
<service android:name=".CallService" android:enabled="true"></service>
<activity
android:name=".DetectPhoneCallsActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
扩展广播接收器的我的 PhoneCallReceiver class 是:
public abstract class PhoneCallReceiverbase extends BroadcastReceiver {
private int mRingVolume=0;
public static int lastState= TelephonyManager.CALL_STATE_IDLE;
public static Date callstartTime;
public static boolean isIncoming;
public static String savedNumber;
private static AudioManager mAudioManager;
private int volume;
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")){
savedNumber=intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
}
else
{
String stateStr=intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
String number=intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
int state=0;
if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE))
state=TelephonyManager.CALL_STATE_IDLE;
else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
state=TelephonyManager.CALL_STATE_OFFHOOK;
else if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
state= TelephonyManager.CALL_STATE_RINGING;
onCallStateChaned(context,state,number);
}
}
}
private void onCallStateChaned(Context context, int state, String number) {
if(lastState==state){
return;
}
// mAudioManager.setStreamMute(AudioManager.STREAM_RING, true);
switch (state){
case TelephonyManager.CALL_STATE_RINGING:
// AudioManager manager=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
isIncoming = true;
callstartTime = new Date();
savedNumber = number;
onIncomingCallReceived(context, number, callstartTime);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//offhook are pickup calls....nothing is done on them
if(lastState!=TelephonyManager.CALL_STATE_RINGING)
{
isIncoming=false;
callstartTime=new Date();
onOutGoingCallStarted(context,number,callstartTime);
}
else
{
isIncoming=true;
callstartTime=new Date();
onIncomingCallAnswered(context,number,callstartTime);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if(lastState==TelephonyManager.CALL_STATE_RINGING){
onMissedCall(context,number,callstartTime);
}
else if(isIncoming)
onIncomingCallEnded(context,number,callstartTime,new Date());
else
onOutGoingCallEnded(context,number,callstartTime,new Date());
break;
}
lastState=state;
}
protected abstract void onIncomingCallReceived(Context context,String number,Date start);
protected abstract void onIncomingCallAnswered(Context context,String number,Date start);
protected abstract void onIncomingCallEnded(Context context,String number,Date start,Date end);
protected abstract void onOutGoingCallStarted(Context context,String number,Date start);
protected abstract void onOutGoingCallEnded(Context context,String number,Date start,Date end);
protected abstract void onMissedCall(Context context,String number,Date start);
}
我的 CallReceiver class 扩展了 PhoneCallReceiver class 是:
public class CalReceiver extends PhoneCallReceiverbase {
TextToSpeech toSpeech;
private AudioManager mAudioManager;
private int volume;
@Override
protected void onIncomingCallReceived(Context context, String number, Date start) {
mAudioManager=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
volume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_MUTE, 0);
} else {
mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, true);
}
Intent intent1 = new Intent(context.getApplicationContext(), CallService.class);
intent1.putExtra("call", "CALL RECEIVED");
context.startService(intent1);
// Toast.makeText(context, "CALL RECIEVED", Toast.LENGTH_LONG).show();
final ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
final List<ActivityManager.RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);
for (ActivityManager.RunningServiceInfo runningServiceInfo : services) {
if (runningServiceInfo.service.getClassName().equals(CallService.class)) {
Log.v("SERVICE", "RUNNING");
}
}
}
@Override
protected void onIncomingCallAnswered(Context context, String number, Date start) {
}
@Override
protected void onIncomingCallEnded(Context context, String number, Date start, Date end) {
Toast.makeText(context,"CALL ENDED",Toast.LENGTH_LONG).show();
}
@Override
protected void onOutGoingCallStarted(Context context, String number, Date start) {
Toast.makeText(context,"CALL STARTED",Toast.LENGTH_LONG).show();
}
@Override
protected void onOutGoingCallEnded(Context context, String number, Date start, Date end) {
Toast.makeText(context,"CALL ENDED",Toast.LENGTH_LONG).show();
}
@Override
protected void onMissedCall(Context context, String number, Date start) {
Toast.makeText(context,"MISSED CALL",Toast.LENGTH_LONG).show();
}
}
最后,我的服务 class 说的是:
public class CallService extends Service implements TextToSpeech.OnInitListener,TextToSpeech.OnUtteranceCompletedListener {
int volume=0;
private TextToSpeech tts;
private String spoken="call started sir!";
private AudioManager mAudioManager;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
tts=new TextToSpeech(this,this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
spoken=(String)intent.getExtras().get("call");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onInit(int status) {
if(status==TextToSpeech.SUCCESS)
{
// mAudioManager.setStreamMute(AudioManager.STREAM_RING, true);
tts.setLanguage(Locale.ENGLISH);
// toSpeech.speak("CALL IS COMING",TextToSpeech.QUEUE_FLUSH,null);
if (Build.VERSION.RELEASE.startsWith("5")) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
tts.speak(spoken, TextToSpeech.QUEUE_FLUSH, null, null);
}
}
else {
tts.speak(spoken, TextToSpeech.QUEUE_FLUSH, null);
}
}
}
@Override
public void onUtteranceCompleted(String utteranceId){
mAudioManager.setStreamMute(AudioManager.STREAM_RING, false);
mAudioManager.setStreamVolume(AudioManager.STREAM_RING,
volume, AudioManager.FLAG_ALLOW_RINGER_MODES);
stopSelf();
}
}
总结查询:
目的:在通话期间使铃声音量静音,并使 TextToSpeech 对象说话并在讲话后继续铃声音量
问题:1)在哪里添加静音铃声的代码
2) 我已经在 onIncomingCallReceived() 中的 CallReceiver class 中添加了代码,但它似乎没有 work.The 铃声不会停止
请帮忙!
我知道这是一个很长的问题,但我被困在这里了。
谢谢您的帮助!
正如我在评论中所说,我找不到让我的应用程序执行您想要的操作的方法。除了在 TTS 宣布呼叫后复制用户的铃声并监视他们与待处理呼叫的进一步交互之外,我只能将当前铃声静音并且无法启动它,这很复杂。
为此:
public static void muteRinger(final Context ctx, final boolean mute) {
final AudioManager am = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);
switch (am.getRingerMode()) {
case AudioManager.RINGER_MODE_NORMAL:
Log.i("TAG", "getRingerMode: RINGER_MODE_NORMAL");
break;
case AudioManager.RINGER_MODE_SILENT:
Log.i("TAG", "getRingerMode: RINGER_MODE_NORMAL");
break;
case AudioManager.RINGER_MODE_VIBRATE:
Log.i("TAG", "getRingerMode: RINGER_MODE_NORMAL");
break;
default:
Log.e("TAG", "getRingerMode: Default??");
break;
}
if(mute){
Log.i("TAG", "muting ringtone");
// Save am.getRingerMode() to the shared preferences as the user's default
try {
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
} catch (Exception e) {
e.printStackTrace();
}
} else {
Log.i("TAG", "restoring ringtone");
try {
am.setRingerMode(// The default you previously saved);
} catch (Exception e) {
e.printStackTrace();
}
}
}
显然,如果您未能正确恢复用户的铃声设置,您将会惹恼用户,如果他们错过重要电话,您可能会有麻烦......
我的目标是让用户知道 calling.I 在服务中使用 TextToSpeech 完成此任务的人。
所以我决定在通话期间将铃声静音:
mAudioManager=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
volume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_MUTE, 0);
} else {
mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, true);
}
我知道这是要添加的代码,但是在哪里呢?它在扩展广播接收器的 class 中吗?
我的问题是每当有来电时,phone 都会正常响铃。我想将铃声静音并让我的 TextToSpeech 对象说话
我的清单文件如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sairaman63yahoo.callerannouncer">
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver android:name=".CalReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
<service android:name=".CallService" android:enabled="true"></service>
<activity
android:name=".DetectPhoneCallsActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
扩展广播接收器的我的 PhoneCallReceiver class 是:
public abstract class PhoneCallReceiverbase extends BroadcastReceiver {
private int mRingVolume=0;
public static int lastState= TelephonyManager.CALL_STATE_IDLE;
public static Date callstartTime;
public static boolean isIncoming;
public static String savedNumber;
private static AudioManager mAudioManager;
private int volume;
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")){
savedNumber=intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
}
else
{
String stateStr=intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
String number=intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
int state=0;
if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE))
state=TelephonyManager.CALL_STATE_IDLE;
else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
state=TelephonyManager.CALL_STATE_OFFHOOK;
else if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
state= TelephonyManager.CALL_STATE_RINGING;
onCallStateChaned(context,state,number);
}
}
}
private void onCallStateChaned(Context context, int state, String number) {
if(lastState==state){
return;
}
// mAudioManager.setStreamMute(AudioManager.STREAM_RING, true);
switch (state){
case TelephonyManager.CALL_STATE_RINGING:
// AudioManager manager=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
isIncoming = true;
callstartTime = new Date();
savedNumber = number;
onIncomingCallReceived(context, number, callstartTime);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//offhook are pickup calls....nothing is done on them
if(lastState!=TelephonyManager.CALL_STATE_RINGING)
{
isIncoming=false;
callstartTime=new Date();
onOutGoingCallStarted(context,number,callstartTime);
}
else
{
isIncoming=true;
callstartTime=new Date();
onIncomingCallAnswered(context,number,callstartTime);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if(lastState==TelephonyManager.CALL_STATE_RINGING){
onMissedCall(context,number,callstartTime);
}
else if(isIncoming)
onIncomingCallEnded(context,number,callstartTime,new Date());
else
onOutGoingCallEnded(context,number,callstartTime,new Date());
break;
}
lastState=state;
}
protected abstract void onIncomingCallReceived(Context context,String number,Date start);
protected abstract void onIncomingCallAnswered(Context context,String number,Date start);
protected abstract void onIncomingCallEnded(Context context,String number,Date start,Date end);
protected abstract void onOutGoingCallStarted(Context context,String number,Date start);
protected abstract void onOutGoingCallEnded(Context context,String number,Date start,Date end);
protected abstract void onMissedCall(Context context,String number,Date start);
}
我的 CallReceiver class 扩展了 PhoneCallReceiver class 是:
public class CalReceiver extends PhoneCallReceiverbase {
TextToSpeech toSpeech;
private AudioManager mAudioManager;
private int volume;
@Override
protected void onIncomingCallReceived(Context context, String number, Date start) {
mAudioManager=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
volume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_MUTE, 0);
} else {
mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, true);
}
Intent intent1 = new Intent(context.getApplicationContext(), CallService.class);
intent1.putExtra("call", "CALL RECEIVED");
context.startService(intent1);
// Toast.makeText(context, "CALL RECIEVED", Toast.LENGTH_LONG).show();
final ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
final List<ActivityManager.RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);
for (ActivityManager.RunningServiceInfo runningServiceInfo : services) {
if (runningServiceInfo.service.getClassName().equals(CallService.class)) {
Log.v("SERVICE", "RUNNING");
}
}
}
@Override
protected void onIncomingCallAnswered(Context context, String number, Date start) {
}
@Override
protected void onIncomingCallEnded(Context context, String number, Date start, Date end) {
Toast.makeText(context,"CALL ENDED",Toast.LENGTH_LONG).show();
}
@Override
protected void onOutGoingCallStarted(Context context, String number, Date start) {
Toast.makeText(context,"CALL STARTED",Toast.LENGTH_LONG).show();
}
@Override
protected void onOutGoingCallEnded(Context context, String number, Date start, Date end) {
Toast.makeText(context,"CALL ENDED",Toast.LENGTH_LONG).show();
}
@Override
protected void onMissedCall(Context context, String number, Date start) {
Toast.makeText(context,"MISSED CALL",Toast.LENGTH_LONG).show();
}
}
最后,我的服务 class 说的是:
public class CallService extends Service implements TextToSpeech.OnInitListener,TextToSpeech.OnUtteranceCompletedListener {
int volume=0;
private TextToSpeech tts;
private String spoken="call started sir!";
private AudioManager mAudioManager;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
tts=new TextToSpeech(this,this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
spoken=(String)intent.getExtras().get("call");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onInit(int status) {
if(status==TextToSpeech.SUCCESS)
{
// mAudioManager.setStreamMute(AudioManager.STREAM_RING, true);
tts.setLanguage(Locale.ENGLISH);
// toSpeech.speak("CALL IS COMING",TextToSpeech.QUEUE_FLUSH,null);
if (Build.VERSION.RELEASE.startsWith("5")) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
tts.speak(spoken, TextToSpeech.QUEUE_FLUSH, null, null);
}
}
else {
tts.speak(spoken, TextToSpeech.QUEUE_FLUSH, null);
}
}
}
@Override
public void onUtteranceCompleted(String utteranceId){
mAudioManager.setStreamMute(AudioManager.STREAM_RING, false);
mAudioManager.setStreamVolume(AudioManager.STREAM_RING,
volume, AudioManager.FLAG_ALLOW_RINGER_MODES);
stopSelf();
}
}
总结查询:
目的:在通话期间使铃声音量静音,并使 TextToSpeech 对象说话并在讲话后继续铃声音量
问题:1)在哪里添加静音铃声的代码 2) 我已经在 onIncomingCallReceived() 中的 CallReceiver class 中添加了代码,但它似乎没有 work.The 铃声不会停止
请帮忙!
我知道这是一个很长的问题,但我被困在这里了。 谢谢您的帮助!
正如我在评论中所说,我找不到让我的应用程序执行您想要的操作的方法。除了在 TTS 宣布呼叫后复制用户的铃声并监视他们与待处理呼叫的进一步交互之外,我只能将当前铃声静音并且无法启动它,这很复杂。
为此:
public static void muteRinger(final Context ctx, final boolean mute) {
final AudioManager am = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);
switch (am.getRingerMode()) {
case AudioManager.RINGER_MODE_NORMAL:
Log.i("TAG", "getRingerMode: RINGER_MODE_NORMAL");
break;
case AudioManager.RINGER_MODE_SILENT:
Log.i("TAG", "getRingerMode: RINGER_MODE_NORMAL");
break;
case AudioManager.RINGER_MODE_VIBRATE:
Log.i("TAG", "getRingerMode: RINGER_MODE_NORMAL");
break;
default:
Log.e("TAG", "getRingerMode: Default??");
break;
}
if(mute){
Log.i("TAG", "muting ringtone");
// Save am.getRingerMode() to the shared preferences as the user's default
try {
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
} catch (Exception e) {
e.printStackTrace();
}
} else {
Log.i("TAG", "restoring ringtone");
try {
am.setRingerMode(// The default you previously saved);
} catch (Exception e) {
e.printStackTrace();
}
}
}
显然,如果您未能正确恢复用户的铃声设置,您将会惹恼用户,如果他们错过重要电话,您可能会有麻烦......