Android模拟器互发短信
2010-09-06 08:15:00 来源:WEB开发网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:
Java代码
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) {
赞助商链接