Android:发送数据短信的问题
Posted
技术标签:
【中文标题】Android:发送数据短信的问题【英文标题】:Android:Problem with sending Data Sms 【发布时间】:2011-06-11 21:36:52 【问题描述】:我已经编写了发送加密消息的代码。 但是加密数据 SMS 没有传递,因为 onrecive() 方法没有调用。我认为我的接收器部分有问题。
现在,我是 android 的新手。任何人都可以帮助我以下代码有什么问题(我使用的是 RSA 算法)?提前谢谢........
发送:
public class MainMethod extends Activity
Button btnSendSMS;
EditText txtPhoneNo;
EditText txtMessage;
Button stop ;
static boolean bombing = false;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
txtMessage = (EditText) findViewById(R.id.txtMessage);
btnSendSMS.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
try
KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5");
SecretKey MD5key = keyGen.generateKey();
FileOutputStream fos = openFileOutput( "MD5key.txt" ,Context.MODE_WORLD_READABLE);
fos.write(MD5key.getEncoded());
catch(Exception e)
;
String phoneNo = txtPhoneNo.getText().toString();
String message = txtMessage.getText().toString();
if (phoneNo.length()>0 && message.length()>0)
try
stop = (Button)findViewById(R.id.btnSendSMS);
BigInteger ptext = new BigInteger(message.getBytes());
BigInteger ciphertext = encrypt(ptext);
message = ciphertext.toString();
stop.setText("Message Sent");
SMSbomb(phoneNo,message );
catch(Exception e)
;
else
stop = (Button)findViewById(R.id.btnSendSMS);
stop.setText("Send Properly");
Toast.makeText(getBaseContext(), "Please enter both phone number and message.", Toast.LENGTH_SHORT).show();
);
private void SMSbomb(String phoneNumber, String message)
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
registerReceiver(new BroadcastReceiver()
@Override
public void onReceive(Context context, Intent intent)
switch (getResultCode())
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_SHORT).show();
stop.setText("Proceeding");
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show();
MainMethod.endBombing();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service", Toast.LENGTH_SHORT).show();
MainMethod.endBombing();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
MainMethod.endBombing();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_SHORT).show();
MainMethod.endBombing();
break;
, new IntentFilter(SENT));
registerReceiver(new BroadcastReceiver()
@Override
public void onReceive(Context arg0, Intent arg1)
switch (getResultCode())
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
stop.setText("Send Message");
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendDataMessage(phoneNumber , null, (short) 3492, message.getBytes(), sentPI, deliveredPI);
stop.setText("Send Message");
public static void endBombing()
bombing = false;
public static synchronized String encrypt(String message)
BigInteger e = new BigInteger("e") ;
BigInteger n = new BigInteger(n");
return (new BigInteger(message.getBytes())).modPow(e, n).toString();
public static synchronized BigInteger encrypt(BigInteger message)
BigInteger e = new BigInteger("e") ;
BigInteger n = new BigInteger("n");
return message.modPow(e, n);
//////////
接收:
公共类 SmsReceiver 扩展了 BroadcastReceiver
private static final String TAG = "MySmsReceiver";
public void onReceive(Context context, Intent intent)
Log.i(TAG, "Recieved a message");
String ds = "" ;
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
if (bundle != null)
Object[] pdusObj = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdusObj.length];
// getting SMS information from PDU
for (int i = 0; i < pdusObj.length; i++)
messages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
for (SmsMessage currentMessage : messages)
if (!currentMessage.isStatusReportMessage())
String messageBody = currentMessage.getDisplayMessageBody();
byte[] messageByteArray = currentMessage.getPdu();
// skipping PDU header, keeping only message body
int x = 1 + messageByteArray[0] + 19 + 7;
String str = new String(messageByteArray, x, messageByteArray.length-x);
ds = currentMessage.getOriginatingAddress() ;
//---display the new SMS message---
String plaintext = decrypt(str);
String plaintext1 = "SMS from " + ds + " Message " + plaintext ;
Toast t = Toast.makeText(context, plaintext1, Toast.LENGTH_LONG);
t.setDuration(30);
t.setGravity(Gravity.CENTER_VERTICAL, 50, 50);
t.show();
//这里的e和n是RSA的共享密钥。 公共静态同步字符串解密(字符串消息) 字符串 ds = "e" ; 字符串 ns = "n" ; 大整数 d = 新大整数(ds); 大整数 n = 新大整数(ns); return new String((new BigInteger(message)).modPow(d, n).toByteArray());
public static synchronized BigInteger decrypt(BigInteger message)
String ds = "e" ;
String ns = "n" ;
BigInteger d = new BigInteger(ds) ;
BigInteger n = new BigInteger(ns) ;
return message.modPow(d, n);
【问题讨论】:
【参考方案1】:您只需注册一次接收器。
而是在onCreate()
中注册您在SMSbomb(phoneNo,message );
中的接收器。
希望对您有所帮助!
【讨论】:
以上是关于Android:发送数据短信的问题的主要内容,如果未能解决你的问题,请参考以下文章
Android AdapterActivity回传数据发送短信