当前位置: 移动技术网 > IT编程>移动开发>Android > Android实现短信加密功能(发送加密短信、解密本地短信)

Android实现短信加密功能(发送加密短信、解密本地短信)

2019年07月24日  | 移动技术网IT编程  | 我要评论

龙漫电玩网,汶上信息港宏顺驾校,激战江南片尾曲

短信加密此类功能由于新手学习的需求量较小,所以在网上很少有一些简单的demo供新手参考。小编做到此处也是花了比较多的时间自我构思,具体的过程也是不过多描述了,讲一下demo的内容。

 

 

 


demo功能:

1、可以发送短信并且加密(通过改变string中的char)

2、能够查看手机中的短信

3、能够给收到的加密短信解密。

涉及到的知识点:

1、intent bundle传递

2、contentresolver获取手机短信

3、listveiw与simpleadapter

4、发送短信以及为发送短信设置要监听的广播

遇到的问题:

1、发送短信字符过长会导致发送失败

解决方法:设置发送每条短信为70个字以内。

原理:每条短信限制160字符以内,每个汉字是2个字符。平时我们发送短信几乎不限长度,是因为一旦超过了单条短信的长度,手机会自动分多条发送,然后接收方分多条接收后整合在一起显示。

代码:


mainactivity:

import android.app.activity; 
import android.content.intent; 
import android.support.v7.app.appcompatactivity; 
import android.os.bundle; 
import android.view.view; 
import android.widget.button; 
 
public class mainactivity extends activity { 
 
 @override 
 protected void oncreate(bundle savedinstancestate) { 
 super.oncreate(savedinstancestate); 
 setcontentview(r.layout.activity_main); 
 
 initview(); 
 } 
 
 private void initview() { 
 button send=(button)findviewbyid(r.id.bt_send); 
 button receive=(button)findviewbyid(r.id.bt_receive); 
 
 send.setonclicklistener(new view.onclicklistener() { 
  @override 
  public void onclick(view view) { 
  intent intent=new intent(mainactivity.this,sendactivity.class); 
  startactivity(intent); 
  } 
 }); 
 
 receive.setonclicklistener(new view.onclicklistener() { 
  @override 
  public void onclick(view view) { 
  intent intent=new intent(mainactivity.this,receiveactivity.class); 
  startactivity(intent); 
  } 
 }); 
 } 
} 

sendactivity:

import android.app.activity; 
import android.app.pendingintent; 
import android.content.broadcastreceiver; 
import android.content.context; 
import android.content.intent; 
import android.content.intentfilter; 
import android.os.bundle; 
import android.telephony.smsmanager; 
import android.util.log; 
import android.view.view; 
import android.widget.button; 
import android.widget.edittext; 
import android.widget.toast; 
 
/** 
 * created by 佳佳 on 2015/12/21. 
 */ 
public class sendactivity extends activity { 
 
 private intentfilter sendfilter; 
 private sendstatusreceiver sendstatusreceiver; 
 
 @override 
 protected void oncreate(bundle savedinstancestate) { 
 super.oncreate(savedinstancestate); 
 setcontentview(r.layout.activity_send); 
 
 initview(); 
 } 
 
 private void initview() { 
 button cancel = (button) findviewbyid(r.id.cancel_edit); 
 button send = (button) findviewbyid(r.id.send_edit); 
 final edittext phone = (edittext) findviewbyid(r.id.phone_edit_text); 
 final edittext msginput = (edittext) findviewbyid(r.id.content_edit_text); 
 
 //为发送短信设置要监听的广播 
 sendfilter = new intentfilter(); 
 sendfilter.addaction("sent_sms_action"); 
 sendstatusreceiver = new sendstatusreceiver(); 
 registerreceiver(sendstatusreceiver, sendfilter); 
 
 send.setonclicklistener(new view.onclicklistener() { 
  @override 
  public void onclick(view v) { 
  toast.maketext(sendactivity.this, "加密发送中,请稍后...", toast.length_short).show(); 
  //接收edittext中的内容,并且进行加密 
  //倘若char+8超出了表示范围,则把原字符发过去 
  string address = phone.gettext().tostring(); 
  string content = msginput.gettext().tostring(); 
  string contents = ""; 
  for (int i = 0; i < content.length(); i++) { 
   try { 
   contents += (char) (content.charat(i) + 8); 
   }catch (exception e) { 
   contents += (char) (content.charat(i)); 
   } 
  } 
 
  //log.i("hahaha",contents); 
 
  //发送短信 
  //并使用sendtextmessage的第四个参数对短信的发送状态进行监控 
  smsmanager smsmanager = smsmanager.getdefault(); 
  intent sentintent = new intent("sent_sms_action"); 
  pendingintent pi = pendingintent.getbroadcast( 
   sendactivity.this, 0, sentintent, 0); 
  smsmanager.sendtextmessage(address, null, 
   contents.tostring(), pi, null); 
  } 
 }); 
 
 cancel.setonclicklistener(new view.onclicklistener() { 
  @override 
  public void onclick(view view) { 
  finish(); 
  } 
 }); 
 } 
 
