2013-01-24 60 views
0

我使用此代碼在接收此呼叫之前向來電號碼發送短信,但不發送短信。在manifext文件I中添加這些權限如何在收到來電之前發送短信

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

    } 

請打電話的人幫我

+0

它是如何工作的?沒有文字發送?有些錯誤? – keyser

+0

無論如何,t1是什麼? – njzk2

+0

dosenot發送消息 –

回答

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