To send message from a emulator send to another emulator, to be noticed have network in emulators.

(emulator 的状态列上有 3G 的符号)

Cast:

MySMS.java

package sdt.android.mysms; import android.app.Activity; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.EditText; import android.widget.Button; import android.widget.Toast; import android.telephony.SmsManager; import android.util.Log; public class MySMS extends Activity { /** Called when the activity is first created. */ private EditText telNum; private EditText msgText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.d("Debug", "MySMS: onCreate"); telNum = (EditText) findViewById(R.id.telNumText_send); msgText = (EditText) findViewById(R.id.message_copntent_send); Button sendBtn = (Button) findViewById(R.id.send_button_send); sendBtn.setOnClickListener(new OnClickListener() { public void onClick(View v) { Log.d("Debug", "MySMS: onClick"); String telNumStr = telNum.getText().toString(); String msgStr = msgText.getText().toString(); Log.d("Debug", "MySMS: " + telNumStr); Log.d("Debug", "MySMS: " + msgStr); SmsManager sms = SmsManager.getDefault(); if (telNumStr.trim().length()>0 && msgStr.trim().length()>0) { try { PendingIntent pi = PendingIntent.getBroadcast(MySMS.this, 0, new Intent(), 0); sms.sendTextMessage(telNumStr, null, msgStr, pi, null); } catch (Exception e) { Toast.makeText(MySMS.this, "Please input phone number and content again!", Toast.LENGTH_LONG).show(); } Toast.makeText(MySMS.this, "Send it successful!", Toast.LENGTH_SHORT).show(); telNum.setText(""); msgText.setText(""); } } }); } }

main.xml

<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout android:id="@+id/widget40" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:id="@+id/tel_num_send" android:layout_width="wrap_content" android:layout_height="25px" android:text="Tel Number:" android:layout_x="10px" android:layout_y="22px"> </TextView> <EditText android:id="@+id/telNumText_send" android:layout_width="197px" android:layout_height="35px" android:text="" android:textSize="18sp" android:layout_x="90px" android:layout_y="12px"> </EditText> <EditText android:id="@+id/message_copntent_send" android:layout_width="319px" android:layout_height="86px" android:text="" android:textSize="18sp" android:layout_x="0px" android:layout_y="112px"> </EditText> <Button android:id="@+id/send_button_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send" android:layout_x="120px" android:layout_y="212px"> </Button> <TextView android:id="@+id/message_content_text_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Input Message" android:layout_x="10px" android:layout_y="82px"> </TextView> </AbsoluteLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="sdt.android.mysms" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MySMS" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.SEND_SMS" /> </manifest>

Screenshot:

Android Mobile Application - Message casting and receiving_第1张图片

Receive:

To create BroadcastReceiver listening the event android.provider.Telephony.SMS_RECEIVED

MySMSReceiver.java

