如何在接听电话之前发送短信

时间:2013-01-24 14:14:04

标签: android


我使用此代码在接收此呼叫之前将短信发送到传入号码,但它不发送短信。在manifestxt文件中,我添加了这些权限

 <uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.SEND_SMS"/>

public class check extends BroadcastReceiver {


Context context = null;
 private static final String TAG = "Phone call";
 private ITelephony telephonyService;


    @Override
     public void onReceive(Context context, Intent intent) {
      Log.v(TAG, "Receving....");

      TelephonyManager telephony = (TelephonyManager) 
      context.getSystemService(Context.TELEPHONY_SERVICE);  
      try {
       Class c = Class.forName(telephony.getClass().getName());
       Method m = c.getDeclaredMethod("getITelephony");
       m.setAccessible(true);
       Bundle b=intent.getExtras();
       num=b.getString(telephony.EXTRA_INCOMING_NUMBER);
       String g=num.substring(num.length()-11,num.length());

       SmsManager smsManager = SmsManager.getDefault();
             smsManager.sendTextMessage(num,null, "rrrrrrrrr", null,null);


      } catch (Exception e) {
       e.printStackTrace();
      }

     }
  

请有人打电话给我帮助

1 个答案:

答案 0 :(得分:0)

让您的类扩展PhoneStateListener然后使用此方法。

public void onCallStateChanged(int state, String phoneNumber) {
    SmsManager sms = SmsManager.getDefault();
    String message = "";
    if (state == TelephonyManager.CALL_STATE_RINGING) {
        sms.sendTextMessage(phoneNumber, null, message, null, null);
    }
}