 class sendstatusreceiver extends broadcastreceiver { 
 
 @override 
 public void onreceive(context context, intent intent) { 
  if (getresultcode() == result_ok) { 
  //发送成功 
  toast.maketext(context, "send succeeded", toast.length_long) 
   .show(); 
 
  intent intent1 = new intent(sendactivity.this, receiveactivity.class); 
  startactivity(intent1); 
  finish(); 
  } else { 
  //发送失败 
  toast.maketext(context, "send failed", toast.length_long) 
   .show(); 
  } 
 } 
 
 } 
 
 @override 
 protected void ondestroy() { 
 super.ondestroy(); 
 //在activity摧毁的时候停止监听 
 unregisterreceiver(sendstatusreceiver); 
 } 
} 

receiveactivity:

import android.app.activity; 
import android.content.intent; 
import android.database.cursor; 
import android.net.uri; 
import android.os.bundle; 
import android.util.log; 
import android.view.view; 
import android.widget.adapterview; 
import android.widget.listview; 
import android.widget.simpleadapter; 
import android.widget.textview; 
 
import java.text.simpledateformat; 
import java.util.arraylist; 
import java.util.date; 
import java.util.hashmap; 
import java.util.list; 
import java.util.map; 


public class receiveactivity extends activity implements adapterview.onitemclicklistener{ 
 private textview tv_address; 
 private textview tv_body; 
 private textview tv_time; 
 private listview listview; 
 private list<map<string, object>> datalist; 
 private simpleadapter simple_adapter; 
 
 @override 
 protected void oncreate(bundle savedinstancestate) { 
 super.oncreate(savedinstancestate); 
 setcontentview(r.layout.activity_receive); 
 
 initview(); 
 } 
 
 @override 
 protected void onstart() { 
 super.onstart(); 
 refreshlist(); 
 } 
 
 
 private void initview() { 
 tv_address = (textview) findviewbyid(r.id.tv_address); 
 tv_body = (textview) findviewbyid(r.id.tv_body); 
 tv_time = (textview) findviewbyid(r.id.tv_time); 
 listview = (listview) findviewbyid(r.id.list_receive); 
 datalist = new arraylist<map<string, object>>(); 
 
 listview.setonitemclicklistener(this); 
 } 
 
 private void refreshlist() { 
 //从短信数据库读取信息 
 uri uri = uri.parse("content://sms/"); 
 string[] projection = new string[]{"address", "body", "date"}; 
 cursor cursor = getcontentresolver().query(uri, projection, null, null, "date desc"); 
 startmanagingcursor(cursor); 
 
 //此处为了简化代码提高效率,仅仅显示20条最近短信 
 for (int i = 0; i < 20; i++) { 
  //从手机短信数据库获取信息 
  if(cursor.movetonext()) { 
  string address = cursor.getstring(cursor.getcolumnindex("address")); 
  string body = cursor.getstring(cursor.getcolumnindex("body")); 
  long longdate = cursor.getlong(cursor.getcolumnindex("date")); 
  //将获取到的时间转换为我们想要的方式 
  simpledateformat dateformat = new simpledateformat("yyyy-mm-dd hh:mm:ss"); 
  date d = new date(longdate); 
  string time = dateformat.format(d); 
 
 
  map<string, object> map = new hashmap<string, object>(); 
  map.put("address", address); 
  map.put("body", body+"body"); 
  map.put("time", time+" time"); 
  datalist.add(map); 
  } 
 } 
 
 simple_adapter = new simpleadapter(this, datalist, r.layout.activity_receive_list_item, 
  new string[]{"address", "body", "time"}, new int[]{ 
  r.id.tv_address, r.id.tv_body, r.id.tv_time}); 
 listview.setadapter(simple_adapter); 
 } 
 
