попробуйте этот код, это .... это работает, работает для меня, я разрабатываю то же самое приложение.используйте этот код в методе oncreate.
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);
}
и используйте этот код вне метода oncreate
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;
}
}