我创建了一个动态短信收到的接收器,但它不工作需要你的帮助...... 我从我的一部手机发送短信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());
}
}
}
答案 0 :(得分:0)