论坛首页 移动开发技术论坛

模拟器互发短信

浏览 7178 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-10-10   最后修改:2009-10-10

以下内容Sinfrancis版权所有,专注请注明来自  http://mdev.cc/dev

 

目的:使用模拟器互发短信息,比如 5554 和 5556互发短息


关键代码:

发送短信代码:
package cc.androidos.sms;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/**
 * 
 * @param demo
 *            :its own object.
 * @param telNum
 *            : telephone number to send to .
 * @param messageText
 *            : the message need to send .
 * @param sendBtn
 *            :Button.
 * 
 */
public class SMSDemo extends Activity {
private static SMSDemo demo;
private EditText telNum;
private EditText messageText;
private Button sendBtn;

/**
* override the method onCreate() 1.bind the data telNum and messageText.
* 2.set the sendBtn's listener.
*/
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send);

demo = this;
telNum = (EditText) findViewById(R.id.telNumText_send);
messageText = (EditText) findViewById(R.id.message_copntent_send);
sendBtn = (Button) findViewById(R.id.send_button_send);

/**
* 1.get the data from the input line.. 2.call the SmsManager and send
* the message.
*/
sendBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String telNumStr = telNum.getText().toString();
String messageStr = messageText.getText().toString();
if (null != telNumStr && null != messageStr) {
SmsManager smsMgr = SmsManager.getDefault();
Intent i = new Intent("cc.androidos.smsdemo.IGNORE_ME");
PendingIntent dummyEvent = PendingIntent.getBroadcast(
SMSDemo.this, 0, i, 0);
try {
smsMgr.sendTextMessage(telNumStr, null, messageStr,
dummyEvent, dummyEvent);
} catch (Exception e) {
Log.e("SmsSending", "SendException", e);
}
} else {
showDialog(getTaskId());
}
}
});
}

/**
* get its object.
* 
* @return SMSDemo
*/
public static SMSDemo getApp() {
return demo;
}
}

 接受短信的Receiver:

package cc.androidos.sms;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.util.Log;

public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
SMSDemo demo = SMSDemo.getApp();
Bundle bundle = intent.getExtras();
StringBuffer sb = new StringBuffer(5000);
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++) {
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
String address = smsMessage[n].getOriginatingAddress();
sb.append(address + ":").append(smsMessage[n].getMessageBody());
}
Log.e("message", sb.toString());
/**
* call an intent to start the activity DisplayMessage with variable
* message data.
*/
Intent i = new Intent(context, DisplayMessage.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("message", sb.toString());
context.startActivity(i);

}
}
 显示短信的Activity:

package cc.androidos.sms;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;

public class DisplayMessage extends Activity {
private EditText messageText;
private EditText messageReplyText;
private Intent intent;
private String telNum;
private String message;
private Button replyBtn;
private ProgressBar bar;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.receiver);
messageText = (EditText) findViewById(R.id.message_get);
messageReplyText = (EditText) findViewById(R.id.message_reply);
replyBtn = (Button) findViewById(R.id.reply_button);
bar = (ProgressBar) findViewById(R.id.bar);
bar.setVisibility(ProgressBar.INVISIBLE);
intent = getIntent();
String data = intent.getStringExtra("message");
String[] str = data.split(":");
if (str.length == 2) {
telNum = str[0];
message = str[1];
}
messageText.append(data);
messageText.append("\n");
// messageText.append("\n");
replyBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
bar.setVisibility(ProgressBar.VISIBLE);
String replyMessage = messageReplyText.getText().toString();
SmsManager smsMgr = SmsManager.getDefault();
Intent i = new Intent("cc.androidos.smsdemo.IGNORE_ME");
PendingIntent dummyEvent = PendingIntent.getBroadcast(
DisplayMessage.this, 0, i, 0);
try {
smsMgr.sendTextMessage(telNum, null, replyMessage,
dummyEvent, dummyEvent);
messageText.append(replyMessage);
messageText.append("\n");
messageReplyText.setText("");

} catch (Exception e) {
Log.e("SmsSending", "SendException", e);
}
bar.setVisibility(ProgressBar.INVISIBLE);
}
});
}

/**
* get the name by his tel number.
* 
* @param telNum
* @return
*/
private String getNameByIdTelnum(String telNum) {
Cursor mCursor = this.getContentResolver().query(Uri.parse("telNum"),
null, null, null, null);
String result = mCursor.getColumnName(1);
Log.e("test name", result);
return "";
// mCursor.getColumnIndex("Sun");
}
}

 
最后别忘记加入权限:


<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
 
测试的时候开两个模拟器,号码填入对方模拟器号即可
   发表时间:2009-12-30  
请问 有没有彩信的?
0 请登录后投票
   发表时间:2010-01-06  
请问怎么样开两个模拟器啊~~
0 请登录后投票
   发表时间:2010-09-06  
请问发送中文短信的话,接收模拟器会出现乱码,该怎么解决?
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics