我正在编写一个关于SMS的程序,我有一个服务,一个广播接收器,当我发送一条短信时,我得到"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“的外部库,我想,我的问题是我收到的号码来自我的意图吗?我试着无缘无故地引用,同样的问题是使用字符串操作.请帮忙谢谢你
更新:我注意到字符串"str“在"lo”方法中使用时为空,请参见下面的代码修改:
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);
};发布于 2012-04-08 13:53:45
好的,我修复了这个问题,谢谢您的帮助,请看下面的代码:
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);
};https://stackoverflow.com/questions/10043843
复制相似问题