-1

私は1つの動的SMS受信受信機を作成しましたが、あなたの助けが必要です...私は1台の携帯電話からポート番号24685でSMSを送信し、他の携帯電話で以下のコードを実行していますが、SMSを受信して​​いない理由がわかりませんそのポート..参考までに、最初の携帯電話で配信レポートを取得していますが、受信者はそのメッセージを検出できません....

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());
      }
    }

}

4

1 に答える 1

0

リンクの下にある次の 2 つの質問の回答を試してください。

内部クラスとしての BroadcastReceiver

ブロードキャスト レシーバーをプログラムで登録する

于 2012-04-25T11:16:59.520 に答える