SMS Encryption This kind of function because the demand for novice learning is small, so there are few simple demo on the Internet for beginners to reference. Small make up to do here is also spent more time self concept, the specific process is but more description, talk about the content of the demo.
Demo function:
1. Can send SMS and encrypt (by changing char in string)
2. Ability to view SMS in mobile phone
3, able to receive the encrypted text message decryption.
The knowledge points involved:
1, Intent bundle transmission
2, Contentresolver access to mobile phone messages
3, Listveiw and Simpleadapter
4. Send SMS and set up broadcast for sending SMS
The problems encountered:
1, send short message character too long will cause send failure
Workaround: Set to send each message within 70 words.
Principle: Each message is limited to 160 characters, each Chinese character is 2 characters. Usually we send SMS almost unlimited length, because once the length of a single message is exceeded, the mobile phone will be automatically divided into multiple send, and then the receiver is divided into several received after the integration of the display.
Code:
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 Allison 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);
Set the broadcast sendfilter = new Intentfilter () to be monitored for sending SMS;
Sendfilter.addaction ("Sent_sms_action");
Sendstatusreceiver = new Sendstatusreceiver ();
Registerreceiver (Sendstatusreceiver, Sendfilter); Send.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {Toast.maketext (Senda
Ctivity.this, "In encrypted send, please later ...", Toast.length_short). Show ();
Receives the contents of the EditText and encrypts//if Char+8 is out of range, the original word Fu Fa past 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);
Send SMS//and use the fourth parameter of Sendtextmessage to monitor the sending status of SMS 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 I
ntent) {if (getresultcode () = = RESULT_OK) {//Send successfully toast.maketext (context, "Send succeeded", Toast.length_long)
. Show ();
Intent intent1 = new Intent (sendactivity.this, Receiveactivity.class);
StartActivity (INTENT1);
Finish ();
else {//Send failed Toast.maketext (context, "Send failed", Toast.length_long). Show ();
}} @Override protected void OnDestroy () {Super.ondestroy ();
Stop listening for unregisterreceiver (sendstatusreceiver) When the activity is destroyed;
}
}
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 ();
} @Overrideprotected 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);
The private void Refreshlist () {//reads information from the SMS database URI = Uri.parse ("content://sms/");
string[] projection = new string[]{"Address", "body", "date"};
Cursor Cursor = Getcontentresolver (). Query (URI, projection, null, NULL, "date desc");
Startmanagingcursor (cursor); Here to simplify the efficiency of the code, just show 20 recent SMS for (int i = 0; i < i++) {//Get information from SMS database of mobile phone if (Cursor.movetonext ()) {String ad
Dress = cursor.getstring (Cursor.getcolumnindex ("Address"));
String BODY = cursor.getstring (Cursor.getcolumnindex ("body")); Long longdate = Cursor.getlong (cursor.getcolumnIndex ("date"));
Converts the acquired time to the way we want 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+");
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) {//Get ListView This I The contents of the//content in the TEM are formatted as follows//{body=[b@43c2da70body, address=+8615671562394address, time=2015-12-24 11:55:50time} Strin
G 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);
Use the bundle store data to send to the next activity Intent intent=new Intent (receiveactivity.this,receiveactivity_show.class);
Bundle Bundle = new Bundle ();
Bundle.putstring ("Body", the 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);
Receive content and 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);
After the text message is decrypted and displayed in the TextView//If the char+8 is out of range, the String real_content = "" is parsed directly according to the original character.
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" &
Gt <textview android:layout_width= "match_parent" android:layout_height= "wrap_content" android:background= "#000" an droid:padding= "12DP" android:text= "Encrypted SMS" android:textcolor= "#fff" android:textsize= "25sp" android:textstyle= "bold" /> <button android:layout_margintop= "120DP" android:id= "@+id/bt_send" android:layout_width= "200DP" Androi d:layout_height= "80DP" android:text= "Send encrypted SMS" android:layout_gravity= "center" android:textsize= "20DP"/> <butt On android:id= "@+id/bt_receive" android:layout_width= "200DP" android:layout_height= "80DP" android:layout_gravity= " Center "android:text=" decrypt local SMS "android:textsize=" "20DP" android:layout_below= "@+id/bt_send"/> </linearlayout&
Gt
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" &
Gt <linearlayout android:layout_width= "match_parent" android:layout_height= "Match_parent" Vertical "android:padding=" 10DP "> <textview android:layout_width=" match_parent "android:layout_height=" W Rap_content "android:text=" Encrypted SMS Send "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:l Ayout_marginbottom= "5DP" android:text= "Sent to:" Android:textsize= "25sp"/> <edittext android:id= "@+id/phon E_edit_text "Android:layoUt_width= "Match_parent" android:layout_height= "wrap_content" android:layout_marginbottom= "10DP" Android:backgroun d= "@drawable/edit_text_style" android:hint= "receiver mobile number" android:maxlength= "android:maxlines=" "1" android:texts Ize= "19SP" android:singleline= "true"/> <textview android:layout_width= "Match_parent" android:layout_he ight= "Wrap_content" android:layout_marginbottom= "5DP" android:text= "Send Content" android:textsize= "25sp"/> <E Dittext android:id= "@+id/content_edit_text" android:layout_width= "match_parent" android:layout_height= "0DP" an droid:layout_weight= "1" android:background= "@drawable/edit_text_style" android:gravity= "Start" android:hint= "single Message Please remain within 70 words "android:maxlength=" android:textsize= "19sp"/> <linearlayout android:layout_width= "Mat" Ch_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= "Cancel Edit" Android : textsize= "20sp"/> <button android:id= "@+id/send_edit" android:layout_width= "0DP" Android:layout_heig ht= "Wrap_content" android:layout_weight= "1" android:text= "send" android:textsize= "20sp"/> </linearlayout&
Gt
</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=" all SMS
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" &
Gt <textview android:layout_width= "match_parent" android:layout_height= "wrap_content" android:background= "#000" an droid:padding= "12DP" android:text= "SMS Decryption" android:textcolor= "#fff" android:textsize= "25sp" android:textstyle= "bold" /> <tablelayout android:layout_width= "match_parent" android:layout_height= "Match_parent" Android:orientat ion= "vertical" android:padding= "10DP" android:stretchcolumns= "1" android:shrinkcolumns= "1" > <tablerow Andr oid: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=" Number: "Android:te Xtcolor= "#000" Android: textsize= "20sp"/> <textview android:id= "@+id/address_show" android:layout_width= "Wrap_content" Andro id:layout_height= "Wrap_content" android:layout_marginbottom= "10DP" android:background= "@drawable/edit_text_style "android:maxlines=" 1 "android:singleline=" true "android:textsize=" 18sp "/> </tablerow > <ta Blerow android:layout_width= "match_parent" android:layout_marginbottom= "10DP" > <textview android:l Ayout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_marginbottom= "5DP" android:text= " Rooms: "android:textcolor=" #000 "android:textsize=" "20sp"/> <textview android:id= "@+id/time_show" Andro Id:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_marginbottom= "10DP" Android:ba ckground= "@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" and
roid:layout_height= "Wrap_content" android:layout_marginbottom= "5DP" android:text= "original text:" Android:textcolor= "#000" Android:textsize= "20sp"/> <textview android:id= "@+id/early_body_show" android:layout_width= "Wrap_cont"
Ent "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= "Mat" Ch_parent "> <textview android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android : layout_marginbottom= "5DP" android:text= "after parsing:" android:textcolor= "#000" android:textsize= "20sp"/> <te
Xtview 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:textSiz E= "18sp"/> </TableRow> </TableLayout> </LinearLayout>
The above is the entire content of this article, Android to achieve SMS encryption, the realization of sending encrypted text messages, decryption local text messages, I hope to help you learn.