android项目:手机防盗软件

基础界面的设置

 <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="numberPassword"
        android:layout_alignParentRight="true"
        android:layout_alignParentLeft="true"
        android:hint="请输入用户名"/>


    <Switch
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="是否开启防盗功能"
        android:layout_below="@id/editText3"
        android:layout_alignParentRight="true"
        android:layout_alignParentLeft="true"/>

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="numberPassword"
        android:layout_below="@id/editText2"
        android:hint="请输入安全号码"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"/>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="0dp"
        android:orientation="vertical">

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/editText3"
            android:layout_alignParentLeft="true"
            android:onClick="lock"
            android:text="锁屏" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:onClick="stopClick"
            android:text="关闭" />
    </LinearLayout>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/button3"
        android:text="" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="保存"
        android:layout_below="@id/switch1"
        android:layout_alignParentRight="true"
        android:layout_alignParentLeft="true"
        android:onClick="readClick"/>

在这里插入图片描述

内部相关代码

public class MainActivity extends AppCompatActivity {
    public static final Object PREF_PHONE_NUMBER = "PREF_PHONE_NUMBER";
    private Switch switch1;
    TelephonyManager tm;
    private EditText editText;
    private TextView textView;
    String model = android.os.Build.MODEL;
    String brand = android.os.Build.BRAND;
    String device = android.os.Build.DEVICE;
    String message;
    StringBuffer sb;
    StringBuffer sa;
    ComponentName mAdminName;
    DevicePolicyManager mDPM;
    public static final String SP_SAFE_KEY = "SP_SAFE_KEY";
    public static final String SP_MYNUMBER_KEY = "SP_MYNUMBER_KEY";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        switch1 = findViewById(R.id.switch1);
        editText = findViewById(R.id.editText2);
        editText =findViewById(R.id.editText3);
        textView =findViewById(R.id.textView);
        mAdminName = new ComponentName(this, MainActivity2.class);
        mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        //如果设备管理器尚未激活,这里会启动一个激活设备管理器的Intent,具体的表现就是第一次打开程序时,手机会弹出激活设备管理器的提示,激活即可。
        if (!mDPM.isAdminActive(mAdminName)) {
            showAdminManagement(mAdminName);
        }
        readPrivateFileClick();
        readSafeClick();
        switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked == true) {
                    Intent intent = new Intent(MainActivity.this, MyService.class);
                    startService(intent);
                    testTelephoneManager();
                    writePrivateFileClick();
                    Toast.makeText(MainActivity.this, "开启防盗", Toast.LENGTH_SHORT).show();
                    if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                        // TODO: Consider calling
                        //    ActivityCompat#requestPermissions
                        // here to request the missing permissions, and then overriding
                        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                        //                                          int[] grantResults)
                        // to handle the case where the user grants the permission. See the documentation
                        // for ActivityCompat#requestPermissions for more details.
                        return;
                    }
                    textView.setText("唯一的设备ID=" + tm.getDeviceId() + "\n" +
                            "手机号=" + tm.getLine1Number() + "\n" +
                            "获取ISO标准的国家码·即国际长途区号=" + tm.getNetworkCountryIso() + "\n" +
                            "当前使用的网络类型=" + tm.getNetworkType() + "\n" +
                            "手机类型=" + tm.getPhoneType() + "\n" +
                            "SIM的状态信息=" + tm.getSimState() + "\n" +
                            "SIM卡的序列号=" + tm.getSimSerialNumber() + "\n" +
                            "服务商名称=" + tm.getSimOperatorName() + "\n" +
                            "型号=" + model + "\n" +
                            "品牌=" + brand + "\n" +
                            "设备名=" + device
                    );
//                    message=tm.getLine1Number();
//                    smsManager();
                } else {
                    Toast.makeText(MainActivity.this, "关闭防盗", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(MainActivity.this, MyService.class);
                    stopService(intent);
                }
            }
        });
    }
    //电话服务管理器的API方法
    public void testTelephoneManager() {
        tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        System.out.println("电话状态=" + tm.getCallState());
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        //测试来电显示:直接发送一个广播
        sendBroadcast(new Intent(this, PhoneListenerReceiver.class));
//        tm.listen(new MyPhoneStateListener(),PhoneStateListener.LISTEN_CALL_STATE);
    }
