Aller au contenu

Notification "En cours"


grofs

Recommended Posts

Posté(e)

Bonjour j'ai cherché un peu partout sur internet, mais je n'ai pas trouvé de tutorial pour mettre sa notification dans "En cours", si quelqu'un sait ?

Merci :)

Posté(e)

Code brut, je te laisse faire joujou avec et l'adapter:

    int icon = R.drawable.icon_grey;
   CharSequence tickerText = "TaskOS is now running!";
   //long when = System.currentTimeMillis();

   Notification notification = new Notification(icon, tickerText, 0);
   notification.flags |= Notification.FLAG_ONGOING_EVENT;

   //Context context = getApplicationContext();
   CharSequence mycontentTitle = "TaskOS";
   CharSequence mycontentText = "Click to open";
   Intent notificationIntent = new Intent(context,TaskOS.class);
   notificationIntent.setAction(Intent.ACTION_MAIN);
   notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

   notification.setLatestEventInfo(context, mycontentTitle, mycontentText, contentIntent);

   mNotificationManager.notify(TASKOS_ID, notification);

Posté(e)

Pour le moment le code est exact sauf :

mNotificationManager.notify(TASKOS_ID, notification);

En particulier "TASKOS_ID, J'ai deviné que TaskOs était ton appli ;) mais ce que je comprend pas c'est ce que je doit mettre à la place de cet id ?

Posté(e)

Rectification, il y a une erreur quand je lance le service dans le quel il y a la notification :

Le log :

05-23 09:56:36.189: ERROR/AndroidRuntime(2713): Uncaught handler: thread main exiting due to uncaught exception
05-23 09:56:36.244: ERROR/AndroidRuntime(2713): java.lang.RuntimeException: Unable to create service com.grofs.school.Encours: java.lang.NullPointerException
05-23 09:56:36.244: ERROR/AndroidRuntime(2713):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2790)
05-23 09:56:36.244: ERROR/AndroidRuntime(2713):     at android.app.ActivityThread.access$3200(ActivityThread.java:119)
05-23 09:56:36.244: ERROR/AndroidRuntime(2713):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1917)
05-23 09:56:36.244: ERROR/AndroidRuntime(2713):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 09:56:36.244: ERROR/AndroidRuntime(2713):     at android.os.Looper.loop(Looper.java:123)
05-23 09:56:36.244: ERROR/AndroidRuntime(2713):     at android.app.ActivityThread.main(ActivityThread.java:4363)
05-23 09:56:36.244: ERROR/AndroidRuntime(2713):     at java.lang.reflect.Method.invokeNative(Native Method)
05-23 09:56:36.244: ERROR/AndroidRuntime(2713):     at java.lang.reflect.Method.invoke(Method.java:521)
05-23 09:56:36.244: ERROR/AndroidRuntime(2713):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-23 09:56:36.244: ERROR/AndroidRuntime(2713):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-23 09:56:36.244: ERROR/AndroidRuntime(2713):     at dalvik.system.NativeStart.main(Native Method)
05-23 09:56:36.244: ERROR/AndroidRuntime(2713): Caused by: java.lang.NullPointerException
05-23 09:56:36.244: ERROR/AndroidRuntime(2713):     at com.grofs.school.Encours.onCreate(Encours.java:80)
05-23 09:56:36.244: ERROR/AndroidRuntime(2713):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2780)
05-23 09:56:36.244: ERROR/AndroidRuntime(2713):     ... 10 more

Et le service lui même :

TextView text;

   public static final String EVENT_ACTION = "Texto service";
   private final IBinder binder = new MyBinder();
   private Encours serviceBinder;
   boolean   started;

   private NotificationManager mNotificationManager;
    Notification notification;
   Vibrator vibrator;

   public class MyBinder extends Binder{
       Encours getService(){
           return Encours.this;
       }


   }



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

   @Override
   public void onCreate() {
         int icon = R.drawable.c_off;
           CharSequence tickerText = "Test";
           long when = System.currentTimeMillis();

           Notification notification = new Notification(icon, tickerText, 0);
           //notification.flags |= Notification.FLAG_ONGOING_EVENT;

           Context context = getApplicationContext();
           CharSequence mycontentTitle = "Test";
           CharSequence mycontentText = "Test";
           Intent notificationIntent = new Intent(context,Cours.class);
           notificationIntent.setAction(Intent.ACTION_MAIN);
           notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
           PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

           notification.setLatestEventInfo(context, mycontentTitle, mycontentText, contentIntent);

           mNotificationManager.notify(1,notification);
           super.onCreate(); 

Voila

Posté(e)

C'est à dire ?

Si tu veux le code entier le voila

package com.grofs.school;



import javax.security.auth.Destroyable;





import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Vibrator;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class Encours extends Service {



   TextView text;

   public static final String EVENT_ACTION = "Texto service";
   private final IBinder binder = new MyBinder();
   private Encours serviceBinder;
   boolean   started;

   private NotificationManager mNotificationManager;
    Notification notification;
   Vibrator vibrator;

   public class MyBinder extends Binder{
       Encours getService(){
           return Encours.this;
       }


   }



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

   @Override
   public void onCreate() {
         int icon = R.drawable.c_off;
           CharSequence tickerText = "Test";
           long when = System.currentTimeMillis();

           Notification notification = new Notification(icon, tickerText, 0);
           //notification.flags |= Notification.FLAG_ONGOING_EVENT;

           Context context = getApplicationContext();
           CharSequence mycontentTitle = "Test";
           CharSequence mycontentText = "Test";
           Intent notificationIntent = new Intent(context,Cours.class);
           notificationIntent.setAction(Intent.ACTION_MAIN);
           notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
           PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

           notification.setLatestEventInfo(context, mycontentTitle, mycontentText, contentIntent);

           mNotificationManager.notify(1,notification);
           super.onCreate();



   }



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

   @Override
   public void onStart(Intent intent, int startId) {

       super.onStart(intent, startId);
   }

   public class Receiver extends BroadcastReceiver{

       @Override
   public void onReceive(Context context, Intent intent) {

       }
       }
}

Posté(e)

Ecoute, il suffit de regarder le code du logcat: il dit:

05-23 09:56:36.244: ERROR/AndroidRuntime(2713): Caused by: java.lang.NullPointerException
05-23 09:56:36.244: ERROR/AndroidRuntime(2713):     at com.grofs.school.Encours.onCreate(Encours.java:80)

Ce qui veut dire que tu as une erreur à la ligne 80 de EnCours.java. Si tu montrais cette ligne à tout le monde, tu ferais gagner un temps précieux aux gens qui t'aident. J'ai donc passé mon temps à regarder et c'est cette ligne:

mNotificationManager.notify(1,notification);

mNotificationManager est donc null.

Il convient donc de faire en sorte qu'il ne soit plus null... :p

NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

Archivé

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

×
×
  • Créer...