Execute BroadcastReceiver when app is killed Android - android

I am doing an App that block some calls. I have a Broadcast Receiver to fix this. But when I kill my app (manually or by system), receiver doesn't work.
I have thought I need a service that execute a Broadcast Receiver, or block calls for itself. How can I do this?
Thank you for advantage.
EDIT
My receiver class:
public class IncomingCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
ITelephony telephonyService;
try {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Method m = tm.getClass().getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(tm);
if ((number != null)) {
telephonyService.endCall();
Toast.makeText(context, "Ending the call from: " + number, Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(context, "Ring " + number, Toast.LENGTH_SHORT).show();
}
if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
Toast.makeText(context, "Answered " + number, Toast.LENGTH_SHORT).show();
}
if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)) {
Toast.makeText(context, "Idle " + number, Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
My Receiver manifest:
<receiver
android:name=".ARQ.IncomingCallReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>

Related

Android - telephonyService.endCall() is delayed

I have this following BroadcastReceiver class to block incoming calls.
public class CallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
String info = "Detect Calls sample application\nOutgoing number: " + phonenumber;
Toast.makeText(context, info, Toast.LENGTH_LONG).show();
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
com.android.internal.telephony.ITelephony telephonyService = (ITelephony) m.invoke(tm);
telephonyService.endCall();
} catch (Exception e) {
System.out.println("error on end call : "+e.getMessage());
e.printStackTrace();
}
}
}
Everything is working as intended but my issue is, everytime there is a delay in telephonyService.endCall();. Meaning, I can hear a fraction of ringtone before it gets disconnected. Also, the call disconnected by endCall() is not counted as a missed call but as a incoming call. Is this the default behaviour?
Thanks for any inputs.

Send sms after call rejection - being sent multiple times

