无法在2个仿真器之间发送SMS

时间:2013-11-09 07:41:09

标签: android listview sms broadcastreceiver smsmanager

我的项目是关于将安装应用程序的2部手机之间的通信。 通信是SMS通信,每个SMS包含数据,接收设备必须解释它并在指定的ListView中排序 该项目包含以下类:发送数据表和显示接收数据的3个ListView类。

我的问题是,我不确定我的短信是否已发送(因为单击发送按钮后没有出现对话框窗口。此外,在第二个模拟器上没有只要有传入的短信,就会显示。

  1. 发送数据表:

    import android.app.Activity;
    import android.os.Bundle;
    import android.telephony.SmsManager;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class AddContactDeatils extends Activity implements OnClickListener {
    Button sendToParallel, sendToParallel2;
    TextView nameT, idT, phoneT;
    EditText nameF, idF, phoneF;
    int recipient;
    
    
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_contact_form);
    sendToParallel=(Button)findViewById(R.id.sendToParallel);
    sendToParallel.setOnClickListener(this);
    sendToParallel2=(Button)findViewById(R.id.sendToParallel2);
    sendToParallel2.setOnClickListener(this);
    nameT=(TextView)findViewById(R.id.nameTitle);
    idT=(TextView)findViewById(R.id.idTitle);
    phoneT=(TextView)findViewById(R.id.phoneTitle);
    nameF=(EditText)findViewById(R.id.nameField);
    idF=(EditText)findViewById(R.id.idField);
    phoneF=(EditText)findViewById(R.id.phoneField);
    
    
        }
        protected void sendSms(String phoneNumber, String message) {
    // TODO Auto-generated method stub
    SmsManager sms=SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, null, null);
        }
    
        public void checkRecipient(EditText et)
        {
    try {
        recipient=Integer.parseInt(et.toString());
    } catch (Exception e) {
        // TODO: handle exception
        Toast.makeText(this, "Only numbers are allowed", Toast.LENGTH_LONG).show();
        et.setText("");
    }
        }
    
        @Override
        public void onClick(View v) {
    // TODO Auto-generated method stub
    int id=v.getId();
    
     String message = null;
     String name=nameF.toString();
     message+=name+";";
     String idTxt=idF.toString();
     message+=idTxt+";";
     String phone=phoneF.toString();
     message+=phone;
    
    switch(id)
    {
    case R.id.sendToParallel:
    {
    
        sendSms("5554", message) ; 
    
    
        break;
    }
    case R.id.sendToParallel2:
    {
        sendSms("5556", message) ; 
        break;
    }
    }
    
        }
    
            }
    
  2. 3 ListViews显示收到的数据

     import java.util.ArrayList;
     import android.app.Activity;
     import android.app.ListActivity;
     import android.content.BroadcastReceiver;
     import android.content.Context;
     import android.content.Intent;
     import android.os.Bundle;
     import android.telephony.SmsMessage;
     import android.view.Menu;
     import android.view.MotionEvent;
     import android.view.View;
     import android.view.View.OnClickListener;
     import android.view.View.OnTouchListener;
     import android.widget.ArrayAdapter;
     import android.widget.ListView;
    
     public class DataLists extends Activity implements OnClickListener  {
     ListView idList, namesList, phonesList;
     MyReciever mr;
     ArrayList<String>ids= new ArrayList<String>();
     ArrayList<String>names=new ArrayList<String>();
     ArrayList<String>phones=new ArrayList<String>();
    
     ArrayAdapter<String> idAdapter, namesAdapter, phonesAdapter;
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.details_lists);
    idList=(ListView)findViewById(R.id.idList);
    namesList=(ListView)findViewById(R.id.namesList);
    phonesList=(ListView)findViewById(R.id.phonesList);
    idAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,ids );
    idList.setAdapter(idAdapter);
    namesAdapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,  names);
    namesList.setAdapter(namesAdapter);
    phonesAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, phones);
    phonesList.setAdapter(phonesAdapter);
    
    
        }
            public void addItemToIdList(String st)
            {
            ids.add(st);
            idAdapter.notifyDataSetChanged();
            }
    
            public void addItemToNamesList(String st)
            {
        names.add(st);
            namesAdapter.notifyDataSetChanged();
            }
    
            public void addItemToPhonesList(String st)
            {
            phones.add(st);
            phonesAdapter.notifyDataSetChanged();
            }
    
    
    
       @Override
       public void onClick(View v) {
    // TODO Auto-generated method stub
    
       }
    
       private class MyReciever extends BroadcastReceiver{
    
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Bundle bundle=intent.getExtras();
        SmsMessage[]msgs=null;
        if(bundle!=null)
        {
            Object[]pdus=(Object[]) bundle.get("pdus");
            msgs=new SmsMessage[pdus.length];
            for(int i=0;i<msgs.length;i++)
            {
                int index=0, prev=0;
                String msgBody=msgs[i].getMessageBody().toString();
                 index=msgBody.indexOf(';');
                 prev=index;
                 String name=msgBody.substring(0, index);
                 addItemToNamesList(name);
                 msgBody=msgBody.substring(index+1);
                 index=msgBody.indexOf(';');
                 String id=msgBody.substring(prev, index);
                 addItemToIdList(id);
                 msgBody=msgBody.substring(index+1);
                 String phone=msgBody;
                 addItemToPhonesList(phone);
    
            }
        }
       }
    
         }
    
    
    
          }
    

1 个答案:

答案 0 :(得分:0)

添加发送和接收短信的权限

   <uses-permission android:name="android.permission.SEND_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_SMS" >
</uses-permission>

和Run Two Emulator for Send Sms ..例如5554到5556