文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>手机防盗小程序+src

手机防盗小程序+src

时间:2010-08-06  来源:niuniu2006t

经过这两天的查阅和思考,终于把这个小程序比较完整的实现了,额,还是个demo,但是功能比较完整了。
主要的功能如下:
1.可以设置当前的手机号码,以后当手机开机时,就检查一下sim卡号码跟记录的号码是否一致,如果不一致,就向指定的信箱发信息,内容是当前的手机号码。
2.向手机发送特定内容的短信,就可以开启一个后台服务,发送当前手机位置到指定的邮箱,以后位置发生变化时,就继续发送位置到指定的邮箱。
首先是文件的结构:

其中,
smsPopUp.java是主activity,开始后,就会设置电话号码。
bootChecker.java是一个broadcastreceiver,主要接收开机的广播,然后检查手机号是否发生变化,如果发生变化,就向指定邮箱发送当前手机号,否则就验证通过。
smsreceiver.java是用来监听短信的broadcastreceiver,如果内容是指定的内容,就启动服务,发送位置信息到指定的邮箱。
Gmailsender封装了邮件发送,可以更改成其他的邮箱。只要修改 private String mailhost = "smtp.gmail.com";  字段即可。
SharedPreferencesHelper封装了SharedPreferences的操作,用来保存数据到手机里面。
以下是相关代码:
bootChecker.java

package sms.PopUp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class bootChecker extends BroadcastReceiver{
    static final String ACTION = "android.intent.action.BOOT_COMPLETED";
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        {
            TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        String simTel = tm.getLine1Number();
        SharedPreferencesHelper sp = new SharedPreferencesHelper(context, "phoneNum");
     String tel = sp.getValue("phoneNum");
     if(simTel.equals(tel))
     {  
        Toast.makeText(context, "phone number validates success ^_^", Toast.LENGTH_LONG);
     }
     else
     {
         GmailSender sender = new GmailSender("[email protected]", "sender_passwd");
     try {
                    sender.sendMail("tel number changed!!!","your phone's new number changed to "+simTel,"[email protected]","receiver");
                } catch (Exception e) {
                    // TODO Auto-generated catch block

                    e.printStackTrace();
                }
     Toast.makeText(context, "complete...", Toast.LENGTH_LONG).show();
     }
        }
    }
}


GmailSender.java

package sms.PopUp;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import java.util.Properties;

public class GmailSender extends javax.mail.Authenticator {
    private String mailhost = "smtp.gmail.com";
    private String user;
    private String password;
    private Session session;


    public GmailSender(String user, String password) {
        this.user = user;
        this.password = password;

        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.host", mailhost);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.quitwait", "false");

        session = Session.getDefaultInstance(props, this);
    }

    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(user, password);
    }

    public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
        try{
        MimeMessage message = new MimeMessage(session);
        DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
        message.setSender(new InternetAddress(sender));
        message.setSubject(subject);
        message.setDataHandler(handler);
        if (recipients.indexOf(',') > 0)
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
        else
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
        Transport.send(message);
        }catch(Exception e){
        }
    }

    public class ByteArrayDataSource implements DataSource {
        private byte[] data;
        private String type;

        public ByteArrayDataSource(byte[] data, String type) {
            super();
            this.data = data;
            this.type = type;
        }
        public ByteArrayDataSource(byte[] data) {
            super();
            this.data = data;
        }
        public void setType(String type) {
            this.type = type;
        }
        public String getContentType() {
            if (type == null)
                return "application/octet-stream";
            else
                return type;
        }
        public InputStream getInputStream() throws IOException {
            return new ByteArrayInputStream(data);
        }
        public String getName() {
            return "ByteArrayDataSource";
        }
        public OutputStream getOutputStream() throws IOException {
            throw new IOException("Not Supported");
        }
    }
}

SharedPreferencesHelper.java

package sms.PopUp;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;

public class SharedPreferencesHelper {

    SharedPreferences sp;
    SharedPreferences.Editor editor;
       
     Context context;
       
    public SharedPreferencesHelper(Context c,String name){
         context = c;
         sp = context.getSharedPreferences(name, 0);
         editor = sp.edit();
     }
    public void putValue(String key, String value){
        editor = sp.edit();
        editor.putString(key, value);
        editor.commit();
    }
    public String getValue(String key){
        return sp.getString(key, null);
     }
}

smsPopUp.java

package sms.PopUp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.*;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class smsPopUp extends Activity {
    /** Called when the activity is first created. */
    String tel;
     EditText et = null;
     public static String sendmail;
     public static String sendmailpasswd;
     public static String receivemail;
     public static String messagesrc="Alan:start";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
     // String imei = tm.getDeviceId();

     tel = tm.getLine1Number();
        et = (EditText)findViewById(R.id.EditText01);
        et.setText(tel);
        Button btn = (Button)findViewById(R.id.Button01);
        btn.setOnClickListener(new Button.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
             SharedPreferencesHelper sp = new SharedPreferencesHelper(getBaseContext(), "phoneNum");
             String etnum = et.getText().toString();
             sp.putValue("phoneNum",etnum);
             Toast.makeText(getBaseContext(), "Your phone number "+etnum+" has been recorded ^_^", Toast.LENGTH_LONG).show();
             et = (EditText)findViewById(R.id.EditText02);
             sendmail = et.getText().toString();
             et = (EditText)findViewById(R.id.EditText03);
             sendmailpasswd = et.getText().toString();
             et = (EditText)findViewById(R.id.EditText04);
             receivemail = et.getText().toString();
             et = (EditText)findViewById(R.id.EditText05);
             messagesrc= et.getText().toString();
             finish();
            }
        });
    }
}

