SMSManager和国家/地区代码前缀

时间:2012-04-06 13:02:12

标签: java android

我正在编写关于短信的编程,我有服务和广播接收器,当发送短信时,我得到“displayoriginatingadress,然后它自动重新发送另一个短信到源号码,但我有一个错误/强制关闭无效的目的地地址因为当我得到源号码(我来自法国)时它是这样的:“+ 33617890922”但我需要得到它像这样的“0617890922”,我该怎么办呢? **任何帮助????

public void onReceive(Context c, Intent intent) {
    // TODO Auto-generated method stub


 mContext = c;




          Bundle pudsBundle = intent.getExtras();

             Object[] pdus = (Object[]) pudsBundle.get("pdus");
             SmsMessage messages =SmsMessage.createFromPdu((byte[]) pdus[0]);    

             Toast.makeText(c, "Service SMS Started OK !", Toast.LENGTH_LONG).show();
             Toast.makeText(c, "SMS Received : "+messages.getMessageBody(),
             Toast.LENGTH_LONG).show();
             String str = messages.getMessageBody();
             String num = messages.getDisplayOriginatingAddress();

         String str1= PhoneNumberUtils.formatNanpNumber(num);
 if(messages.getMessageBody().contentEquals("klmj")){

                 if (intent.getAction().equals(ACTION_RECEIVE_SMS)){







                    Intent serviceIntent = new Intent(mContext, ServiceLocation.class);
                    serviceIntent.putExtra("id", str1);


                    mContext.startService(serviceIntent);
             }

.... .... .... ....


好吧问题不是来自“+33”因为我试图用“+33677890098”替换目标地址发送短信及其工作,我丢失了,这是我从我的广播接收器得到的数据是否已损坏?这里有一些代码我也不知道如何使用我发现的名为“phoneutil”的外部库,我认为,我的问题来自我的意图中收到的数字?我试着把报价没有运气,同样的问题使用一些String操作... wtf请帮助谢谢你

更新:我注意到在“lo”方法中使用字符串“str”为空,请参阅下面的代码修改:

public class ServiceTe extends Service{
private final String ACTION_RECEIVE_SMS =  "android.provider.Telephony.SMS_RECEIVED";  
private String initData;
int myLatitude , myLongitude;
private String loc;
private String str;
private String num;
public void onCreate(){

lo();


}
public int onStartCommand(Intent itn, int num1, int flag){

 str = itn.getStringExtra("id");
 Toast.makeText(this, "One !" +str, Toast.LENGTH_LONG).show();



return 0;
}

public void lo(){
    // TODO Auto-generated method stub



     Toast.makeText(this, "Two !", Toast.LENGTH_LONG).show();
     TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
     GsmCellLocation cellLocation = (GsmCellLocation) telephonyManager.getCellLocation();

        //This Toast show : "" , Nothing...

    Toast.makeText(this, str, Toast.LENGTH_LONG).show();

        int cid = cellLocation.getCid();
        int lac = cellLocation.getLac();

        if(RqsLocation(cid, lac)){


      loc = (
              String.valueOf((float)myLatitude/1000000)
              + " : "
              + String.valueOf((float)myLongitude/1000000));
    Its doesnt work like this,Force Close , and invalid DestinationAddress error
    SmsManager.getDefault().sendTextMessage(str OR this :        itn.getStringExtra("id"); , null, loc, null, null);

        }else{ Its work like this !
            SmsManager.getDefault().sendTextMessage("+33643598356", null, "Cant Report Location", null, null);
        };

1 个答案:

答案 0 :(得分:1)

好的我已经修复了问题,感谢您的帮助,请看下面的代码:

 public class ServiceTe extends Service{
 private final String ACTION_RECEIVE_SMS =  "android.provider.Telephony.SMS_RECEIVED";  
 private String initData;
 int myLatitude , myLongitude;
 private String loc;
 private String str;
 private String num;
 public void onCreate(){




 }
 public int onStartCommand(Intent itn, int num1, int flag){

  str = itn.getStringExtra("id");
  Toast.makeText(this, "One !" +str, Toast.LENGTH_LONG).show();

 //Call here lo with str as argument: 
 lo(str);


 return 0;
 }

 //Added a string here to receive the Data String from the intent(str)...

 public void lo(String num){
// TODO Auto-generated method stub

 //Thats all i have my data from my intent avaible here !


 Toast.makeText(this, "Two !", Toast.LENGTH_LONG).show();
      TelephonyManager telephonyManager =         (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
 GsmCellLocation cellLocation = (GsmCellLocation) telephonyManager.getCellLocation();


    int cid = cellLocation.getCid();
    int lac = cellLocation.getLac();

    if(RqsLocation(cid, lac)){


  loc = (
          String.valueOf((float)myLatitude/1000000)
          + " : "
          + String.valueOf((float)myLongitude/1000000));
Its doesnt work like this,Force Close , and invalid DestinationAddress error
  SmsManager.getDefault().sendTextMessage(str, null, loc, null, null);

    }else{ Its work like this !
        SmsManager.getDefault().sendTextMessage("+33643598356", null, "Cant Report Location", null, null);
    };