2014-05-08 4 views
-4

J'utilise alarmManager pour lancer le service toutes les heures que le service crée une notification, mais le résultat est que je reçois plus d'une notification en une heure, le service lance-t-il la méthode onStartCommand() ou que se passe-t-il exactement?Utiliser AlarmManger pour démarrer le service

AlarmManager:

cal = Calendar.getInstance(); 
     triggerAtTime = cal.getTimeInMillis()+ (1 * 30 * 60 * 1000); // starts in 30 minutes 
     repeat_alarm_every = (1 * 60 * 60 * 1000); // repeat every 2 hour 
     int _id = (int) System.currentTimeMillis(); 
     Intent intent = new Intent(Alarm.this, NotificaSpeed.class); 
     pintent = PendingIntent.getService(Alarm.this, _id, intent, 0); 
     alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
     alarm.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtTime, repeat_alarm_every, pintent); 

service:

public class NotificaSpeed extends Service{ 

@Override 
public IBinder onBind(Intent intent) { 
    // TODO Auto-generated method stub 
    return null; 
} 
@Override 
public void onCreate() { 

    mNotificationManager = (NotificationManager) getSystemService(ns); 
    super.onCreate(); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 

    new VerifySpeedCar().execute(); 
    return super.onStartCommand(intent, flags, startId); 
} 

public void notification(int carsNumber, int speed){ 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(NotificaSpeed.this); 
    if(carsNumber != 0) 

     notificationBuilder.setContentTitle(carsNumber+" cars are exceeding the speed"); 

         .setSmallIcon(R.drawable.ic_launcher) 
         .setAutoCancel(true); 
    final int _idI = (int) System.currentTimeMillis(); 
    Intent notificationIntent = new Intent(NotificaSpeed.this, ListCars.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, _idI, notificationIntent, 0); 
    notificationBuilder.setContentIntent(contentIntent); 

    notificationBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE); 

    mNotificationManager.notify(SIMPLE_NOTIFICATION_ID, notificationBuilder.build()); 

} 

class VerifySpeedCar extends AsyncTask<String, String, String> { 

protected String doInBackground(String... args) { 
    // Building Parameters 
    List<NameValuePair> params = new ArrayList<NameValuePair>(); 
    params.add(new BasicNameValuePair(TAG_ACCOUNTID, accountID)); 
    params.add(new BasicNameValuePair(TAG_SPEEDKPH, ""+speedpref)); // how can I get the value of edittext of the speed from the sharedpreferences? 

     JSONObject json = jParser.makeHttpRequest(url, "GET", params); 
     try { 
      int success = json.getInt(TAG_SUCCESS); 
      if (success == 1) { 
       cars = json.getJSONArray(TAG_CARS); 
       carsNumber = cars.length(); 
      } else 
       carsNumber = 0; 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 

    return null; 
} 
protected void onPostExecute(String file_url) { 
    if(carsNumber != 0){ 
     notification(carsNumber, speedpref); 
    } 
}}} 
+1

poster votre code ... – SMR

+0

@SMR Je l'ai déjà fait mais pas ansewrs http://stackoverflow.com/questions/23539400/use-alarmmanager-and-service-to-get-data-from-server-repeatedly – Abdel

+0

il suffit de poster votre code uniquement lié à Alarm Manager et service ici. – SMR

Répondre

1

parce que la Service étant répétée par AlramManager il est important d'arrêter le Service lorsque la tâche est terminée.

MyService.java

public class MyService extends Service { 
    @Override 
    public void onStart(Intent intent, int startId) { 
     //do your task here 

     stopSelf(); // important to stop service here 
    } 

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

vous savez déjà comment commencer à utiliser gestionnaire d'alarme service.

+0

merci pour votre réponse, alors est-ce le meilleur moyen d'obtenir des données périodiquement? et est-ce la bonne façon de commencer le service? dernière question s'il vous plaît, quand je vérifie dans la base de données, je ne veux pas vérifier dans les données que j'ai déjà vérifié une idée s'il vous plaît. – Abdel

+0

'quand je vérifie dans la base de données je ne veux pas vérifier dans les données que j'ai déjà vérifié une idée s'il vous plaît» je ne comprends pas votre point @AbdelkarimTinguer – SMR

Questions connexes