我已经创建了一个动态短信接收接收器,但它不工作需要你的帮助......我正在从我的一部手机上的端口号 24685 上发送短信,并在另一部手机上运行下面提到的代码,但不知道为什么我没有收到任何短信那个端口..为了您的信息,我在我的第一部手机上收到了交付报告,但我的接收器无法检测到该消息....
public class TestingSMSReceiverActivity extends Activity {
private Context context;
private MessageReceivedReceiver messageReceivedReceiver;
private String port="24685";
private short sendPort= 24685;
private TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView) findViewById(R.id.tv);
context=getApplicationContext();
openReceiver();
}
public class MessageReceivedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
showLog("MessageReceivedReceiver Message Received");
Toast.makeText(context, "SMS Received", Toast.LENGTH_SHORT).show();
try
{
switch (getResultCode()) {
case Activity.RESULT_OK:
Bundle bundle = arg1.getExtras();
SmsMessage[] msgs = null;
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]);
byte[] data = msgs[i].getUserData();
String receivedTextMessage=new String(data);
Toast.makeText(context, ""+msgs.length+" "+receivedTextMessage, Toast.LENGTH_SHORT).show();
processReceivedSMS(receivedTextMessage);
}
}
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
showLog("RESULT_ERROR_GENERIC_FAILURE");
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
showLog("RESULT_ERROR_NO_SERVICE");
break;
}
}
catch(Exception e)
{
Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT).show();
}
}
}// end of public class messageReceivedReceiver
private void openReceiver()
{
try
{
showLog("Open Receiver");
IntentFilter filter = new IntentFilter();
filter.addDataScheme("sms");//Try commenting the same for text msg
filter.addDataAuthority("localhost", port);
filter.addAction("android.intent.action.DATA_SMS_RECEIVED");//Binary Msg
if(messageReceivedReceiver==null){
messageReceivedReceiver=new MessageReceivedReceiver();
showLog("new MessageReceivedReceiver instance created");
}
context.registerReceiver(messageReceivedReceiver,
filter);
}
catch(Exception e)
{
showLog("openReceiver "+e.toString());
}
}
private void showLog(String string) {
// TODO Auto-generated method stub
Log.d("SMSReceiver", string);
}
private void processReceivedSMS(String receivedTextMessage)
{
unregisterReceivers();
showLog("SMS receivedTextMessage= "+receivedTextMessage);
try
{
tv.append(receivedTextMessage);
showLog("Received Message "+receivedTextMessage);
}
catch(Exception e)
{
showLog("processReceivedSMS "+e.toString());
}
}//end of processReceivedSMS
private void unregisterReceivers() {
try {
if(messageReceivedReceiver!=null){
context.unregisterReceiver(messageReceivedReceiver);
showLog("Unregister Received Receiver");
}
} catch (Exception e) {
showLog("unregisterReceivers "+e.toString());
}
}
}