 @override 
 public void onitemclick(adapterview<?> adapterview, view view, int i, long l) { 
 //获取listview中此个item中的内容 
 //content的内容格式如下 
 //{body=[b@43c2da70body, address=+8615671562394address, time=2015-12-24 11:55:50time} 
 string content = listview.getitematposition(i) + ""; 
 string body = content.substring(content.indexof("body=") + 5, 
  content.indexof("body,")); 
 //log.i("hahaha",body); 
 string address = content.substring(content.indexof("address=") + 8, 
  content.lastindexof(",")); 
 //log.i("hahaha",address); 
 string time = content.substring(content.indexof("time=") + 5, 
  content.indexof(" time}")); 
 //log.i("hahaha",time); 
 
 //使用bundle存储数据发送给下一个activity 
 intent intent=new intent(receiveactivity.this,receiveactivity_show.class); 
 bundle bundle = new bundle(); 
 bundle.putstring("body", body); 
 bundle.putstring("address", address); 
 bundle.putstring("time", time); 
 intent.putextras(bundle); 
 startactivity(intent); 
 
 } 
} 

receiveactivity_show:

import android.app.activity; 
import android.os.bundle; 
import android.widget.textview; 
 
public class receiveactivity_show extends activity { 
 private textview address_show; 
 private textview time_show; 
 private textview early_body_show; 
 private textview late_body_show; 
 
 @override 
 protected void oncreate(bundle savedinstancestate) { 
 super.oncreate(savedinstancestate); 
 setcontentview(r.layout.activity_receive_show); 
 
 initview(); 
 } 
 
 private void initview() { 
 
 address_show = (textview) findviewbyid(r.id.address_show); 
 time_show = (textview) findviewbyid(r.id.time_show); 
 early_body_show = (textview) findviewbyid(r.id.early_body_show); 
 late_body_show = (textview) findviewbyid(r.id.late_body_show); 
 
 //接收内容和id 
 bundle bundle = this.getintent().getextras(); 
 string body = bundle.getstring("body"); 
 string time = bundle.getstring("time"); 
 string address = bundle.getstring("address"); 
 
 
 address_show.settext(address); 
 early_body_show.settext(body); 
 time_show.settext(time); 
 
 //对短信消息进行解密后显示在textview中 
 //倘若char+8超出了表示范围,则直接按照原字符解析 
 string real_content = ""; 
 for (int i = 0; i < body.length(); i++) { 
  try { 
  char textchar=(char) (body.charat(i) + 8); 
  real_content += (char) (body.charat(i) - 8); 
  }catch (exception e){ 
  real_content += (char) (body.charat(i)); 
  } 
 } 
 late_body_show.settext(real_content); 
 } 
 
} 

activity_main:

<?xml version="1.0" encoding="utf-8"?> 
 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical"> 
 
 <textview 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:background="#000" 
 android:padding="12dp" 
 android:text="加密短信" 
 android:textcolor="#fff" 
 android:textsize="25sp" 
 android:textstyle="bold" /> 
 <button 
 android:layout_margintop="120dp" 
 android:id="@+id/bt_send" 
 android:layout_width="200dp" 
 android:layout_height="80dp" 
 android:text="发送加密短信" 
 android:layout_gravity="center" 
 android:textsize="20dp"/> 
 <button 
 android:id="@+id/bt_receive" 
 android:layout_width="200dp" 
 android:layout_height="80dp" 
 android:layout_gravity="center" 
 android:text="解密本地短信" 
 android:textsize="20dp" 
 android:layout_below="@+id/bt_send"/> 
 
</linearlayout> 

activity_send:

