我的项目是关于将安装应用程序的两部手机之间的通信。通信是 SMS 通信,每条 SMS 都包含数据,接收设备必须对其进行解释并在指定的 ListView 中排序。
该项目包含以下类:发送数据表单和显示接收到的数据的 3 个 ListView 类。
我的问题是,我不确定是否发送了我的短信(因为单击发送按钮后没有出现对话框窗口。此外,在第二个模拟器上,只要有传入的短信消息,就没有任何指示。
发送数据表:
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; } } } }
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); } } } } }