elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Tutorial básico de Quickjs


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  [Android]Procesos en segundo plano.
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: [Android]Procesos en segundo plano.  (Leído 1,830 veces)
kondrag_X1

Desconectado Desconectado

Mensajes: 157


Ver Perfil
[Android]Procesos en segundo plano.
« en: 21 Octubre 2015, 12:53 pm »

Hola gente, aquí estamos de nuevo a ver si me pueden sacar del follon que yo solo me he metido.

el trabajo: basicamente se trata de crear un servicio desde la actividad principal y que cuando la app se encuentre en segundo el servicio siga activo. el servicio va a ser un escaner de dispositivos bluetooth. el controlador del dispositivo sigue un patrón singleton, es decir, tengo una instancia del objeto que se va instanciando en todas las actividades.

Problema: A continuación os dejo un trozo de código con el desarrollo. Lo que me ocurre es que cuando pongo a escanear y mato el proceso el scaneo continua y no entiendo muy bien porque.

Para el escaneo y para ahorrar bateria hago un scaneo y luego descanso, inicio escaneo y descanso.

Código
  1. public class ServiceDetectionTag extends IntentService {
  2.    private static final String NAMECLASS = "ServiceDetectionTag";
  3.    private static final long STOP_SCAN_TIMER = 10 * 1000;
  4.    private static final long START_SCAN_TIMER = 30 * 1000;
  5.  
  6.    private static Context mContext;
  7.    private static HandlerBLE mHandlerBLE;
  8.    private static ServiceBroadcastReceiver mServiceBroadcastReceiver;
  9.    private static Thread workerThread = null;
  10.    private static Boolean alive;
  11.  
  12.    public ServiceDetectionTag()
  13.    {
  14.        super(NAMECLASS);
  15.    }
  16.  
  17.    /*private volatile Timer mTimerStart;
  18.     private volatile Timer mTimerStop;*/
  19.  
  20.  
  21.    @Override
  22.    protected void onHandleIntent(Intent intent)
  23.    {
  24.        alive = true;
  25.        mHandlerBLE = ((BLE_Application) getApplication()).getmHandlerBLEInstance(this.getApplicationContext());
  26.        ((BLE_Application) getApplication()).resetHandlerBLE();
  27.        mContext = getApplicationContext();
  28.  
  29.        mServiceBroadcastReceiver = new ServiceBroadcastReceiver();
  30.        IntentFilter i = new IntentFilter(ServiceBroadcastReceiver.ACTION_NOTIFY_NEW_DEVICE_FOUND);
  31.        registerReceiver(mServiceBroadcastReceiver, i);
  32.  
  33.        if(workerThread == null || !workerThread.isAlive())
  34.        {
  35.            workerThread = new Thread(new Runnable()
  36.            {
  37.                public void run()
  38.                {
  39.                    while(isAlive())
  40.                    {
  41.                        try
  42.                        {
  43.                            if (Constant.DEBUG)
  44.                                Log.i(Constant.TAG, NAMECLASS + " ## -- onHandleIntent -> Start scanning");
  45.  
  46.                            mHandlerBLE.startLeScan();
  47.                            workerThread.sleep(START_SCAN_TIMER);
  48.  
  49.                            if (Constant.DEBUG)
  50.                                Log.i(Constant.TAG, NAMECLASS + " ## -- onHandleIntent -> Stop scanning");
  51.                            mHandlerBLE.stopLeScan();
  52.                            workerThread.sleep(STOP_SCAN_TIMER);
  53.  
  54.                        } catch (Exception e) {
  55.                            e.printStackTrace();
  56.                        }
  57.                    }
  58.                    /*
  59.                     if (Constant.DEBUG)
  60.                         Log.i(Constant.TAG, NAMECLASS + " ## -- onHandleIntent -> 1º Start scanning");
  61.                     mHandlerBLE.startLeScan();
  62.  
  63.                     mTimerStart = new Timer();
  64.                     mTimerStop = new Timer();
  65.  
  66.                     mTimerStart.scheduleAtFixedRate(new TimerTask() {
  67.                         @Override
  68.                         public void run()
  69.                         {
  70.                             if (Constant.DEBUG)
  71.                                 Log.i(Constant.TAG, NAMECLASS + " ## -- onHandleIntent -> Start scanning");
  72.                             mHandlerBLE.startLeScan();
  73.                         }
  74.                     }, 0, START_SCAN_TIMER + STOP_SCAN_TIMER);
  75.  
  76.                     mTimerStop.scheduleAtFixedRate(new TimerTask() {
  77.                         @Override
  78.                         public void run()
  79.                         {
  80.                             if (Constant.DEBUG)
  81.                                 Log.i(Constant.TAG, NAMECLASS + " ## -- onHandleIntent -> Stop scanning");
  82.                             mHandlerBLE.stopLeScan();
  83.                         }
  84.                     }, 0, START_SCAN_TIMER);*/
  85.                }
  86.            });
  87.            workerThread.start();
  88.        }
  89.    }
  90.  
  91.    @Override
  92.    public void onDestroy()
  93.    {
  94.        super.onDestroy();
  95.        alive = false;
  96.  
  97.        if (Constant.DEBUG)
  98.            Log.i(Constant.TAG, NAMECLASS + " ## -- onDestroy() -> Stop scanning");
  99.        mHandlerBLE.stopLeScan();
  100.  
  101.        try
  102.        {
  103.            workerThread.join();
  104.        } catch (InterruptedException e) {
  105.            e.printStackTrace();
  106.        }
  107.  
  108.        unregisterReceiver(mServiceBroadcastReceiver);
  109.    }
  110.  
  111.    public static Boolean isAlive()
  112.    {
  113.        if(alive != null)
  114.            return !alive;
  115.  
  116.        return false;
  117.    }
  118.  
  119.    public class ServiceBroadcastReceiver extends BroadcastReceiver {
  120.        public static final String ACTION_NOTIFY_NEW_DEVICE_FOUND = "iot_ble.NOTIFY_NEW_DEVICE";
  121.  
  122.        public ServiceBroadcastReceiver() {
  123.            super();
  124.        }
  125.  
  126.        @Override
  127.        public void onReceive(Context context, Intent intent) {
  128.            if (Constant.DEBUG)
  129.                Log.i(Constant.TAG, " ## ServiceBroadcastReceiver -- onReceive -> inside");
  130.            String action = intent.getAction();
  131.            if (action.equals(ServiceBroadcastReceiver.ACTION_NOTIFY_NEW_DEVICE_FOUND)) {
  132.                if (Constant.DEBUG)
  133.                    Log.i(Constant.TAG, " ## ServiceBroadcastReceiver -- onReceive -> sending notication(statusBar)");
  134.  
  135.  
  136.                MyNotificationHandler myNotificationHandler;
  137.                myNotificationHandler = new MyNotificationHandler(mContext);
  138.                myNotificationHandler.SendNotify();
  139.            }
  140.        }
  141.  
  142.    }
  143.  


« Última modificación: 21 Octubre 2015, 12:55 pm por kondrag_X1 » En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
como ejecutar procesos en segundo plano
Programación C/C++
hiisoka 4 4,749 Último mensaje 28 Noviembre 2014, 17:01 pm
por x64core
procesos en segundo plano y cargas
Programación Visual Basic
MC.cover 0 3,174 Último mensaje 6 Noviembre 2016, 02:11 am
por MC.cover
[W10]Como puedo eliminar procesos en segundo plano?
Windows
huchoko 2 2,129 Último mensaje 20 Octubre 2018, 19:10 pm
por huchoko
MOVIDO: [W10]Como puedo eliminar procesos en segundo plano?
Dudas Generales
Songoku 0 1,899 Último mensaje 20 Octubre 2018, 18:24 pm
por Songoku
¿Cómo puedo ejecutar procesos en segundo plano con python 3.x?
Programación General
Jedahee 5 5,780 Último mensaje 4 Noviembre 2019, 18:30 pm
por engel lex
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines