2011-12-24 81 views
0

的默認消息查看器中發送短信我已經創建了一個應用程序,分析正在接收的SMS並將消息發送回發件人。 但是那些由我的手機自動發送的短信不會顯示在默認的信息查看器中。如何完成它?顯示在Android

這是我用它來發送短信

package com.lalsoft.janko; 

import java.util.Calendar; 

import android.content.BroadcastReceiver; 
import android.content.ContentValues; 
import android.content.Context; 
import android.content.Intent; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.telephony.gsm.SmsManager; 
import android.telephony.gsm.SmsMessage; 
import android.util.Log; 
import android.view.View; 


public class SMSReceiver extends BroadcastReceiver 
{ 
public String SendMsgBody; 


private static final String LOG_TAG = "SMSReactor"; 

@Override 
public void onReceive(Context context, Intent intent) 
{ 

    //---get the SMS message passed in--- 
    Bundle bundle = intent.getExtras();   
    SmsMessage[] msgs = null; 
    String str = ""; 
    String PhNo=""; 
    String MsgBody=""; 
    if (bundle != null) 
    { 
     //---retrieve the SMS message received--- 
     Object[] pdus = (Object[]) bundle.get("pdus"); 
     msgs = new SmsMessage[pdus.length];    
     for (int i=0; i<msgs.length; i++) 
     { 
      msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);     
      str += "SMS from " + msgs[i].getOriginatingAddress(); 
      PhNo=msgs[i].getOriginatingAddress(); 
      str += " :"; 
      str += msgs[i].getMessageBody().toString(); 
      MsgBody=msgs[i].getMessageBody().toString(); 
      str += "\n"; 
     } 


     if(MsgBody.equalsIgnoreCase("Test")) 
     { 
       SendSMS(PhNo,"Got you!!...Missile Launch initiated.Your location has been traced and the missile will hit your place in approximatly 2 Mins.. ESCAPE!!"); 
      //SendSMS2(PhNo,"Got you!!...Missile Launch initiated.Your location has been traced and the missile will hit your place in approximatly 2 Mins.. ESCAPE!!"); 
     } 

} 



/* public void SendSMS2(String phonenumber,String message) 
{ 
    startActivity(new Intent(Intent.ACTION_VIEW,Uri.fromParts(message, phonenumber, null))); 
} 
*/ 
private void SendSMS(String phonenumber,String message) 
{ 
    SmsManager manager = SmsManager.getDefault(); 
    manager.sendTextMessage(phonenumber, null, message, null, null); 
} 

} 

這裏當收到帶有「測試」上寫短信的代碼,我的應用程序發送短信回sender..but在這個SendSMS方法,它發送SMS,但發送消息沒有顯示在本機/默認SMS應用程序中。我應該怎麼做才能將此短信通過手機發送回發件人,並顯示在本機SMS應用程序中。

+0

代碼請。沒有代碼,我們不能承擔任何事情。 – 2011-12-24 07:34:17

+0

這是我用來發送短信的代碼.. private void SendSMS(String phonenumber,String message) { SmsManager manager = SmsManager.getDefault(); manager.sendTextMessage(phonenumber,null,message,null,null); } – Shijilal 2011-12-24 08:24:18

+0

感謝您的鏈接..我正在尋找第一種方法..但是startactivity顯示errror ..「方法startActivity(意圖)是未定義的類型SMSReceiver」 – Shijilal 2011-12-25 05:50:34

回答

0

最後,我得到了成功..雖然我已經閱讀過可能的地方這種方法不應該在很多地方使用.. like this我在這裏使用這種方法..如果任何人使用此代碼,然後請通知關於第一個問題..

這裏是我的代碼..

private void SendSMS(Context c,String phonenumber,String message) 
{ 
    SmsManager manager = SmsManager.getDefault(); 
    manager.sendTextMessage(phonenumber, null, message, null, null); 

    //For displaying in Outbox 
    ContentValues values = new ContentValues(); 
    values.put("address", phonenumber); 
    values.put("body", SendMsgBody); 
    c.getContentResolver().insert(Uri.parse("content://sms/sent"), values); 
} 

我打電話從這部分這種方法(在這個問題本身顯示完整的代碼)。其他方面在我上面的代碼中添加..

if(MsgBody.equalsIgnoreCase("Test")) 
    { 
      SendSMS(context,PhNo,"Got you!!...Missile Launch initiated.Your location has been traced and the missile will hit your place in approximatly 2 Mins.. ESCAPE!!"); 

    } 

就是這樣..希望它可以幫助別人喜歡我..