package sdt.android.mysms; import android.app.Activity; import android.os.Bundle; import android.content.BroadcastReceiver; import android.telephony.SmsMessage; import android.app.PendingIntent; import android.content.Intent; import android.view.View; import android.view.View.OnClickListener; import android.widget.EditText; import android.widget.Button; import android.widget.Toast; import android.telephony.SmsManager; import android.util.Log; import android.content.Context; import sdt.android.mysms.MySMSDisplay; public class MySMSReceiver extends BroadcastReceiver { private static final String strACT = "android.provider.Telephony.SMS_RECEIVED"; public void onReceive(Context context, Intent intent) { if(intent.getAction().equals(strACT)) { StringBuilder sb = new StringBuilder(); Bundle bundle = intent.getExtras(); Log.d("SDTDebug", "onReceive"); if (bundle != null) { Object[] pdus = (Object[]) bundle.get("pdus"); SmsMessage[] msg = new SmsMessage[pdus.length]; //Log.d("SDTDebug", "PUDS LEN:" + (int)pdus.length); for (int i=0; i<(int)pdus.length; i++) { msg[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); //sb.append("From: "); String address = msg[i].getDisplayOriginatingAddress(); sb.append(address + ":").append(msg[i].getDisplayMessageBody()); } //display_intent.setClass(MySMS.this, MySMSDisplay.class); Intent display_intent = new Intent(context, MySMSDisplay.class); display_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Log.d("SDTDebug", "Send Message: " + sb.toString()); //Bundle display_bundle = new Bundle(); //display_bundle.putString("message", sb.toString()); display_intent.putExtra("message", sb.toString()); context.startActivity(display_intent); } } } }


MySMSDisplay.java : To create a activity while receiving message.

package sdt.android.mysms; import android.app.Activity; import android.app.PendingIntent; import android.widget.EditText; import android.os.Bundle; import android.widget.Button; import android.widget.ProgressBar; import android.widget.Toast; import android.content.Intent; import android.telephony.SmsManager; import android.view.View.OnClickListener; import android.view.View; import android.util.Log; public class MySMSDisplay extends Activity { private EditText msg_get; private EditText msg_replyto; private Button btnReply; private ProgressBar pb; private Intent intent; private String message; private String telNum; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.display); Log.d("SDTDebug", "MySMSDisplay-onCreate"); msg_get = (EditText) findViewById(R.id.msg_get); msg_replyto = (EditText) findViewById(R.id.msg_replyto); btnReply = (Button) findViewById(R.id.replyto); pb = (ProgressBar) findViewById(R.id.progressbar); pb.setVisibility(ProgressBar.INVISIBLE); intent = getIntent(); String msg = intent.getStringExtra("message"); String[] msgstrs = msg.split(":"); if(msgstrs.length == 2) { telNum = msgstrs[0]; message = msgstrs[1]; } msg_get.append(msg); msg_get.append("/n"); btnReply.setOnClickListener(new OnClickListener() { public void onClick(View v) { pb.setVisibility(ProgressBar.VISIBLE); String replyMessage = msg_replyto.getText().toString(); SmsManager smsmanager = SmsManager.getDefault(); if (replyMessage.trim().length() > 0) { try { PendingIntent pi = PendingIntent.getBroadcast(MySMSDisplay.this, 0, new Intent(), 0); smsmanager.sendTextMessage(telNum, null, replyMessage, pi, null); msg_get.append(replyMessage); msg_get.append("/n"); msg_replyto.setText(""); } catch (Exception e) { Toast.makeText(MySMSDisplay.this, "Please type message you want to reply again!", Toast.LENGTH_SHORT); } pb.setVisibility(ProgressBar.INVISIBLE); } } }); } }

MySMSDisplay's layout: display.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/tvgotmsg" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Got message: " /> <EditText android:id="@+id/msg_get" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/tvreplymsg" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Reply content: " /> <EditText android:id="@+id/msg_replyto" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/replyto" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Reply" /> <ProgressBar android:id="@+id/progressbar" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>

To Add permission and activity claiming in AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="sdt.android.mysms"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MySMS" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="MySMSDisplay" android:label="@string/app_name" /> <receiver android:name="MySMSReceiver"> <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver> </application> <uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.SEND_SMS" /> </manifest>

Screenshot:

1. Send message from emulator #5554 to #5556

Android Mobile Application - Message casting and receiving_第2张图片

2. Emulator #5556 got message from #5554

Android Mobile Application - Message casting and receiving_第3张图片

更多相关文章

  1. Android 图片相关
  2. android 图片点击一下就放大到全屏,再点一下就回到原界面
  3. Android图片压缩、加水印
  4. Android获取网页上的图片的代码
  5. android 使用imageloader 获取图片时,如果需要带cookie
  6. android 图片叠加效果——两种方法
  7. Android TabHost 文字及图片的设置
  8. android调用系统邮件发送图片文本
  9. android之【代码实现ImageView图片切换】

随机推荐

  1. ButterKnife源码解析
  2. Android(安卓)IPC之ContentProvider和Soc
  3. Android(安卓)Launcher 设置壁纸
  4. GitHub 上排名前 100 的 Android(安卓)开
  5. Android的IPC机制之Binder详细介绍
  6. Android(安卓)getWidth和getMeasuredWidt
  7. Android--通知之Notification
  8. Android进阶篇-百度地图获取地理信息
  9. layout_weight及常见属性解析
  10. Android之MediaPlayer 音频播放