Ver Mensaje Individual
  #5 (permalink)  
Antiguo 28/01/2016, 13:46
tote93
 
Fecha de Ingreso: noviembre-2014
Ubicación: Córdoba
Mensajes: 4
Antigüedad: 9 años, 5 meses
Puntos: 0
Respuesta: Iniciación con proyecto

Cita:
Iniciado por cocu3 Ver Mensaje
hola,
Asi es hay que tener un servicio activo en el android que este monitoreando los cambios en el servidor,

saludos
Buenas de nuevo, ya tengo la aplicacion/BD/web casi terminada, he creado un proyecto de prueba para servicios, he creado uno muy simple que al pulsar a un boton inicia dicho servicio y al pulsar otro lo para.
Ahora me gustaria que cada X tiempo ejecutase una acción, por ejemplo un toast, sin embargo tengo un error que no se corregir y que me tiene de cabeza y creo que son por los context.
La aplicación real, cuando aprenda a manejarme un poco mas con el timer y servicios, sera la de que cada X tiempo realize una peticion y obtención de datos de un servidor de BD.
Adjunto el codigo de prueba a ver si puedes ayudarme de nuevo.
Actividad Principal:
Código:
package proyecto.android.myapplication;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void startService(Context mContext, View view){

        Intent intent = new Intent(this, MyService.class);
        startService(intent);
    }

    public void stopService(View view){

        Intent intent = new Intent(this, MyService.class);
        stopService(intent);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
Servicio+TimerTask:
Código:
package proyecto.android.myapplication;

import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

public class MyService extends Service {
    Timer timer;
    MyTask task;
    int DELAY = 1000;
    Context ctx;
    @Override
    public void onCreate() {
        super.onCreate();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (timer != null) {
            timer.cancel();
        }
        timer = new Timer();
        task = new MyTask();

        //delay 1000ms
        timer.schedule(task, DELAY);
        Toast.makeText(this, "Servicio Iniciado", Toast.LENGTH_SHORT).show();
        return START_STICKY;
     }

    @Override
    public void onDestroy() {
       // Toast.makeText(this, "Servicio DETENIDO", Toast.LENGTH_SHORT).show();
        if (timer != null) {
            timer.cancel();
            timer = null;
        }

    }





    class MyTask extends TimerTask {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override

                public void run() {
                    //recibir y enviar datos
                    Toast.makeText(MainActivity, "Servicio runableleleleel", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }






    @Override
    public IBinder onBind(Intent intent) {
       return null;
    }
}
El error esta en las lineas de:
Toast.makeText(MainActivity, "Servicio runableleleleel", Toast.LENGTH_SHORT).show(); //////Indicando que Expression expected

runOnUiThread(new Runnable() ///// runOnUiThreatd(anonymous.java.lang.Runnable
Saludos y gracias por las futuras respuestas.

Última edición por tote93; 28/01/2016 a las 14:46