//    /**
//     * 电话服务的监听器
//     */
    private static class MyPhoneStateListener extends PhoneStateListener {
        @Override
        public void onCallStateChanged(int state, String phoneNumber) {
            super.onCallStateChanged(state, phoneNumber);
            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING:
                    System.out.println("正在响铃");
                    break;
                case TelephonyManager.CALL_STATE_IDLE:
                    System.out.println("挂机状态");
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    System.out.println("正在接听电话...");
                    break;
            }
        }
    }
    //存入SIM卡序列号
    public void writePrivateFileClick() {
        try {
            OutputStream out = openFileOutput("sim.txt", Context.MODE_PRIVATE);
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_NUMBERS) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            String info = tm.getSimSerialNumber();
            byte[] bytes=info.getBytes();
            out.write(bytes,0,bytes.length);//写入
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //读取SIM卡序列号
    public void readPrivateFileClick(){
        try {
            InputStream in=openFileInput("sim.txt");
            byte[] bytes=new byte[1024];
            sb=new StringBuffer();
            int len=-1;
            while((len=in.read(bytes))!=-1){
                sb.append(new String(bytes,0,len));
            }
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //存入安全号码
    public void readClick(View v){
        try {
            OutputStream out = openFileOutput("safe.txt", Context.MODE_PRIVATE);
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_NUMBERS) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            String info =editText.getText().toString();
            byte[] bytes=info.getBytes();
            out.write(bytes,0,bytes.length);//写入
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String safe=editText.getText().toString();
        SPUtils.putValue(MainActivity.this, SP_SAFE_KEY, safe);//存入安全号码
        SPUtils.putValue(MainActivity.this, SP_MYNUMBER_KEY, sb.toString());//存入SIM卡序列号
    }
    //读取安全号码
    public void readSafeClick(){
        try {
            InputStream in=openFileInput("safe.txt");
            byte[] bytes=new byte[1024];
            sa=new StringBuffer();
            int len=-1;
            while((len=in.read(bytes))!=-1){
                sa.append(new String(bytes,0,len));
            }
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //锁屏功能
    public void lock(View v){
        if (mDPM.isAdminActive(mAdminName)) {
            mDPM.lockNow();
        }
    }
    private void showAdminManagement(ComponentName mAdminName) {
        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "activity device");
        startActivityForResult(intent,1);
    }

    public void stopClick(View view){
        finish();
    }
}

广播实现对更改SIM与开机自启的相关设定(每一次开机都会启动一次,将显示其中代码删减可实现不弹出担任可后台运行进行相关的监控)

public class BootBroadcastReceiver extends BroadcastReceiver {
        public BootBroadcastReceiver(){}
    String ACTION = "android.intent.action.BOOT_COMPLETED";
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent .getAction().equals(ACTION)){
            Intent intent1 =new Intent(context,MainActivity.class);
            intent1 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


            ***~~context.startActivity(intent1);~~ ***



            SharedPreferences sp = context.getSharedPreferences("Test", Context.MODE_PRIVATE);
            String safenumber = sp.getString("SP_SAFE_KEY", "");
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            String nowsim = tm.getSimSerialNumber();
            String nownum=tm.getLine1Number();
            String savedsim = sp.getString("SP_MYNUMBER_KEY", "");
            if (!nowsim.equals(savedsim)) {
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(safenumber, null, nownum, null, null);
            }
            Toast.makeText(context,"重启成功",Toast.LENGTH_SHORT).show();
        }
    }
}

服务器端的相关设置

public class MyService extends Service {
    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("my Service create");
    }
    //在该方法中实现服务的核心业务
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        //使用线程完成长时间的工作
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("onStartCommand");
            }
        }).start();

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("my service Destroy");
    }
}

实现对电话的监听

public class PhoneListenerReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        TelephonyManager tm = (TelephonyManager) context.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
        tm.listen(new MyPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);
        System.out.println("PhoneListenerReceiver started");
    }
    static WindowManager wm=null;
    private class MyPhoneStateListener extends PhoneStateListener{
        private Context context;
        TextView textView =null;
        public MyPhoneStateListener(Context context){
            this.context=context;
        }

        @Override
        public void onCallStateChanged(int state, String phoneNumber) {
            super.onCallStateChanged(state, phoneNumber);
            if (state == TelephonyManager.CALL_STATE_RINGING){
                wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
                WindowManager.LayoutParams params =new WindowManager.LayoutParams();

                params.type=WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;

                params.flags=WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

                params.width =WindowManager.LayoutParams.WRAP_CONTENT;
                params.height=WindowManager.LayoutParams.WRAP_CONTENT;
                textView =new TextView(context);
                textView.setText("当前来电号码为:"+phoneNumber);
                wm.addView(textView,params);
            }else if (state ==TelephonyManager.CALL_STATE_IDLE){
                if (wm!=null){
                    wm.removeView(textView);
                    wm =null ;
                }
            }
        }
    }

}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值