Aller au contenu

Arrêter un service


marlon01

Recommended Posts

Posté(e)

Bonjour à tous,

J'ai mis en place, dans mon application, le lancement d'une notification à une date et une heure précise.

Il n'y a aucun problème quant à la notification, elle se lance bien à la date et l'heure prévue. En revanche, le service qui se lance à l'heure de la notification ne s'arrête pas automatiquement et fonctionne en continue en arrière plan après le lancement de la notification.

J'ai cherché à arrêter ce service automatiquement mais rien ne fonctionne et j'avoue que je commence à desespérer :(((

Je suis novice sur java et android alors peut être que je n'applique pas bien la méthode d'arrêt d'un service.

Voici mon code actuel :

AlarmReceiver.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AlarmReceiver extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {
 // When our Alaram time is triggered , this method will be excuted (onReceive)
 // We're invoking a service in this method which shows Notification to the User
  Intent myIntent = new Intent(context, NotificationService.class);
  context.startService(myIntent);
   }

}

NotificationService.java

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class NotificationService extends Service {

private NotificationManager mManager;

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

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

@Override
public void onstart(Intent intent, int startId) {
 super.onstart(intent, startId);
 // Getting Notification Service
 mManager = (NotificationManager) this.getApplicationContext()
   .getSystemService(
  this.getApplicationContext().NOTIFICATION_SERVICE);
 /*
  * When the user taps the notification we have to show the Home Screen
  * of our App, this job can be done with the help of the following
  * Intent.
  */
 Intent intent1 = new Intent(this.getApplicationContext(), MyView.class);

 Notification notification = new Notification(R.drawable.ic_launcher,
   "Message de la notification", System.currentTimeMillis());

 intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
   | Intent.FLAG_ACTIVITY_CLEAR_TOP);

 PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
   this.getApplicationContext(), 0, intent1,
   PendingIntent.FLAG_UPDATE_CURRENT);

 notification.flags |= Notification.FLAG_AUTO_CANCEL;

 notification.setLatestEventInfo(this.getApplicationContext(),
   "Message", "Message de la notification",
   pendingNotificationIntent);

 mManager.notify(0, notification);
}

@Override
public void onDestroy() {
 // TODO Auto-generated method stub
 super.onDestroy();
}

}

MyView.java

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;

public class MyView extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 Calendar Calendar_Object = Calendar.getInstance();
Date now = Calendar_Object.getTime();   
 Calendar_Object.set(Calendar.YEAR, 2013);
Calendar_Object.set(Calendar.MONTH, 4); 
Calendar_Object.set(Calendar.DAY_OF_MONTH, 8);

 Calendar_Object.set(Calendar.HOUR_OF_DAY, 12);
 Calendar_Object.set(Calendar.MINUTE, 02);
 Calendar_Object.set(Calendar.SECOND, 0);


 // MyView is my current Activity, and AlarmReceiver is the
 // BoradCastReceiver
 Intent myIntent = new Intent(MyView.this, AlarmReceiver.class);

 PendingIntent pendingIntent = PendingIntent.getBroadcast(MyView.this,
   0, myIntent, 0);

 AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

 /*
  * The following sets the Alarm in the specific time by getting the long
  * value of the alarm date time which is in calendar object by calling
  * the getTimeInMillis(). Since Alarm supports only long value , we're
  * using this method.
  */

 [color=#0000FF]if[/color] (Calendar_Object.getTime().after(now))
alarmManager.set(AlarmManager.RTC, Calendar_Object.getTimeInMillis(),pendingIntent);
}

Et enfin l'extrait de code de mon manifest

<receiver android:name=".AlarmReceiver" >
    </receiver>

    <service
	    android:name="NotificationService"
	    android:enabled="true" />

Merci par avance si vous avez une solution à me proposer!!!!!

Posté(e)

bonjour,

Vu le peu de traitement que tu fais , je ne mettrais pas en place la notion de Service que je réserve pour les traitements longs à exécuter en tâche de fond.

je mettrai la création de la notification directement dans le broadcastReceiver.

donc ton appli n'aurai qu'un réceiver et une activity.

Marc

  • 2 weeks later...

Archivé

Ce sujet est désormais archivé et ne peut plus recevoir de nouvelles réponses.

×
×
  • Créer...