我正在创建一个应用程序,告诉用户正在呼叫的人。
因此,作为测试的一部分,我创建了一个抽象类,它扩展了广播接收器以处理调用。
我还创建了一个扩展抽象类的类,从这里我开始了一个服务。
在服务中,我创建了一个TextToSpeech对象并开始说()。
这似乎不起作用。除了铃声,我什么都听不见。 每当我接到电话时,我都会听到铃声而不是声音。
目标:我希望听到铃声开始响起。
请帮助我!
感谢。
我的广播接收器如下:
public abstract class PhoneCallReceiverbase extends BroadcastReceiver {
public static int lastState= TelephonyManager.CALL_STATE_IDLE;
public static Date callstartTime;
public static boolean isIncoming;
public static String savedNumber;
@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;
}
switch (state){
case TelephonyManager.CALL_STATE_RINGING:
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);
}
扩展PhoneCallReceiver类的类:
public class CalReceiver extends PhoneCallReceiverbase {
TextToSpeech toSpeech;
@Override
protected void onIncomingCallReceived(Context context, String number, Date start) {
Toast.makeText(context,"CALL RECIEVED",Toast.LENGTH_LONG).show();
Intent intent=new Intent(context,CallService.class);
intent.putExtra("call","CALL RECEIVED");
context.startService(intent);
}
@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();
}
}
我的服务如下:
public class CallService extends Service implements TextToSpeech.OnInitListener,TextToSpeech.OnUtteranceCompletedListener {
private TextToSpeech tts;
private String spoken;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.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)
{
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) {
stopSelf();
}
@Override
public void onDestroy() {
if(tts!=null)
{
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
}