哎,忙了一中午就搞了一个小实例,发现自己的效率还有待提高,不过,虽说是一个小实例,让我对短信这块掌握了许多!
先贴代码:(必要的解释都在注释中,不懂得可以留言)!
MainActivity:PhoneFinder.java
- package com.android.yhb;
- import java.math.BigInteger;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import android.app.Activity;
- import android.content.SharedPreferences.Editor;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- public class PhoneFinder extends Activity {
- private EditText edit01;
- private EditText edit02;
- private Button button_ok;
- private TextView textview;
- static final String PASSWORD_PREF_KEY = "passwd";
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- edit01 = (EditText)findViewById(R.id.password);
- edit02 = (EditText)findViewById(R.id.password_confirm);
- textview = (TextView)findViewById(R.id.text1);
- button_ok = (Button)findViewById(R.id.ok);
- button_ok.setOnClickListener(listener);
- /*SharedPreferences setting = getSharedPreferences(PASSWORD_PREF_KEY, 0);
- String t1 = setting.getString("PASSWORD", null);
- edit01.setText(t1);
- edit02.setText(t1);*/
- }
- OnClickListener listener = new OnClickListener() {
- public void onClick(View v) {
- String p1 = edit01.getText().toString();
- String p2 = edit02.getText().toString();
- if(p1.equals(p2)) {
- if(p1.length() >= 6 && p2.length() >= 6) {
- Editor edit = getSharedPreferences(PASSWORD_PREF_KEY, MODE_PRIVATE).edit();
- String md5hash = getMd5Hash(p1);
- edit.putString("PASSWORD", md5hash);
- edit.commit();
- textview.setText("password updated");
- } else {
- textview.setText("password must be at least 6 characters");
- }
- } else {
- edit01.setText("");
- edit02.setText("");
- textview.setText("password do not match");
- }
- }
- };
- //这里是MD5的加密方法
- public static String getMd5Hash(String input) {
- try {
- MessageDigest md = MessageDigest.getInstance("MD5");
- byte[] messageDigest = md.digest(input.getBytes());
- BigInteger number = new BigInteger(1,messageDigest);
- String md5 = number.toString(16);
- while (md5.length() < 32)
- md5 = "0" + md5;
- return md5;
- } catch(NoSuchAlgorithmException e) {
- Log.e("MD5", e.getMessage());
- return null;
- }
- }
- }
FinderReceiver.java
- package com.android.yhb;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.location.Location;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.telephony.gsm.SmsManager;
- import android.telephony.gsm.SmsMessage;
- public class FinderReceiver extends BroadcastReceiver {
- Context context;
- @Override
- public void onReceive(Context context, Intent intent) {
- this.context = context;
- SharedPreferences passwdfile = context.getSharedPreferences(
- PhoneFinder.PASSWORD_PREF_KEY, 0);
- String correctMd5 = passwdfile.getString(PhoneFinder.PASSWORD_PREF_KEY,
- null);
- if (correctMd5 != null) {
- /*SmsMessage[] messages = Telephony.Sms.Intents
- .getMessagesFromIntent(intent);*/
- Bundle bundle = intent.getExtras();
- Object pdus[] = (Object[]) bundle.get("pdus");//SMS消息的byte[]存储在pdus中
- SmsMessage smsMessage[] = new SmsMessage[pdus.length];
- //for (SmsMessage msg : smsMessage) {
- for(int i = 0; i < smsMessage.length; i++) {
- smsMessage[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
- if (smsMessage[i].getMessageBody().contains("SMSLOCATE:")) {
- String[] tokens = smsMessage[i].getMessageBody().split(":");
- if (tokens.length >= 2) {
- String md5hash = PhoneFinder.getMd5Hash(tokens[1]);
- if (md5hash.equals(correctMd5)) {
- String to = smsMessage[i].getOriginatingAddress();//以字符串的形式返回发送者(sender)的原始地址
- SmsManager sm = SmsManager.getDefault();
- /*LocationManager lm =
- (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
- SmsManager sm = SmsManager.getDefault();
- Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
- double latitude = location.getLatitude(); //经度
- double longitude = location.getLongitude(); //纬度
- double altitude = location.getAltitude(); //海拔
- sm.sendTextMessage(to, null, "经度:" + String.valueOf(latitude) + "纬度:" + String.valueOf(longitude) + "海拔:" + String.valueOf(altitude),
- null, null); */
- sm.sendTextMessage(to, null, "成功了!", null, null);
- NotificationManager nm =
- (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
- Notification notification = new Notification(android.R.drawable.ic_dialog_info, "您的地理位置", System.currentTimeMillis());
- PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, PhoneFinder.class), 0);
- notification.setLatestEventInfo(context, "GPS地址", "你已经被发现了,赶紧把手机还给我,不然我就报警!", contentIntent);
- nm.notify(R.string.service_start, notification);
- }
- }
- } }
- //}
- }
- }
- }
这段代码中我注释了关于GPS的求解过程,主要是因为在模拟器上不好搞GPS的位置,我试着用DDMS的send发送GPS坐标,但是不见效果,不知为何。所以注释掉,我想在真机上肯定会有效果的,因为求解GPS坐标这段代码完整无误,经常用!
这个实例实际上是我在一个论坛里看到的,他的源代码我运行不成功,并且他的源代码好像是基于Android1.0API的,经过多方努力,终于把他改成新版本下的程序,主要改动了第36行代码,第36行代码主要是从pdus中恢复出SmsMessage对象。