전화 수신시 호출되는 BroadcastReceiver를 구현하려고합니다. BroadcastReceiver에서 연락처의 전화 번호에서 연락처 이름을 얻고 특정 메시지를 설명하는 TextToSpeech 개체를 초기화하려고합니다.BroadcastReceiver 구성 요소는 서비스에 바인딩 할 수 없습니다.
"android.content.ReceiverCallNotAllowedException : BroadcastReceiver 구성 요소가 서비스에 바인딩 할 수 없습니다"오류가 발생합니다. 원하는 기능을 얻기 위해 무엇을 할 수 있습니까? 여기
public class IncomingCall extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
// this.context = context;
// TELEPHONY MANAGER class object to register one listner
TelephonyManager tmgr = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
//Create Listner
MyPhoneStateListener PhoneListener = new MyPhoneStateListener(context);
// 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 {
Context context;
TextToSpeech ttobj;
MyPhoneStateListener(Context context)
{
this.context = context;
ttobj = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
ttobj.setLanguage(Locale.UK);
}
});
}
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;
String name = getContactName(incomingNumber,context);
String msg="";
if(name=="")
name = "Unknown Number";
msg = "Sir, Incoming Call from "+name ;
ttobj.speak(msg, TextToSpeech.QUEUE_FLUSH, null);
}
}
public String getContactName(final String phoneNumber, Context context)
{
Uri uri=Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,Uri.encode(phoneNumber));
String[] projection = new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME};
String contactName="";
Cursor cursor=context.getContentResolver().query(uri,projection,null,null,null);
if (cursor != null) {
if(cursor.moveToFirst()) {
contactName=cursor.getString(0);
}
cursor.close();
}
return contactName;
}
}
}
질문을 편집하고 전체 Java 스택 추적을 게시하는 것이 좋습니다. – CommonsWare