转载声明:文章来源:https://blog.csdn.net/sinat_23092639/article/details/47679673
最近做了一个简单的短信操作模块,比较实用小巧。主要功能是可以发送短信(包括短信发送状态的提示),储存短信(可以用于短信列表显示等等),短信的储存可以用SQLite,不过觉得也就几十条上百条短信,用SQLite未免大材小用,还麻烦,于是决定用SharePreference结合对象序列化来做这个模块。
首先是短信实体类,为了复用,做成抽象类,再具体项目中使用只要继承该抽象类即可:
public abstract class Message implements Serializable{
private String number;
//private int playTime;
private int id;
private String userName;
private String messageText;
private String time;
public Message(String number, int playTime, int id, String userName,
String messageText, String time) {
super();
this.number = number;
// this.playTime = playTime;
this.id = id;
this.userName = userName;
this.messageText = messageText;
this.time = time;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getMessageText() {
return messageText;
}
public void setMessageText(String messageText) {
this.messageText = messageText;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "号码:"+this.number+",用户:"+this.userName+",编号:"
+this.id+",短信内容:"+this.messageText;
}
}
拥有几个短信必须的数据作为成员变量,然后重新toString方法。
核心操作类,MessageUtils,拥有发送短信方法,包括发送完后对短信的保存,短信发送成功与否、接收成功与否状态的反馈功能,对单条短信和全部短信获取都有提供对外接口,使用的时候非常方便:
public class MessageUtils {
//private Message message;
private Context context;
private String SENT_SMS_ACTION = "SENT_SMS_ACTION";
private String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
//携带发送是否成功状态的Intent
private Intent sentIntent;
private PendingIntent sentPI;
//携带接收是否成功状态的Intent
private Intent deliverIntent;
private PendingIntent deliverPI;
public MessageUtils(Context context) {
super();
this.context = context;
sentIntent = new Intent(SENT_SMS_ACTION);
deliverIntent = new Intent(DELIVERED_SMS_ACTION);
}
/**
* 序列化对象
* @param message
* @return
* @throws IOException
*/
private String serialize(Message message) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(
byteArrayOutputStream);
objectOutputStream.writeObject(message);
String serStr = byteArrayOutputStream.toString("ISO-8859-1");
serStr = java.net.URLEncoder.encode(serStr, "UTF-8");
objectOutputStream.close();
byteArrayOutputStream.close();
return serStr;
}
/**
* 反序列化对象
*
* @param str 将要转化为对象的字符串
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
private Message deSerialization(String str) throws IOException,
ClassNotFoundException {
if(str == null || str == ""){
return null;
}
String redStr = java.net.URLDecoder.decode(str, "UTF-8");
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
redStr.getBytes("ISO-8859-1"));
ObjectInputStream objectInputStream = new ObjectInputStream(
byteArrayInputStream);
Message message = (Message) objectInputStream.readObject();
objectInputStream.close();
byteArrayInputStream.close();
return message;
}
/**
* 保存短信
* @param strObject 序列化后转化为String的短信对象
* @param num 短信存储的序号
*/
private void saveMessage(Message message,int num) {
String strObject;
try {
String i = String.valueOf(num);
strObject = serialize(message);
SharedPreferences sp = context.getSharedPreferences("Message", 0);
Editor edit = sp.edit();
edit.putString(i, strObject);
edit.commit();
//重置短信总条数
setSmsCount(num);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 删除某条短信
* @param num 被删除短信的存储序号
*/
public void deleteMessage(int num) {
int sum = getSmsCount();
if(sum == num){
setSmsCount(--sum);
return;
}
for(int i = num;i < sum;i++ ){
Message message = getMessage(i+1);
saveMessage(message, i);
}
// int count = getSmsCount();
// setSmsCount(--count);
}
/**
* 获得某条短信
* @param num 短信存储的序号
* @return
*/
public Message getMessage(int num) {
SharedPreferences sp = context.getSharedPreferences("Message", 0);
Message message;
String i = String.valueOf(num);
try {
message = deSerialization(sp.getString(i, null));
return message;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
/**
* 获得全部存储的短信
* @return
*/
public List<Message> getAllMessage() {
List<Message> messages = new ArrayList<Message>();
for(int i = 1;i <= getSmsCount();i++){
Message message = getMessage(i);
messages.add(message);
}
return messages;
}
/**
* 发送短信,注册发送状态广播和接收状态广播,每发送一条短信就将其储存起来
* @param messageText 短信内容
* @param num 对方号码
*/
public void sendMessage(Message message){
SmsManager smsManager = SmsManager.getDefault();
sentPI = PendingIntent.getBroadcast(context, 0, sentIntent, 0);
deliverPI = PendingIntent.getBroadcast(context, 0, deliverIntent, 0);
regSendBroadcast();
regRecBroadcast();
List<String> divideContents = smsManager.divideMessage(message.getMessageText());
for (String text : divideContents) {
smsManager.sendTextMessage(message.getNumber(), null, text, sentPI, deliverPI);
}
int count = getSmsCount();
//每发送一条短信就将其储存
saveMessage(message, ++count);
}
/**
* 注册发送状态广播,等待运营商返回结果码,根据结果码作出相应的反应
*/
private void regSendBroadcast(){
context.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context _context, Intent _intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(context,
"短信发送成功", Toast.LENGTH_SHORT)
.show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(context,
"短信发送失败", Toast.LENGTH_SHORT)
.show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(context,
"短信发送失败", Toast.LENGTH_SHORT)
.show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(context,
"短信发送失败", Toast.LENGTH_SHORT)
.show();
break;
}
}
}, new IntentFilter(SENT_SMS_ACTION));
}
/**
* 注册短信接收状态广播,一旦接收方成功收到短信则收到广播
*/
private void regRecBroadcast(){
context.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context _context, Intent _intent) {
Toast.makeText(context,
"收信人已经成功接收", Toast.LENGTH_SHORT)
.show();
}
}, new IntentFilter(DELIVERED_SMS_ACTION));
}
/**
* 设置短信条数记录
* @param count
*/
private void setSmsCount(int count) {
SharedPreferences sp = context.getSharedPreferences("Message", 0);
Editor edit = sp.edit();
edit.putInt("smsCount", count);
edit.commit();
}
/**
* 获得存储短信总条数
* @return
*/
public int getSmsCount() {
SharedPreferences sp = context.getSharedPreferences("Message", 0);
int count = sp.getInt("smsCount", 0);
return count;
}
}
使用对象的序列化和反序列化,将一个Message对象转化为String对象,将其存储在SharePreference中,键值为短信的序号。通过该序号就可以查询或删除指定短信。
这样单项目需要短信操作并需要存储短信时,可以使用这个小模块,只需要一个MessageUtils对象,调用它的public方法既能方便操作短信,符合面向对象高聚合低耦合,可复用的设计原则。
帖子还没人回复快来抢沙发