短信加密此类功能由于新手学习的需求量较小,所以在网上很少有一些简单的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;
/**
* Created by 佳佳 on 2015/12/21.
*/
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
ReceiveActivity_show:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
/**
* Created by 佳佳 on 2015/12/24.
*/
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:
activity_send:
activity_receive:
activity_receive_show:
源码地址:http://download.csdn.net/detail/double2hao/9376630