2017-06-19 2 views
2

Je souhaite effectuer des notifications locales qui n'apparaissent que le 1er, le 2ème et le 3ème jour du mois, par exemple 11h. J'ai mis en place mes notifications pendant 3 jours consécutifs à 12h30, 14h30 et 18h30 mais cela n'a été montré qu'une fois, à 14h30. Quand j'ai vérifié le moniteur android pour cette heure il ressemble à androïde juste sauter ce sec ?! Voici mon code:La notification locale Android ne s'affichera parfois pas

ici est mon service qui est configuré dans l'activité principale onCreate

`@EService 
public class NotifiService extends Service { 
public int counter = 0; 

public NotifiService(Context applicationContext) { 
    super(); 
    Log.w("here!", "here i am"); 
} 
public NotifiService(){ 

} 




@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    super.onStartCommand(intent,flags,startId); 
    Log.i("-----b", "onStartCommand"); 

    startTimer(); 


    return Service.START_STICKY; //super.onStartCommand(intent, flags, startId); 

} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    Intent broadCastIntent = new Intent("com.Yyyyy.Xxxxx.Yyyyy.BootBro"); 
    sendBroadcast(broadCastIntent); 
    stopTimerTask(); 
} 

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

@Background 
public void checkDate(){ 
    Log.w("f", "if working"); 
    Calendar cal = Calendar.getInstance(); 
    int d = cal.get(Calendar.DAY_OF_MONTH); 
    int h = cal.get(Calendar.HOUR_OF_DAY); 
    int s = cal.get(Calendar.SECOND); 
    int m = cal.get(Calendar.MINUTE); 
    if(d==17 || d == 18 || d == 19){ 
     if (h == 12 || h == 14 || h == 16) { 

      if (m == 30) { 
       if (s == 30) { 


       Log.w("notify", "working>>>>"); 
        Intent intent = new Intent(getApplicationContext(), MainActivity_.class); 
        PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

        NotificationCompat.Builder b = new NotificationCompat.Builder(getApplicationContext()); 

        b.setAutoCancel(false) 
          .setDefaults(Notification.DEFAULT_ALL) 
          .setWhen(System.currentTimeMillis()) 
          .setSmallIcon(R.drawable.ic_app_icon) 
          .setTicker("FabReminder") 
          .setContentTitle("FabReminder") 
          .setContentText(getString(R.string.notification)) 
          .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND) 
          .setContentIntent(contentIntent); 
        //.setContentInfo("Tap this bar and select shop"); 


        NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); 
        notificationManager.notify(1, b.build()); 
       } 
      } 
     } 
    } 

} 
private Timer timer; 
private TimerTask timerTask; 
long oldTime=0; 
public void stopTimerTask(){ 
    if (timer != null){ 
     timer.cancel(); 
     timer = null; 
    } 
} 
public void initializeTimerTask(){ 
    timerTask = new TimerTask() { 
     @Override 
     public void run() { 
      checkDate(); 
     } 
    }; 
} 
public void startTimer(){ 
    timer = new Timer(); 
    initializeTimerTask(); 
    timer.schedule(timerTask,1000,1000); 
} 

@Override 
public void onTaskRemoved(Intent rootIntent) { 
    super.onTaskRemoved(rootIntent); 
    Log.w("killed task >>>"," task killed"); 
    Intent broadCastIntent = new Intent("com.Yyyyy.Xxxxx.Yyyyy.BootBro"); 
    sendBroadcast(broadCastIntent); 
}` 

Voici mon BroadcastReceiver:

`BootBro public class étend BroadcastReceiver { public int MID;

@Override 
public void onReceive(Context context, Intent intent) { 
    //context.startService(new Intent(context, NotifiService_.class)); 
    Log.w(BootBro.class.getSimpleName(), "Service stops!"); 
context.startService(new Intent(context, NotifiService_.class)); 


} 

} `

et AndroidManifest

`<service android:name="com.Yyyyy.Xxxx.Yyyyy.NotifiService_" android:enabled="true"/> 
    <receiver android:name="com.Yyyyy.Xxxx.Yyyy.BootBro" android:enabled="true" 
     android:exported="true" 
     android:label="RestartServiceWhenStopped"> 

     <intent-filter><action android:name="android.intent.action.BOOT_COMPLETED"> 

     </action> 
      <action android:name="com.Yyyyy.Xxxx.Yyyy.BootBro"/> 
     </intent-filter> 
    </receiver>` 
+0

Ça vous dérange de le faire de manière différente? – AndroidStorm

+0

Je pense que votre option sonne bien, merci beaucoup. Je vais le vérifier aujourd'hui :) – Gorthez

Répondre

1

Cette approche vous pouvez configurer facilement avec un AlarmManager.

Cette classe fournit services.These d'alarme accès au système vous permettent de programmer votre application à exécuter à un moment donné dans l'avenir ...More

Les alarmes ont ces caractéristiques:

  • Ils vous permettent de tirer des intentions à des moments et/ou des intervalles définis.
  • Vous pouvez les utiliser conjointement avec des récepteurs de diffusion pour démarrer des services et effectuer d'autres opérations.
  • Ils fonctionnent en dehors de votre application, vous pouvez donc les utiliser pour déclencher des événements ou des actions même lorsque votre application n'est pas en cours d'exécution et même si l'appareil lui-même est en veille.
  • Ils vous aident à minimiser les besoins en ressources de votre application. Vous pouvez programmer les opérations sans compter sur des minuteries ou en cours d'exécution des services d'arrière-plan

continue La documentation officielle d'Android ont un tutoriel que vous pouvez suivre here

Et un exemple ici aussi avec un simple app que vous pouvez configurer une alarme se réveiller.