I want to send SMS after reject incoming call.
App is sending the SMS but the problem is that it is sending it twice. I am unable to figure out where problem is.
Below is the code used.
public class CallBarring extends BroadcastReceiver
{
private String number;
#Override
public void onReceive(Context context, Intent intent)
{
// If, the received action is not a type of "Phone_State", ignore it
if (!intent.getAction().equals("android.intent.action.PHONE_STATE"))
return;
// Else, try to do some action
else
{
// Fetch the number of incoming call
number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
// Check, whether this is a member of "Black listed" phone numbers stored in the database
if(MainActivity.blockList.contains(new Blacklist(number)))
{
// If yes, invoke the method
disconnectPhoneItelephony(context);
sendSMS(context);
// return;
}else{
disconnectPhoneItelephony(context);
sendSMS(context);
}
}
}
public void sendSMS(Context context) {
try{
String message = SharedPrefActivity.CommonMethod.getPrefsData(context, SharedPrefActivity.Constants.TextMessage, "");
Intent intent=new Intent(context,CallBarring.class);
PendingIntent pi=PendingIntent.getActivity(context, 0, intent,0);
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(number, null, message, pi,null);
Toast.makeText(context, "SMS Sent", Toast.LENGTH_LONG).show();
} catch (Exception e)
{
Toast.makeText(context, "SMS faild, please try again later!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
// Method to disconnect phone automatically and programmatically
// Keep this method as it is
#SuppressWarnings({ "rawtypes", "unchecked" })
private void disconnectPhoneItelephony(Context context)
{
ITelephony telephonyService;
TelephonyManager telephony = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
try
{
Class c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
telephonyService.endCall();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Try This:
// Check, whether this is a. member of "Black listed" phone. numbers stored in the database
if(MainActivity.blockList.contains(new Blacklist(number)))
{
// If yes, invoke the method
disconnectPhoneItelephony(context);
sendSMS(context);
// return;
}else{
disconnectPhoneItelephony(context);
sendSMS(context);
}
This to
// Check, whether this is a. member of "Black listed" phone. numbers stored in the database
if(MainActivity.blockList.contains(new Blacklist(number)))
{
// If yes, invoke the method
disconnectPhoneItelephony(context);
sendSMS(context)
}

blocking specific numbers (call and sms) in android

I'm creating an app in which a user can choose any number to block
I think right algorithm is : when phone rings, a function check that number is in list or not (check in database) and if there is, reject that call
i found this code in internet but it doesn't work,should i call onReceive function in my activity? what should i wrote on main activity?
here is the code:
public class IncomingCallReceiver extends BroadcastReceiver {
private ITelephony telephonyService;
private String blacklistednumber = "+458664455";
#Override
public void onReceive(Context context, Intent intent) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
ITelephony telephonyService = (ITelephony) m.invoke(tm);
Bundle bundle = intent.getExtras();
String phoneNumber = bundle.getString("incoming_number");
Log.e("INCOMING", phoneNumber);
if ((phoneNumber != null) && phoneNumber.equals(blacklistednumber)) {
telephonyService.silenceRinger();
telephonyService.endCall();
Log.e("HANG UP", phoneNumber);
}
} catch (Exception e) {
e.printStackTrace();
}
}
interface:
public interface ITelephony {
boolean endCall();
void answerRingingCall();
void silenceRinger();
}
i also added this below code to manifest
<receiver android:name="IncomingCallReceiver" >
</receiver>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permissionandroid:name="android.permission.PROCESS_INCOMING_CALLS"/>
did i miss anything?

BroadcastReceiver is not listening the phone state - Unfortunately stopped when app is not in use

I have done the call recording in my application and am calling one Activity when the phone is in Ringing state.In that Activity just am showing the phone number and person name. For that I have written the code in my class which it extends BroadcastReceiver.
This scenario is working fine when I use the application. But If I close the application then am getting Unfortunately stopped
This is my AndroidManifest.xml.
<receiver android:name="com.domyhome.broadcastreceiver.IncomingCallInterceptor" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
This is my onRecive method. When am using my application If i get a call then the current activity is not going to the background. So I have written the code to finish the Activity.
#Override
public void onReceive(Context context, Intent intent) {
LoginActivity.login.finish();
MainActivity.main.finish();
telephonyRegister(context,intent);
}
This is the functionality. I'am calling the ProjectDailogActivity when the phone state is Ringing. I'am finishing the same Activity when the call is attended or ended.
public void telephonyRegister(final Context context, Intent intent)
{
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
ctx=context;
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_IDLE)) {
try
{
ProjectDailogActivity.projdialog.finish();
}catch(Exception e)
{
e.printStackTrace();
}
if(ring==true&&callReceived==false)
{
Toast.makeText(context, "THIS IS MISSED CALL FROM"+phoneNumber, Toast.LENGTH_LONG).show();
String smsmessage = "Thanks for contacting us. We will get back you soon";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, "Dear Customer!"+" "+smsmessage, null, null);
Log.i("sms",smsmessage);
Toast.makeText(context, "SMS sent.",
Toast.LENGTH_LONG).show();
callReceived=false;
}
else if(callReceived==true)
{
try
{
ProjectDailogActivity.projdialog.finish();
}catch(Exception e)
{
e.printStackTrace();
}
if(Conntact.isAvailable(phoneNumber))
{
try{
myAudioRecorder.release();
myAudioRecorder.stop();
myAudioRecorder = null;
}
catch(Exception e)
{
System.out.print("error");
}
Toast.makeText(context,"Recording Stopped", Toast.LENGTH_SHORT).show();
}
}
}
else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_OFFHOOK))
{
try{
ProjectDailogActivity.projdialog.finish();
}catch(Exception e)
{
e.printStackTrace();
}
dir = Environment.getExternalStorageDirectory();
try {
file = File.createTempFile("sound", ".mp3", dir);
} catch (IOException e) {
e.printStackTrace();
return;
}
myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(file.getAbsolutePath());
if(Conntact.isAvailable(phoneNumber))
{
callReceived = true;
ring=false;
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(context, "recording started", Toast.LENGTH_SHORT).show();
}
else
{
}
}
else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_RINGING)){
phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
ring = true;
System.out.println("phonenum"+Conntact.isAvailable(phoneNumber));
if(!Conntact.isAvailable(phoneNumber))
{
Toast.makeText(ctx, phoneNumber+"not available", Toast.LENGTH_SHORT).show();
}
else if(Conntact.isAvailable(phoneNumber))
{
Intent intent11 = new Intent(ctx,ProjectDailogActivity.class);
intent11.putExtra("incoming_number",phoneNumber);
intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(intent11);
}
}
}
Conntact is a table where already some numbers has been stored. So when I get a call just am checking whether the calling number is available in the Conntact table.
If it is available then show the ProjectDailogActivity. If the number is available and the call is also picked means then do the recording. Everything is working fine . But it shows Unfortunately stopped when my app is not in use.
Consider unregistering for broadcast events while quitting from your app,Click here for a SO answer on same issue

how to end up my outgoing call.

with statrtActivity(callIntent), call goes and then i have to wait for few seconds and end automatically. to end up my call i have taken mycalss extends Broadcastreceiver then in that onreceive() i implemented.in that method i got only to set old number and newnumber and toast is printing.
What i want exactly is to end call what i need to write. and how to call onreceive method from my class? plase help me. i didnt get anywhere.
#Override
public void onReceive(Context context, Intent intent) {
final String oldNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
this.setResultData(newPhNnumber);
final String newNumber = this.getResultData();
if((newNumber!=null)&&(newNumber!=oldNumber))
{
String msg = "Intercepted outgoing call. Old number " + oldNumber + ", new number " + newNumber;
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
this.abortBroadcast();----> what it does?
}
You may try this:
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
ITelephony telephonyService = (ITelephony) m.invoke(tm);
telephonyService.endCall();
} catch (Exception e) {
e.printStackTrace();
}
you simply abort outgoing call through following code
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction()
.equals("android.intent.action.NEW_OUTGOING_CALL")) {
phoneNo = intent
.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
if (phoneNo.isBlocked(blockNo)) {
setResultData(null);
}
}
}

Categories

Resources