Вы можете использовать ITelephone.aidl и размещать события для прослушивания состояния телефона.Я помогаю вам в обработке телефонных звонков (включая короткий фрагмент о том, как его отключить).СМС тоже бывает аналогичным образом.Было бы лучше, если бы вы могли исследовать и выяснить это
import com.android.internal.telephony.ITelephony;
import android.os.IBinder;
import android.widget.Toast;
import android.telephony.TelephonyManager;
import android.telephony.PhoneStateListener;
public class CallMonitor
{
protected StateListener phoneStateListener;
//stops the service to monitor any call.
public void stopMonitor()
{
try
{
TelephonyManager telephonymanager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
telephonymanager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
phoneStateListener=null;
Toast.makeText(this, "Call Monitoring Stopped", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
}
}
public void startMonitor()
{
try
{
phoneStateListener = new StateListener();
TelephonyManager telephonymanager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
telephonymanager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
Toast.makeText(this, "Call Monitoring Started", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
e.printStackTrace();
}
}
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
class StateListener extends PhoneStateListener
{
@Override
public void onCallStateChanged(int state, String incomingNumber)
{
super.onCallStateChanged(state, incomingNumber);
switch(state)
{
case TelephonyManager.CALL_STATE_RINGING:
Context context = getApplicationContext();
try
{
TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
Class c = Class.forName(manager.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
ITelephony telephony = (ITelephony)m.invoke(manager);
telephony.endCall();
}
catch(Exception e)
{
Toast.makeText(context, "Error ending the call" + e.getMessage(), Toast.LENGTH_LONG).show();
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
case TelephonyManager.CALL_STATE_IDLE:
break;
}
}
};
}
Разрешения
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />