How to receive an incoming phone call automatically in android - android

What I want is... when an incoming call is coming it will answered automatically.
Here is my code but its not working.
public class PhoneStateReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try {
System.out.println("Receiver start");
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Toast.makeText(context, "Incoming Call State", Toast.LENGTH_SHORT).show();
Toast.makeText(context, "Ringing State Number is -" + incomingNumber, Toast.LENGTH_SHORT).show();
}
if ((state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))) {
Toast.makeText(context, "Call Received State", Toast.LENGTH_SHORT).show();
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
Toast.makeText(context, "Call Idle State", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
I hope in the RINGING function I need to do some code. But I am not getting the proper ans so I post this. Please Developers Help me out.
Thanks in advance

Related

Set Custom call activity

I would like to set my own activity infront the call screen. I have seen that there are many examples for this but with the older versions of android, while I want it to work with android 6.0 and above. This means that I have to deal with the permissions. I managed to grant the necessary permissions. After that I make a Class that inherits BroadcastReceiver so that I can detect when the phone is ringing, the only problem is that I can't send my activity infront of the call display. These are some of the classes I use:
public class PhoneStateReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
try {
System.out.println("Receiver start");
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
Toast.makeText(context, " Receiver start ", Toast.LENGTH_SHORT).show();
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Toast.makeText(context, "Ringing State Number is -", Toast.LENGTH_SHORT).show();
Intent dialogIntent = new Intent(context, LockActivity.class);
dialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
context.startActivity(dialogIntent);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class LockActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lock_screen);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
+ WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
+WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
+WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
Button btnLock = (Button) findViewById(R.id.btnUnlock);
final EditText txtPass = (EditText) findViewById(R.id.txtPass);
btnLock.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String pass = txtPass.getText().toString();
if(pass.equals("pass")||pass.equals("пасс")) {
finish();
}else{
Toast.makeText(LockActivity.this, "Wrong password!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
If anything else is needed please ask!
I managed to solve it, the problem is that it takes time to start the in-built call activity, so my activity started first and the other went on top of it. Therefore I made the current thread of my activity to sleep for less than a second. The in-built activity was launched and then my activity went on top of it.
public class PhoneStateReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
try {
System.out.println("Receiver start");
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
Toast.makeText(context, " Receiver start ", Toast.LENGTH_SHORT).show();
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Toast.makeText(context, "Ringing State Number is -", Toast.LENGTH_SHORT).show();
Intent dialogIntent = new Intent(context, LockActivity.class);
dialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
Thread.sleep(700);
context.startActivity(dialogIntent);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

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)
}

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

I am trying to delete a particular incoming number after hang up but these code not working properly

I am trying to delete a particular incoming number from call log when phone hang-up. I tried as i can do but that code not working the number still remain in call log.
it is working properly when broadcast invoke but this is not deleting number of incoming call.
Thanks in Advance.
public class ListenPhoneState extends BroadcastReceiver {
private boolean isPressOffHock = false;
static private String incomingNumberToDelete;
#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);
}
if(incomingNumberToDelete != null)
{
String queryString="NUMBER="+incomingNumberToDelete;
context.getContentResolver().delete(CallLog.Calls.CONTENT_URI,queryString,null);
Log.d("Delete", "Delete Number " + incomingNumberToDelete );
}
}
private class MyPhoneStateListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
if(state == TelephonyManager.CALL_STATE_RINGING)
{
incomingNumberToDelete = incomingNumber;
Log.d("Check", "income " + incomingNumberToDelete );
}
switch(state) {
case TelephonyManager.CALL_STATE_IDLE:
//Log.d("IDLE", state + "incoming" + incomingNumber);
isPressOffHock = true;
break;
case TelephonyManager.CALL_STATE_RINGING:
//Log.d("RINGING", state + "incoming" + incomingNumber);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
String msg = "New Phone Call Event. Incomming Number : "+incomingNumber;
// Log.d("CALL_STATE_OFFHOOK",msg );
break;
}
}
}
}
Usually there is a delay between the broadcast of a phone state and the system data that is written or system activities (such as call logs or phone ringing). Right now, your receiver is probably trying to delete the call log before it is present.
You could delay the delete or take a look at this post for registering a ContentObserver:
Delete call from call log after call end

How to know when call is made and connected?

hello im using a broadcast reciever to try to find out when a call is "made" and after the call is "made" also when the made call is "connected" i searched around and came up with this code but this works when recievng a call and making a call, all i really need is when the call is made and connected, any help in guiding me in this is appreciated, thank you for your time
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if(action.equals(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED)){
//action for phone state changed
Toast.makeText(context, "Receiver call status", Toast.LENGTH_SHORT).show();
Log.d("reciever","entered keep going");
if(telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED){
Toast.makeText(context, "Receiver call connected", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "Receiver call not connected", Toast.LENGTH_SHORT).show();
}
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED);
registerReceiver(receiver,filter);
Here you can find all states of call, just make handle this states, like i did:
public class PhoneState extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
//Some Action
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
case TelephonyManager.CALL_STATE_RINGING:
//Some Action
break;
//case TelephonyManager.ANOTHER_STATE: ...
}
}
}

Categories

Resources