0

Dans mon application, je vais utiliser ce code pour utiliser la notification:Android: Est-il possible de définir la notification à notifier sur le temps de désir? et même si le projet ne fonctionne pas?

private void addDefaultNotification(){ 
    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 

    int icon = R.drawable.icon; 
    CharSequence text = "Notification Text"; 
    CharSequence contentTitle = "Notification Title"; 
    CharSequence contentText = "Sample notification text."; 
    long when = System.currentTimeMillis(); 

    Intent intent = new Intent(this, NotificationViewer.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0); 
    Notification notification = new Notification(icon,text,when); 

    long[] vibrate = {0,100,200,300}; 
    notification.vibrate = vibrate; 

    notification.ledARGB = Color.RED; 
    notification.ledOffMS = 300; 
    notification.ledOnMS = 300; 

    notification.defaults |= Notification.DEFAULT_LIGHTS; 
    //notification.flags |= Notification.FLAG_SHOW_LIGHTS; 

    notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent); 

    notificationManager.notify(com.example.NotifivcationSample.Constants.NOTIFICATION_ID, notification); 
} 

RightNow Je reçois le notofication. En appelant cette fonction. Mais je veux, c'est qu'il devrait être informer l'utilisateur, même si l'application ne fonctionne pas à ce moment-là sur l'appareil et la notification devrait être notifier à l'heure désirée.

Est-ce possible? Si oui, alors aidez-moi s'il vous plaît pour cela. Merci.

Modifié:

public class AlarmNotificationReceiver extends BroadcastReceiver{ 
    //private Intent intent; 
    private NotificationManager notificationManager; 
    private Notification notification; 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 

     long value1 = intent.getLongExtra("param1", 0);  
     String value2 = intent.getStringExtra("param2"); 

     addTwoMonthNotification(); 

    } 
} 

je l'ai fait comme ça, mais pas en mesure de créer une notification dans cette classe de récepteur. Pourquoi ? et que devrais-je faire?

Répondre

2

Oui, il est possible.

Vous devez enregistrer votre intention dans AlarmManager et faire en sorte que la classe du récepteur de notification attende la notification d'AlarmManager au moment de l'exécution.

Fondamentalement, vous aurez besoin de:

  1. classe notification intention (sous-classe d'intention)

    public class MyNotificationIntent extends Intent { 
        // basic implementation ... 
    } 
    
  2. classe NotificationReceiver, sous-classe de BroadcastReceiver.Où vous recevez une notification de AlarmManger et que vous devez exécuter votre code pour afficher une notification (vous avez déjà ce genre de choses)

    public class AlarmNotificationReceiver extends BroadcastReceiver{ 
    
        @Override 
        public void onReceive(Context context, Intent intent){ 
    
         long value1 = intent.getLongExtra("param1", 0); 
         String value2 = intent.getStringExtra("param2"); 
    
         // code to show the notification 
         ... 
         notificationManager.notify(...); 
        } 
    } 
    
  3. fonction utilitaire pour enregistrer votre Notification AlarmManager

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    Intent intent = new MyNotificationIntent("com.alex.intent.action.ALARM_NOTIFICATION", 
        Uri.parse("notification_alarm_id"), context, AlarmNotificationReceiver.class); 
    intent.putExtra("param1", value1); 
    intent.putExtra("param2", value2); 
    
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    alarmManager.set(AlarmManager.RTC_WAKEUP, timeToRun, pendingIntent); 
    
+0

Ok Merci pour la réponse. Laissez-moi essayer. –

+0

Le troisième est la fonction d'utilité? Et si c'est alors où je dois l'appeler? Supposons que si j'utilise toogleButton pour activer/désactiver l'alerm alors devrais-je appeler cette fonction sur "on" à droite? –

+0

Oui, vous devriez l'appeler où vous voulez programmer l'alarme. –

1

http://android-er.blogspot.com/2010/10/simple-example-of-alarm-service-using.html

classe AlarmManager permet d'accéder aux services d'alarme du système. Ceux-ci vous permettent de programmer votre application pour qu'elle soit exécutée à un moment donné dans le futur. Lorsqu'une alarme se déclenche, l'intention qui a été enregistrée pour elle est diffusée par le système, démarrant automatiquement l'application cible si elle n'est pas déjà en cours d'exécution.

vous pouvez aussi lire celui-ci: http://android.arnodenhond.com/tutorials/alarm-notification

Vous devriez être en mesure d'informer votre utilisateur à la fois souhaitée sans problème

AJOUTÉE

Ou vous pouvez faire:

new Handler().postDelayed(new Runnable() { public void run() { 
      //Shoot your notification here 
     } 
    }, 1000 * 60 * 5); 

** OU **

http://developer.android.com/resources/articles/timed-ui-updates.html

private Runnable mUpdateTimeTask = new Runnable() { 
    public void run() { 
     final long start = mStartTime; 
     long millis = //something; 
     int seconds = //something; 
     int minutes = //something; 
     seconds  =//something; 

     mHandler.postAtTime(this, 
       start + (((minutes * 60) + seconds + 1) * 1000)); 
    } 
}; 
+0

Est-ce que vous s'il vous plaît aider à l'utiliser pour mon code? –

+0

code direct ..... wow :), tout d'abord vous devriez comprendre le concept que le @ Profete162 a fourni ici et l'essayer au moins pour une fois. –

+0

J'ai ajouté du code pour un exemple de travail –

Questions connexes