Android實(shí)現(xiàn)短信加密功能(發(fā)送加密短信、解密本地短信)
短信加密此類(lèi)功能由于新手學(xué)習(xí)的需求量較小,所以在網(wǎng)上很少有一些簡(jiǎn)單的demo供新手參考。小編做到此處也是花了比較多的時(shí)間自我構(gòu)思,具體的過(guò)程也是不過(guò)多描述了,講一下demo的內(nèi)容。



demo功能:
1、可以發(fā)送短信并且加密(通過(guò)改變string中的char)
2、能夠查看手機(jī)中的短信
3、能夠給收到的加密短信解密。
涉及到的知識(shí)點(diǎn):
1、intent bundle傳遞
2、ContentResolver獲取手機(jī)短信
3、listveiw與simpleAdapter
4、發(fā)送短信以及為發(fā)送短信設(shè)置要監(jiān)聽(tīng)的廣播
遇到的問(wèn)題:
1、發(fā)送短信字符過(guò)長(zhǎng)會(huì)導(dǎo)致發(fā)送失敗
解決方法:設(shè)置發(fā)送每條短信為70個(gè)字以?xún)?nèi)。
原理:每條短信限制160字符以?xún)?nèi),每個(gè)漢字是2個(gè)字符。平時(shí)我們發(fā)送短信幾乎不限長(zhǎng)度,是因?yàn)橐坏┏^(guò)了單條短信的長(zhǎng)度,手機(jī)會(huì)自動(dòng)分多條發(fā)送,然后接收方分多條接收后整合在一起顯示。
代碼:

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);
//為發(fā)送短信設(shè)置要監(jiān)聽(tīng)的廣播
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, "加密發(fā)送中,請(qǐng)稍后...", Toast.LENGTH_SHORT).show();
//接收edittext中的內(nèi)容,并且進(jìn)行加密
//倘若char+8超出了表示范圍,則把原字符發(fā)過(guò)去
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);
//發(fā)送短信
//并使用sendTextMessage的第四個(gè)參數(shù)對(duì)短信的發(fā)送狀態(tài)進(jìn)行監(jiān)控
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) {
//發(fā)送成功
Toast.makeText(context, "Send succeeded", Toast.LENGTH_LONG)
.show();
Intent intent1 = new Intent(SendActivity.this, ReceiveActivity.class);
startActivity(intent1);
finish();
} else {
//發(fā)送失敗
Toast.makeText(context, "Send failed", Toast.LENGTH_LONG)
.show();
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//在Activity摧毀的時(shí)候停止監(jiān)聽(tīng)
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() {
//從短信數(shù)據(jù)庫(kù)讀取信息
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);
//此處為了簡(jiǎn)化代碼提高效率,僅僅顯示20條最近短信
for (int i = 0; i < 20; i++) {
//從手機(jī)短信數(shù)據(jù)庫(kù)獲取信息
if(cursor.moveToNext()) {
String address = cursor.getString(cursor.getColumnIndex("address"));
String body = cursor.getString(cursor.getColumnIndex("body"));
long longDate = cursor.getLong(cursor.getColumnIndex("date"));
//將獲取到的時(shí)間轉(zhuǎn)換為我們想要的方式
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中此個(gè)item中的內(nèi)容
//content的內(nèi)容格式如下
//{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存儲(chǔ)數(shù)據(jù)發(fā)送給下一個(gè)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);
//接收內(nèi)容和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);
//對(duì)短信消息進(jìn)行解密后顯示在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="發(fā)送加密短信" 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="加密短信發(fā)送" 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="發(fā)送至:" 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="接收人手機(jī)號(hào)碼" 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="發(fā)送內(nèi)容" 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="單條短信請(qǐng)保持在70字以?xún)?nèi)" 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="發(fā) 送" 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="號(hào)碼:" 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="時(shí)間:" 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>
以上就是本文的全部?jī)?nèi)容,android實(shí)現(xiàn)短信加密,實(shí)現(xiàn)發(fā)送加密短信、解密本地短信,希望對(duì)大家的學(xué)習(xí)有所幫助。
- Android實(shí)戰(zhàn)教程第四篇之簡(jiǎn)單實(shí)現(xiàn)短信發(fā)送器
- Android基礎(chǔ)開(kāi)發(fā)小案例之短信發(fā)送器
- Android開(kāi)發(fā)之電話(huà)撥號(hào)器和短信發(fā)送器實(shí)現(xiàn)方法
- Android短信發(fā)送器實(shí)現(xiàn)方法
- Android發(fā)送短信功能代碼
- Android Mms之:短信發(fā)送流程(圖文詳解)
- android中可以通過(guò)兩種方式調(diào)用接口發(fā)送短信
- Android實(shí)現(xiàn)將已發(fā)送的短信寫(xiě)入短信數(shù)據(jù)庫(kù)的方法
- Android實(shí)現(xiàn)發(fā)送短信功能實(shí)例詳解
- 簡(jiǎn)單實(shí)現(xiàn)android短信發(fā)送器
相關(guān)文章
Android?app啟動(dòng)節(jié)點(diǎn)與上報(bào)啟動(dòng)實(shí)例詳解
系統(tǒng)的啟動(dòng)過(guò)程非常復(fù)雜,下面這篇文章主要給大家介紹了關(guān)于Android?app啟動(dòng)節(jié)點(diǎn)與上報(bào)啟動(dòng)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
Android持久化技術(shù)之SharedPreferences存儲(chǔ)實(shí)例詳解
這篇文章主要介紹了Android持久化技術(shù)之SharedPreferences存儲(chǔ),結(jié)合實(shí)例形式較為詳細(xì)的分析了SharedPreferences存儲(chǔ)的原理、應(yīng)用及具體實(shí)現(xiàn)方法,需要的朋友可以參考下2016-01-01
Android實(shí)現(xiàn)數(shù)據(jù)按照時(shí)間排序
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)數(shù)據(jù)按照時(shí)間排序的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
Android編程單擊圖片實(shí)現(xiàn)切換效果的方法
這篇文章主要介紹了Android編程單擊圖片實(shí)現(xiàn)切換效果的方法,以實(shí)例形式分析了Android布局及切換功能的具體實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android 讀取文件內(nèi)容實(shí)現(xiàn)方法總結(jié)
這篇文章主要介紹了Android 讀取文件內(nèi)容實(shí)現(xiàn)方法的相關(guān)資料,這里提供了幾種方法,大家可以選擇使用,需要的朋友可以參考下2016-10-10
Android自定義控件實(shí)現(xiàn)望遠(yuǎn)鏡效果
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)望遠(yuǎn)鏡效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
android利用handler實(shí)現(xiàn)打地鼠游戲
這篇文章主要為大家詳細(xì)介紹了android利用handler實(shí)現(xiàn)打地鼠游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
Android Studio快捷鍵生成TAG、Log.x日志輸出介紹
這篇文章主要介紹了Android Studio快捷鍵生成TAG、Log.x日志輸出介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
Android ListView滑動(dòng)改變標(biāo)題欄背景漸變效果
這篇文章主要為大家詳細(xì)介紹了Android ListView滑動(dòng)改變標(biāo)題欄背景漸變效果,透明轉(zhuǎn)變成不透明,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07

