安卓手机放盗开发

本文介绍了一种基于Android系统的手机防盗报警应用开发方案。通过使用手机内置的距离传感器监测手机与用户之间的距离变化,当手机被非法拿走时,系统将触发报警声及振动提醒。文章详细阐述了如何创建服务组件、调用传感器API以及实现声音播放等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目标:防盗,当手机被盗远离口袋时发出报警声音,其原理是使用手机内置的距离传感器,检测到距离变化时播放报警声音

知识点:了解服务(service)的使用方法,调用手机传感器,播放声音


利用service后台监控手机的距离传感器,若检测到距离变化则播放报警声音,我们需要在AndroidManifest.xml中申请手机振动权限,并声明我们的服务组件,Android的四大组件使用时都要在这个文件中声明,AndroidManifest.xml文件如下:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="my.project.phonesecurity">  
  4.   
  5.     <uses-permission android:name="android.permission.VIBRATE"></uses-permission><span style="white-space:pre"> </span><!--振动权限-->  
  6.     <application  
  7.         android:allowBackup="true"  
  8.         android:icon="@mipmap/ic_launcher"  
  9.         android:label="@string/app_name"  
  10.         android:supportsRtl="true"  
  11.         android:theme="@style/AppTheme">  
  12.         <activity android:name=".MainActivity">  
  13.             <intent-filter>  
  14.                 <action android:name="android.intent.action.MAIN" />  
  15.   
  16.                 <category android:name="android.intent.category.LAUNCHER" />  
  17.             </intent-filter>  
  18.         </activity>  
  19. <span style="white-space:pre">  </span><!--注册服务组件-->  
  20.         <service android:name="my.project.phonesecurity.AntiTheftService"/>  
  21.     </application>  
  22. </manifest>  

设置距离报警服务:

上面我们注册了服务组件,接下来就要通过服务实现我们需要的功能,首先新建AntiTheftService类(与上面注册名称一致)继承Service父类,新建的类中会有一个onBind()方法,这是Service中唯一的抽象方法,我们不使用绑定方式启动服务,暂时忽略掉这个方法,重写onCreate()、onStartcommand()和onDestory()这三个方法,onCreate方法在服务创建的时候调用,onStartCommand会在每次服务启动的时候调用,onDstory在服务销毁的时候调用,我们在onCreate方法中获得传感器和振动服务并设置监听,加载音频等,在onDestory方法中停止播放音频,并取消传感器监听器。

传感器的使用方法都比较类似,首先第一步要获取到SensorManager的实例,SensorManager是系统所有传感器的管理器,通过调用getDefaultSensor()方法来得到任意的传感器类型:

Sensor sensor = sensorManager.getDefaultSeneor(Sensor.TYPE_PROXIMITY);

使用Sensor.TYPE_PROXIMITY常量来指定传感器类型。

