2014-05-13 3 views
-2

Comment puis-je rappeler mon service Android toutes les 10 secondes? Ceci est ma classe de service.Rappel des services Android

package com.example.vaccinationsystem; 

import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
import android.util.Log; 
import android.widget.Toast; 

public class BackgroundServices extends Service{ 

    private static final String TAG = "MyService"; 

    @Override 
    public IBinder onBind(Intent arg0) { 
     return null; 
     } 

    @Override 
    public void onCreate() { 
    Toast.makeText(this, "Congrats! MyService Created", Toast.LENGTH_LONG).show(); 
    Log.d(TAG, "onCreate"); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) { 
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); 
    Log.d(TAG, "onStart"); 
    } 

    @Override 
    public void onDestroy() { 
    Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show(); 
    Log.d(TAG, "onDestroy"); 
    } 
} 

Je l'appelle dans une autre classe par:

 startService(new Intent(this, BackgroundServices.class));  

Comment peut-il courir après toutes les 10 secondes.

Répondre

0

essayer cette AlarmManager

public void startTimer() 
    { 
     Log.i("timer Striii" , "timer is "); 
     Intent Service1 = new Intent(MainActivity.this , BackgroundServices.class); 
     PendingIntent piService1 = PendingIntent.getService(MainActivity.this , 0 , Service1 , PendingIntent.FLAG_UPDATE_CURRENT); 
     AlarmManager alarmManager1 = (AlarmManager) MainActivity.this.getSystemService(Context.ALARM_SERVICE); 
     alarmManager1.cancel(piService1); 
     alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP , System.currentTimeMillis() , 16500 , piService1); 
    } 
+0

Il y a un problème. Il va créer le service à chaque fois. Je veux juste commencer. ? –