AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.MyApplication" tools:targetApi="31"> <activity android:name=".MainActivity" android:exported="true" android:label="@string/app_name" android:theme="@style/Theme.AppCompat.Light.DarkActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".MovementDetectionService" android:enabled="true" android:exported="true" /> </application> </manifest> |
MainActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.example.myapplication import android.content.Intent import android.os.Bundle import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Iniciar el servicio al abrir la aplicación startService(Intent(this, MovementDetectionService::class.java)) } } |
MovementDetectionService.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
package com.example.myapplication import android.app.Service import android.content.Context import android.content.Intent import android.hardware.Sensor import android.hardware.SensorEvent import android.hardware.SensorEventListener import android.hardware.SensorManager import android.os.Handler import android.os.IBinder import android.os.Vibrator import android.widget.Toast class MovementDetectionService : Service(), SensorEventListener { private lateinit var sensorManager: SensorManager private lateinit var accelerometer: Sensor private var lastX: Float = 0f private var lastY: Float = 0f private var lastZ: Float = 0f private var lastTime: Long = 0L private val threshold = 0.2f // Ajustar este valor según sea necesario private val delay = 5000L // Cambiado a 10 segundos private var handler: Handler? = null private lateinit var runnable: Runnable override fun onCreate() { super.onCreate() Toast.makeText(this, "funcionando", Toast.LENGTH_SHORT).show() sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)!! sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL) handler = Handler() runnable = Runnable { // Activar alarma cuando el teléfono esté inactivo después del retraso triggerAlarm() } } override fun onDestroy() { super.onDestroy() sensorManager.unregisterListener(this) handler?.removeCallbacks(runnable) } override fun onBind(intent: Intent?): IBinder? { return null } override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {} override fun onSensorChanged(event: SensorEvent?) { event?.let { if (it.sensor.type == Sensor.TYPE_ACCELEROMETER) { val currentTime = System.currentTimeMillis() if (currentTime - lastTime > delay) { lastTime = currentTime val x = it.values[0] val y = it.values[1] val z = it.values[2] val deltaX = Math.abs(x - lastX) val deltaY = Math.abs(y - lastY) val deltaZ = Math.abs(z - lastZ) if (deltaX < threshold && deltaY < threshold && deltaZ < threshold) { handler?.postDelayed(runnable, delay) } else { handler?.removeCallbacks(runnable) } lastX = x lastY = y lastZ = z } } } } private fun triggerAlarm() { Toast.makeText(this, "alarma", Toast.LENGTH_SHORT).show() // Activar una alarma o cualquier otra acción aquí val vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator vibrator.vibrate(1000) // Vibrar durante 1 segundo // Se puede reproducir un sonido, mostrar una notificación, etc., para alertar al usuario } } |