嗯 今天写了两个activity来玩玩sms。。
首先是做一个输入框,输入号码和短信内容。
<?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>
对应的activity:
package com.sun.smsdemo;
/**
* @author Sun.Zhong
* @version 1.0
*/
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.sendform);
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("com.sun.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;
}
}
SmsManager smsMgr = SmsManager.getDefault();
Intent i = new Intent("com.sun.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);
}
这一段就是发送操作了。。
嗯 然后再搞个接受的ui。
<?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"
>
<EditText
android:id="@+id/message_get"
android:layout_width="319px"
android:layout_height="86px"
android:text=""
android:textSize="18sp"
android:layout_x="0px"
android:layout_y="42px"
>
</EditText>
<EditText
android:id="@+id/message_reply"
android:layout_width="319px"
android:layout_height="76px"
android:textSize="18sp"
android:layout_x="0px"
android:layout_y="182px"
>
</EditText>
<TextView
android:id="@+id/widget29"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView1"
android:layout_x="-150px"
android:layout_y="-18px"
>
</TextView>
<Button
android:id="@+id/reply_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reply"
android:layout_x="120px"
android:layout_y="282px"
>
</Button>
<TextView
android:id="@+id/widget42"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reply Message:"
android:layout_x="10px"
android:layout_y="152px"
>
</TextView>
<TextView
android:id="@+id/widget43"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Message:"
android:layout_x="10px"
android:layout_y="12px"
>
</TextView>
<ProgressBar
android:id="@+id/bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="130px"
android:layout_y="132px"
>
</ProgressBar>
</AbsoluteLayout>
接收短信需用实现BroadcastReceiver ,并覆盖
public void onReceive(Context context, Intent intent) 方法:
package com.sun.smsdemo;
/**
* @author Sun.Zhong
* @version 1.0
*/
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);
}
}
这样当有message来的时候,这个SMSReceiver 就会call它的onReceive()方法
然后把message的内容通过intent传到显示数据的DisplayMessage activity中
package com.sun.smsdemo;
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.main);
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("com.sun.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" />
这是初步的功能,后面想添加一个类似聊天记录的功能。。。详细再说。
在JE写block不能直接上传图片真TMD不爽。。。。
是了 忘记说怎么用。。启动两个emulator,同时在这两条鸟一起运行这个应用,然后tel num一个填5556 一个填5554就可以互发短信了。。。。
ps:UI做的丑了一点。。。