Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 初級開發 >> Android模擬器互發短信

Android模擬器互發短信

編輯:初級開發

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

關鍵代碼:

發送短信代碼:

Java代碼

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;

}

}

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:

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) {

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);

}

}

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:

Java代碼

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(" ");

// messageText.append(" ");

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(" ");

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");

}

}

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(" ");

// messageText.append(" ");

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(" ");

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");

}

}

最後別忘記加入權限:

XML代碼

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

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

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

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

測試的時候開兩個模擬器,號碼填入對方模擬器號即可

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved