嗯 今天写了两个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做的丑了一点。。。
分享到:
相关推荐
Programe SMS on Android.
android短信监听器,可转发收到的短信。支持短信设置接收机号码。仅供技术交流。-android SMS spy,transfer SMS to a receiver。The receiver can be set by commmand SMS
android 通讯录 SMS 支付SIM卡和手机中的SIM联系人的操作
android手机提取短信,sqlite3数据库开发源码
android 发短信源码 自己写的源码 可以拿去看看 有注释
Android SMS短消息服务系统,自己开发的Android短信收发系统,可以用于办公等专项业务。
Android SMS 发送 接收及格式
基于android手机系统的 短信 sms 发送接收
本程序的主要功能是短信语音播报,主要运行于android平台,能自动播报预设联系人发来的短信,能给我们的生活带来很大的方便。
android sms 工具 可以测试使用
android4.0 sms源码, 很多新特性, 可以参考啊。 老乔年轻时说, 有名的艺术家是抄袭, 伟大的艺术家是偷窃。 what are you waiting for ? let's do it !
Nextcloud SMS (Android) Nextcloud SMS app pushes your Android devices conversation into your Nextcloud instance, using ocsms app. :arrow_forward: Access ocsms app sources are available here: ...
Android 短信拦截转发,基于安卓手机的短信广播处理,有短信拦截,黑名单设置,短信拦截后转发
Android之SMS短信和Contacts联系人实例
android 收取短信时间,内容等。。。。。。
react-native-sms, 支持iOS和Android的短信发送方 react-native-sms用户定义的SendSMS使用这个 rn 组件发送带有回调 (completed/cancelled/error). iOS和Android的短信。目前,只支持用户发送短信。 this react-na
Android 发送短信并且保存短信记录。
对于学习android短信信息知识的问题,有一些小型的解决,可以参考
Android 纯短信收发程序,想在android中集成短信功能的同学绝对有帮助。
android demo,SMS短信通知。android demo,SMS短信通知。