smsreceiver.java

package sms.PopUp;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;

public class smsreceiver extends BroadcastReceiver{
    private static final String mACTION =
         "android.provider.Telephony.SMS_RECEIVED";
     LocationListener loclistener;
         @Override
         public void onReceive( final Context context, Intent intent)
         {
         // TODO Auto-generated method stub
       
         // if (intent.getAction().equals(mACTION))

         {
        
         StringBuilder sb = new StringBuilder();

         Bundle bundle = intent.getExtras();
        
         if (bundle != null)
         {
         Object[] myOBJpdus = (Object[]) bundle.get("pdus");
         SmsMessage[] messages = new SmsMessage[myOBJpdus.length];
         for (int i = 0; i<myOBJpdus.length; i++)
         {
         messages[i] =
         SmsMessage.createFromPdu((byte[]) myOBJpdus[i]);
         }
         for (SmsMessage currentMessage : messages)
         {
         sb.append(currentMessage.getDisplayMessageBody());
         }
         }
         String sms = sb.toString();
         // Toast.makeText(context, sms, Toast.LENGTH_LONG).show();

         if(sms.equals("Alan:start"))
         {
              Toast.makeText(context, "got signal", Toast.LENGTH_LONG).show();
              LocationManager lm = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
                   Location loc = (Location) lm.getLastKnownLocation(lm.NETWORK_PROVIDER);
              GmailSender sender = new GmailSender("[email protected]", "sender_passwd");
                try {
                        sender.sendMail("location",loc.getLongitude()+","+loc.getLatitude(),"[email protected]","[email protected]");
                } catch (Exception e) {
                    // TODO Auto-generated catch block

                    e.printStackTrace();
                }
              loclistener =
                  new LocationListener()
                  {
                  @Override
                  public void onLocationChanged(Location location)
                  {
                  // TODO Auto-generated method stub

                 
                  /* ���&#1467;��&#1397;�λ�&#248;��&#689;����location����getMyLocation */
                  Toast.makeText(context, location.getLongitude()+","+location.getLatitude(), Toast.LENGTH_LONG).show();
                 
                  GmailSender sender = new GmailSender("[email protected]", "sender_passwd");
                  try {
                          sender.sendMail("location",location.getLongitude()+","+location.getLatitude(),"[email protected]","[email protected]");
                  } catch (Exception e) {
                      // TODO Auto-generated catch block

                      e.printStackTrace();
                  }
                  }
                 
                  @Override
                  public void onProviderDisabled(String provider)
                  {
                  // TODO Auto-generated method stub

                  loclistener = null;
                  }
                 
                  @Override
                  public void onProviderEnabled(String provider)
                  {
                  // TODO Auto-generated method stub

                 
                  }
                 
                  @Override
                  public void onStatusChanged(String provider,
                  int status, Bundle extras)
                  {
                  // TODO Auto-generated method stub

                 
                  }
                  };
                  lm.requestLocationUpdates(lm.NETWORK_PROVIDER, 1000, 10, loclistener);
             
         }
         }
         }
}

然后呢,就是整个程序的纲要了,AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest package="sms.PopUp"
    android:versionCode="1"
    android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".smsPopUp"
        android:label="@string/app_name">
        <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
      <receiver android:name="smsreceiver">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
      </receiver>
      
      <receiver android:name="bootChecker" >
          <intent-filter>
              <action android:name="android.intent.action.BOOT_COMPLETED" />
           <category android:name="android.intent.category.HOME" />
        </intent-filter>
    </receiver>
    </application>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
    <uses-permission android:name="android.permission.RECEIVE_SMS">      </uses-permission>
    <uses-permission android:name="android.permission.INTERNET">          </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"> </uses-permission>
</manifest>


以上就是主要的代码了。 需要进一步完善的地方:上面发件地址,密码,收件地址,都是写在代码里面,可以像phoneNum那样写到配置文件里面。 当发现手机号码变化时,可以根据特定信息,监控发送的短信,拨号记录之类的,也可以进行监控。 更发散的思考,其实不用位置也可以,知道手机号,就可以去营业厅寻求帮助。或者,根据监控的信息,或者根据6度理论,以这个号码为根,以跟他有通信关系的为边,来个深度为6的光搜,应该能找到个我认识的人吧  嘎嘎。。。 然后反推~~  @_@ 缺陷:其实还是有硬伤的。如果对方直接把手机恢复出场设置,那就完蛋了。。。。
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载