I have a project in Android using blocking a call and send sms back to the caller.
I search all here. But those codes not working. I need help.
I am using Android 2.2
Asked
Active
Viewed 1,115 times
-4
-
first, put here your code to see what's wrong, second, you can't block a call in android, only answer and close. – Buda Gavril Mar 06 '12 at 08:02
-
my app is Blocking calls and return a message to caller stating the reason for block the calls ...If not possible to block the calls,how can avoid calls. – user121 Mar 08 '12 at 05:34
-
Id ont know how to programettically close calls – user121 Mar 08 '12 at 07:35
2 Answers
1
try this code it....this is working working for me i am developing same application. use this code in oncreate method.
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener callStateListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber) {
// React to incoming call.
// number = PhoneNumberUtils.formatNumber(incomingNumber);
number = incomingNumber;
// If phone ringing
if (state == TelephonyManager.CALL_STATE_RINGING) {
new LoadStuff().execute(incomingNumber, message);
boolean isEnabled = Settings.System.getInt(
getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) == 1;
if (!isEnabled) {
Settings.System.putInt(getContentResolver(),
Settings.System.AIRPLANE_MODE_ON,
isEnabled ? 0 : 1);
// Post an intent to reload
Intent intent = new Intent(
Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", !isEnabled);
sendBroadcast(intent);
}
}
}
// If incoming call received
if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
}
if (state == TelephonyManager.CALL_STATE_IDLE) {
boolean isEnabled = Settings.System.getInt(
getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) == 1;
if (isEnabled) {
Settings.System.putInt(getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0
: 1);
// Post an intent to reload
Intent intent = new Intent(
Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", !isEnabled);
sendBroadcast(intent);
}
}
}
};
telephonyManager.listen(callStateListener,
PhoneStateListener.LISTEN_CALL_STATE);
}
and use this code outside oncreate method
public class LoadStuff extends AsyncTask<String, String, Void> {
@Override
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
String number = params[0];
String message = params[1];
boolean error = false;
try {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(number, null, message, null, null);
} catch (IllegalArgumentException e) {
error = true;
}
if (error) {
Toast.makeText(getBaseContext(), "SMS SENDING FAILED",
Toast.LENGTH_SHORT).show();
}
return null;
}
}

user2823167
- 11
- 2
1
Use this ex for sms manager
Button bttsendsms;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bttsendsms =(Button)findViewById(R.id.button1);
bttsendsms.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Toast.makeText(SmsActivity.this, "hello", 6000).show();
sendSMS("5556","hello friends");
sendSMS("5558","hello friends");
}
});
}
private void sendSMS(String phoneNumber,String message)
{
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(phoneNumber,null,message,null,null);
}
}

Kumar KL
- 15,315
- 9
- 38
- 60