[java]  view plain  copy
  1. package my.project.phonesecurity;  
  2.   
  3. import android.app.Service;  
  4. import android.content.Intent;  
  5. import android.hardware.Sensor;  
  6. import android.hardware.SensorEvent;  
  7. import android.hardware.SensorEventListener;  
  8. import android.hardware.SensorManager;  
  9. import android.media.AudioManager;  
  10. import android.media.SoundPool;  
  11. import android.os.IBinder;  
  12. import android.os.Vibrator;  
  13. import android.util.Log;  
  14.   
  15. /** 
  16.  * Created by lx on 2017/9/4. 
  17.  */  
  18.   
  19. public class AntiTheftService extends Service {  
  20.     private static final String TAG = "PhoneSecurity";  
  21.     private SensorManager mgr;  //传感器管理  
  22.     private Sensor proximity; //距离传感器  
  23.     private Vibrator vibrator;  //振动马达  
  24.     private double lastVal = -1;  
  25.     private SoundPool soundPool;  
  26.     private int soundID = 0;    //报警音效id  
  27.     private int streamID = 0;   //报警音效播放流id  
  28.   
  29.     @Override  
  30.     public IBinder onBind(Intent intent) {  
  31.         return null;  
  32.     }  
  33.   
  34.     @Override  
  35.     public void onCreate() {  
  36.         super.onCreate();  
  37.         //初始化音频资源池,加载报警音频  
  38.         soundPool = new SoundPool(10, AudioManager.STREAM_SYSTEM,5);  
  39.         soundID = soundPool.load(this,R.raw.beep,1);  
  40.         mgr = (SensorManager) getSystemService(SENSOR_SERVICE);  
  41.         proximity = mgr.getDefaultSensor(Sensor.TYPE_PROXIMITY);  
  42.         vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);  
  43.   
  44.         mgr.registerListener(proximityListener,proximity,SensorManager.SENSOR_DELAY_UI);  
  45.     }  
  46.   
  47.     @Override  
  48.     public int onStartCommand(Intent intent, int flags, int startId) {  
  49.         Log.i(TAG,"服务启动");  
  50.         return super.onStartCommand(intent, flags, startId);  
  51.     }  
  52.   
  53.     @Override  
  54.     public void onDestroy() {  
  55.   
  56.         soundPool.stop(streamID);  
  57.         soundPool.unload(soundID);  
  58.         mgr.unregisterListener(proximityListener);  
  59.         super.onDestroy();  
  60.     }  
  61.   
  62.     private SensorEventListener proximityListener = new SensorEventListener() {  
  63.         @Override  
  64.         public void onSensorChanged(SensorEvent event) {  
  65.             double thisVal = event.values[0];  
  66.             Log.d(AntiTheftService.TAG,"onSensorChanged["+thisVal+":"+lastVal+"]");  
  67.             if(lastVal == -1){  
  68.                 //首次检测的值保存到lastVal,以便后面比较判断是否远离  
  69.                 lastVal = thisVal;  
  70.                 //短振动提示  
  71.                 vibrator.vibrate(100);  
  72.             }  
  73.             else {  
  74.                 if(thisVal != lastVal){  
  75.                     //两次检测的距离不同,说明有异常,长振动并报警  
  76.                     vibrator.vibrate(1000);  
  77.                     if(streamID == 0){  
  78.                         streamID = soundPool.play(soundID,1,1,0,-1,1);  
  79.                     }  
  80.                 }  
  81.             }  
  82.         }  
  83.   
  84.         @Override  
  85.         public void onAccuracyChanged(Sensor sensor, int accuracy) {  
  86.   
  87.         }  
  88.     };  
  89. }  

界面实现:

设置三个按钮,一个用于开始服务,一个用于停止服务,一个将程序隐藏到后台,界面部分比较简单,主界面Activity如下:

[java]  view plain  copy
  1. package my.project.phonesecurity;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8.   
  9. import java.util.Timer;  
  10. import java.util.TimerTask;  
  11.   
  12. public class MainActivity extends Activity {  
  13.   
  14.     Button startButton,stopButton,hideButton;  
  15.     private Timer timer;  
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.   
  21.         startButton = (Button) findViewById(R.id.startButton);  
  22.         stopButton = (Button) findViewById(R.id.stopButton);  
  23.         hideButton = (Button) findViewById(R.id.hideButton);  
  24.   
  25.         startButton.setOnClickListener(new ButtonListener());  
  26.         stopButton.setOnClickListener(new ButtonListener());  
  27.         hideButton.setOnClickListener(new ButtonListener());  
  28.     }  
  29.     class ButtonListener implements View.OnClickListener{  
  30.         @Override  
  31.         public void onClick(View v) {  
  32.             switch(v.getId()){  
  33.                 case R.id.startButton:  
  34.                     //手机放入口袋需要时间,设置延迟  
  35.                     int delay = 5000;  
  36.                     timer = new Timer();  
  37.                     timer.schedule(new TimerTask() {  
  38.                         @Override  
  39.                         public void run() {  
  40.                             Intent intent = new Intent(MainActivity.this,AntiTheftService.class);  
  41.                             startService(intent);  
  42.                         }  
  43.                     },delay);  
  44.                     startButton.setClickable(false);  
  45.                     stopButton.setClickable(true);  
  46.                     break;  
  47.                 case R.id.stopButton:  
  48.                     if(timer != null) {  
  49.                         timer.cancel();  
  50.                         timer.purge();  
  51.                     }  
  52.                     Intent intent = new Intent(MainActivity.this,AntiTheftService.class);  
  53.                     stopService(intent);  
  54.                     startButton.setClickable(true);  
  55.                     stopButton.setClickable(false);  
  56.                     break;  
  57.                 case R.id.hideButton:  
  58.                     //实现home按键的效果,隐藏当前Activity界面  
  59.                     Intent intent2 = new Intent(Intent.ACTION_MAIN);  
  60.                     intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  61.                     intent2.addCategory(Intent.CATEGORY_HOME);  
  62.                     startActivity(intent2);  
  63.             }  
  64.         }  
  65.     }  
  66. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值