I am trying to Implement a BroadcastReceiver which is called when a Phone call is received. In the BroadcastReceiver i am trying to get the name of the contact from the contact's phone number and then initialize a TextToSpeech Object which narrates a specific message.
I am Getting error "android.content.ReceiverCallNotAllowedException: BroadcastReceiver components are not allowed to bind to services". So what can i do to achieve the desired functionality. Here is the code below
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;
}
}
}
Create a Service in your app (it will need to be a foreground service) which is started by your BroadcastReceiver to do the work. In your service you will be able to query the contact into and trigger the text to speech.
Related
I was reading some post but none have a good answer for me.
So, which is the best way to pass data from broadcast to activity without restart the activity?
Actually I'm using this.
SMSListener:
public class SmsListener extends BroadcastReceiver {
private OnSmsReceivedListener listener = null;
#Override
public void onReceive(Context context, Intent intent) {
try {
if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION.equals(intent.getAction())) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
for (SmsMessage smsMessage : Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
String messageBody = smsMessage.getMessageBody();
String phoneNumber = smsMessage.getDisplayOriginatingAddress();
if (listener != null) {
listener.onSmsReceived(phoneNumber, messageBody);
}
}
}
}
} catch (Exception e)
{
Log.e("Error", "Some some");
}
}
public void setOnSmsReceivedListener(Context context) {
this.listener = (OnSmsReceivedListener) context;
}
}
OnSmsReceivedListener:
public interface OnSmsReceivedListener {
void onSmsReceived(String sender, String message);
}
Activity:
public class MainActivity extends AppCompatActivity implements OnSmsReceivedListener {
private SmsListener receiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
/***********/
receiver = new SmsListener();
receiver.setOnSmsReceivedListener(this);
}
#Override
public void onSmsReceived(String sender, String message) {
Log.e("Test", "Sender: "+sender+" - Message: "+message);
}
}
Another of my questions is why I never get log "Test" in my activity. Is like listener is always null, why?
You should add at the end of onCreate()
final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";
IntentFilter intentFilter = new IntentFilter(SMS_RECEIVED_ACTION);
registerReceiver(receiver, intentFilter);
and in the onPause()
unregisterReceiver(receiver);
add also the following permission on AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
As we know that, when we use any menu in sim toolkit it send command to mobile network in ussd or sms format.
I need that sms or ussd to record and show it to me in android application.
I am calling the library service which i got in main activity like this
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent srvIntent = new Intent(this, CDUSSDService.class);
startService(srvIntent);
}
}
USSD Service Class is like that:
public class CDUSSDService extends Service{
private String TAG = CDUSSDService.class.getSimpleName();
private boolean mActive = true;//false //we will only activate this "USSD listener" when we want it
BroadcastReceiver receiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
if(intent.getAction().equals(Intent.ACTION_INSERT))
{
//activity wishes to listen to USSD returns, so activate this
mActive = true;
Log.d(TAG, "activate ussd listener");
showtoast(""+"activate ussd listener");
}
else if(intent.getAction().equals(Intent.ACTION_DELETE))
{
mActive = false;
Log.d(TAG, "deactivate ussd listener");
showtoast(""+"DeActivate ussd listener");
}
}
};
private final IExtendedNetworkService.Stub mBinder = new IExtendedNetworkService.Stub ()
{
public void clearMmiString() throws RemoteException
{
Log.d(TAG, "called clear");
showtoast("called clear.");
}
public void setMmiString(String number) throws RemoteException
{
Log.d (TAG, "setMmiString:" + number);
showtoast("setMmiString:"+number);
}
public CharSequence getMmiRunningText() throws RemoteException
{
if(mActive == true)
{
return null;
}
return "USSD Running";
}
public CharSequence getUserMessage(CharSequence text)
throws RemoteException {
Log.d(TAG, "get user message " + text);
showtoast("GET Usr Message:"+text);
if(mActive == false){
//listener is still inactive, so return whatever we got
Log.d(TAG, "inactive " + text);
showtoast("inactive:"+text);
return text;
}
//listener is active, so broadcast data and suppress it from default behavior
//build data to send with intent for activity, format URI as per RFC 2396
Uri ussdDataUri = new Uri.Builder()
.scheme(getBaseContext().getString(R.string.uri_scheme))
.authority(getBaseContext().getString(R.string.uri_authority))
.path(getBaseContext().getString(R.string.uri_path))
.appendQueryParameter(getBaseContext().getString(R.string.uri_param_name), text.toString())
.build();
sendBroadcast(new Intent(Intent.ACTION_GET_CONTENT, ussdDataUri));
mActive = false;
return null;
}
};
#Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "called onbind");
//the insert/delete intents will be fired by activity to activate/deactivate listener since service cannot be stopped
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_INSERT);
filter.addAction(Intent.ACTION_DELETE);
filter.addDataScheme(getBaseContext().getString(R.string.uri_scheme));
filter.addDataAuthority(getBaseContext().getString(R.string.uri_authority), null);
filter.addDataPath(getBaseContext().getString(R.string.uri_path), PatternMatcher.PATTERN_LITERAL);
registerReceiver(receiver, filter);
return mBinder;
}
public void showtoast(String str)
{
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
}
}
I have created MainActivity inside the UssdLibray structure is like that in the below picture:
you can do this with the help of this library https://github.com/alaasalman/ussdinterceptor
Im developing a call blocking application but i faced with this error.
The error is attached.
Would you please help me out ?
public class IncomingCall extends BroadcastReceiver {
Context context;
#Override
public void onReceive(Context context, Intent intent) {
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);
// state = 1 means when phone is ringing
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();
}
}
}
}
You're encountering this error because "context" in "Toast.makeText(context, msg, duration);" is null. You need to set the class's "context" field in your onReceive method:
#Override
public void onReceive(Context context, Intent intent) {
this.context = context;
I want to block a certain phone number from calling me. I coded everything and it's working fine when I put the number within application. But I don't know how to make a custom list of phone numbers that I manually enter after the application is installed. I want to change any number and save it. In PhoneCallStateListener I made a new class of type Resource.java where I extract the number. But it did not work. Please help.
Here is my code:
public class PhoneCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
PhoneCallStateListener customPhoneListener = new PhoneCallStateListener(context);
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
public class PhoneCallStateListener extends PhoneStateListener {
private Context context;
public PhoneCallStateListener(Context context){
this.context = context;
}
#SuppressWarnings("unchecked")
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
//Turn ON the mute
audioManager.setStreamMute(AudioManager.STREAM_RING, true);
//i want to getnum from here
Resource res=new Resource();
String get=res.et.getText().toString();
//end of getting num
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class clazz = Class.forName(telephonyManager.getClass().getName());
Method method = clazz.getDeclaredMethod("getITelephony");
method.setAccessible(true);
ITelephony telephonyService = (ITelephony) method.invoke(telephonyManager);
//Checking incoming call number
if (incomingNumber.equalsIgnoreCase(get)) {
telephonyService.silenceRinger();
}
} catch (Exception e) {
e.printStackTrace();
}
//Turn OFF the mute
audioManager.setStreamMute(AudioManager.STREAM_RING, false);
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
Resource.java file:
public class Resource extends Activity {
Button btsave,btcancel;
EditText et;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.numbers);
et=(EditText)findViewById(R.id.editNumber);
btsave=(Button)findViewById(R.id.buttonSave);
btcancel=(Button)findViewById(R.id.buttonCancel);
edittext();
}
public void edittext() {
// TODO Auto-generated method stub
InputStreamReader is=new InputStreamReader(this.getResources().openRawResource(R.raw.numtext));
BufferedReader buff=new BufferedReader(is);
StringBuilder finaltext=new StringBuilder();
String line = null;
try{
while((line= buff.readLine())!=null)
{
finaltext.append(line);
}
}
catch(Exception e)
{
e.printStackTrace();
}
et.setText(finaltext);
}
}
I don't know where is a problem.
Listener doesn't catch onRinging event (so I can decide will I accept or reject incoming calls).
in manifest is this:
<uses-permission android:name="android.permission.USE_SIP" />
in main activity onCreate is this:
IntentFilter filter = new IntentFilter();
filter.addAction("android.SipDemo.INCOMING_CALL");
callReceiver = new IncomingCallReceiver();
this.registerReceiver(callReceiver, filter);
...
SipManager manager = SipManager.newInstance(this);
Intent i = new Intent();
i.setAction("android.SipDemo.INCOMING_CALL");
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, Intent.FILL_IN_DATA);
manager.open(me, pi, null);
in BroadcastReceiver class is this:
public void onReceive(Context context, Intent intent) {
SipAudioCall incomingCall = null;
try {
SipAudioCall.Listener listener = new SipAudioCall.Listener() {
...
#Override
public void onCallEnded(SipAudioCall call) {
// TODO Auto-generated method stub
super.onCallEnded(call);
}
#Override
public void onRinging(SipAudioCall call, SipProfile caller) {
try {
call.answerCall(30);
call.startAudio();
call.setSpeakerMode(true);
if(call.isMuted()) {
call.toggleMute();
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
WalkieTalkieActivity wtActivity = (WalkieTalkieActivity) context;
incomingCall = wtActivity.manager.takeAudioCall(intent, listener);
wtActivity.call = incomingCall;
...
I recieve CallEnd event and onChanged (after I end call) but I dont recieve onRinging event.
what could be a problem?
Thnx
EDIT :
I changed it all.
I put new intent filter to receiver like this (bold):
<receiver android:name=".IncomingCallReceiver" android:label="Call Receiver">
**<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>**
</receiver>
and I changed BroadcastReceiver inherit class like this (bold one):
#Override
public void onReceive(Context context, Intent intent) {
try {
**PhoneStateListener phoneListener=new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
// TODO Auto-generated method stub
Log.d("DEBUG", "Phone listener....");
String stateString = "N/A";
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
stateString = "Idle";
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
stateString = "Off Hook";
break;
case TelephonyManager.CALL_STATE_RINGING:
stateString = "Ringing";
break;
}
}
};**
WalkieTalkieActivity wtActivity = (WalkieTalkieActivity) context;
SipSession ses=wtActivity.manager.getSessionFor(intent);
**TelephonyManager telephony = (TelephonyManager) Context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);**
...
Now I get only IDLE state but still there is no ringing.
There is an error in the source code of SipAudioCall class.
To work around this issue:
incomingCall = wtActivity.manager.takeAudioCall(intent, null);
incomingCall.setListener(listener, true);
I have implemented same scenario to accept or reject incoming call in sip demo.
Create your own Activity (IncomingGui.java) with two buttons Accept and Reject.
In BroadcastReciever class call your Activity(IncomingGui.java) on incoming call event.
WalkieTalkieActivity wtActivity = (WalkieTalkieActivity) context;
incomingCall = wtActivity.manager.takeAudioCall(intent, listener);
showIncomingCallGui(intent, context);
wtActivity.call = incomingCall;
Then define following methods in BroadcastReciever class
public void showIncomingCallGui(Intent intent,Context context) {
Intent incomingCall=new Intent(context,IncomingCallGui.class);
context.startActivity(incomingCall);
}
public static void answerIncomingCall(){
try {
incomingCall.answerCall(30);
incomingCall.startAudio();
incomingCall.setSpeakerMode(true);
if (incomingCall.isMuted()) {
incomingCall.toggleMute();
}
}
catch(Exception e){
System.out.println(e.toString());
}
}
public static void rejectIncomingCall(){
try {
if (incomingCall !=null) {
incomingCall.endCall();
incomingCall.close();
}
}
catch(Exception e){
System.out.println(e.toString());
}
Then in IncomingGui.java define following methods behind "Accept" and "Reject" Buttons.
Now on incoming call you will have your own incoming call activity to accept or reject call.
public void onCallAcceptButton(View view){
IncomingCallReceiver.answerIncomingCall();
}
public void onCallRejectButton(View view){
IncomingCallReceiver.rejectIncomingCall();
this.finish();
}