<?xml version="1.0" encoding="utf-8"?> 
 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical"> 
 
 
 <linearlayout 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 android:padding="10dp"> 
 
 <textview 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:text="加密短信发送" 
  android:textcolor="#000" 
  android:textsize="35sp" /> 
 
 <view 
  android:layout_width="match_parent" 
  android:layout_height="3dp" 
  android:layout_marginbottom="20dp" 
  android:background="#cecece" /> 
 
 <textview 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:layout_marginbottom="5dp" 
  android:text="发送至:" 
  android:textsize="25sp" /> 
 
 <edittext 
  android:id="@+id/phone_edit_text" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:layout_marginbottom="10dp" 
  android:background="@drawable/edit_text_style" 
  android:hint="接收人手机号码" 
  android:maxlength="15" 
  android:maxlines="1" 
  android:textsize="19sp" 
  android:singleline="true" /> 
 
 <textview 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:layout_marginbottom="5dp" 
  android:text="发送内容" 
  android:textsize="25sp" /> 
 
 <edittext 
  android:id="@+id/content_edit_text" 
  android:layout_width="match_parent" 
  android:layout_height="0dp" 
  android:layout_weight="1" 
  android:background="@drawable/edit_text_style" 
  android:gravity="start" 
  android:hint="单条短信请保持在70字以内" 
  android:maxlength="70" 
  android:textsize="19sp" 
  /> 
 
 <linearlayout 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:layout_margin="10dp"> 
 
  <button 
  android:id="@+id/cancel_edit" 
  android:layout_width="0dp" 
  android:layout_height="wrap_content" 
  android:layout_weight="1" 
  android:text="取消编辑" 
  android:textsize="20sp" /> 
 
  <button 
  android:id="@+id/send_edit" 
  android:layout_width="0dp" 
  android:layout_height="wrap_content" 
  android:layout_weight="1" 
  android:text="发 送" 
  android:textsize="20sp" /> 
 </linearlayout> 
 </linearlayout> 
</linearlayout> 

activity_receive:

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical" android:layout_width="match_parent" 
 android:layout_height="match_parent"> 
 
 <textview 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:padding="10dp" 
 android:text="所有短信" 
 android:textcolor="#fff" 
 android:background="#000" 
 android:textsize="23sp" 
 android:textstyle="bold"/> 
 
 <listview 
 android:id="@+id/list_receive" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent"></listview> 
 
</linearlayout> 

activity_receive_show:

<?xml version="1.0" encoding="utf-8"?> 
 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical"> 
 
 <textview 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:background="#000" 
 android:padding="12dp" 
 android:text="短信解密" 
 android:textcolor="#fff" 
 android:textsize="25sp" 
 android:textstyle="bold" /> 
 
 <tablelayout 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 android:padding="10dp" 
 android:stretchcolumns="1" 
 android:shrinkcolumns="1"> 
 
 <tablerow android:layout_margintop="10dp" 
  android:layout_width="match_parent" 
  > 
 
  <textview 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_marginbottom="5dp" 
  android:text="号码:" 
  android:textcolor="#000" 
  android:textsize="20sp" /> 
 
  <textview 
  android:id="@+id/address_show" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_marginbottom="10dp" 
  android:background="@drawable/edit_text_style" 
  android:maxlines="1" 
  android:singleline="true" 
  android:textsize="18sp" /> 
 </tablerow 
  > 
 
 <tablerow 
  android:layout_width="match_parent" 
  android:layout_marginbottom="10dp" 
  > 
 
  <textview 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_marginbottom="5dp" 
  android:text="时间:" 
  android:textcolor="#000" 
  android:textsize="20sp" /> 
 
  <textview 
  android:id="@+id/time_show" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_marginbottom="10dp" 
  android:background="@drawable/edit_text_style" 
  android:maxlines="1" 
  android:textsize="18sp" /> 
 </tablerow> 
 
 <tablerow android:layout_marginbottom="10dp" 
  android:layout_width="match_parent"> 
 
  <textview 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_marginbottom="5dp" 
  android:text="原文本:" 
  android:textcolor="#000" 
  android:textsize="20sp" /> 
 
  <textview 
  android:id="@+id/early_body_show" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:background="@drawable/edit_text_style" 
  android:minlines="6" 
  android:maxlines="6" 
  android:textsize="18sp" 
  /> 
 </tablerow> 
 
 <tablerow 
  android:layout_width="match_parent"> 
 
  <textview 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_marginbottom="5dp" 
  android:text="解析后:" 
  android:textcolor="#000" 
  android:textsize="20sp" /> 
 
  <textview 
  android:id="@+id/late_body_show" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:background="@drawable/edit_text_style" 
  android:minlines="6" 
  android:maxlines="6" 
  android:singleline="false" 
  android:textsize="18sp" 
  /> 
 </tablerow> 
 
 
 </tablelayout> 
</linearlayout> 

以上就是本文的全部内容,android实现短信加密,实现发送加密短信、解密本地短信,希望对大家的学习有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网