2011-07-20 27 views
7

Estoy intentando iniciar una notificación de barra de estado desde un receptor de transmisión y luego detenerla desde otro receptor de transmisión, pero estoy teniendo problemas. Me gustaría iniciar la notificación en la barra de estado cuando el usb está conectado y luego cuando el usb está desconectado, me gustaría detenerlo. Tengo los dos receptores configurados y trabajando simplemente luchando con iniciar y detener uno desde un receptor aquí está el código. Tengo actualmenteIniciar y detener una notificación del receptor de difusión

Mi único error con mi código es la línea myNotificationManager = (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE);, el error solo dice que getSystemService no está definido y quiere hacer que el método suponga que el receptor no tiene soporte para ese método, como lo haría una actividad así que lo que hay que hacer para crear y detener una notificación por parte de los receptores gracias por cualquier ayuda

public class USBConnect extends BroadcastReceiver { 

public NotificationManager myNotificationManager; 
public static final int NOTIFICATION_ID = 1; 

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

    myNotificationManager = (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE); 

     CharSequence NotificationTicket = "USB Connected!"; 
     CharSequence NotificationTitle = "USB Connected!"; 
     CharSequence NotificationContent = "USB is Connected!"; 

     Notification notification = new Notification(R.drawable.usbicon, NotificationTicket, 0); 
     Intent notificationIntent = new Intent(context, MyClass.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
     notification.setLatestEventInfo(context, NotificationTitle, NotificationContent, contentIntent); 
     notification.flags |= Notification.FLAG_ONGOING_EVENT; 
     myNotificationManager.notify(NOTIFICATION_ID, notification); 
     } 
} 

y entonces el rec Eiver para cuando se desconecta esto creo que está muy bien y debería funcionar Creo que mi problema es sólo en la clase USBConnect

public class USBDisCon extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.cancel(USBConnect.NOTIFICATION_ID); 
    } 
} 

Respuesta

9

Bueno terminé pensando que a mí mismo para referencia futura para personas con todo lo que tenía que hacer mi mismo problema fue agregar contexto en el frente de getSystemService aquí está mi código que trabajó

public class USBConnect extends BroadcastReceiver { 

public NotificationManager myNotificationManager; 
public static final int NOTIFICATION_ID = 1; 

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

    myNotificationManager = (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE); 

     CharSequence NotificationTicket = "USB Connected!"; 
     CharSequence NotificationTitle = "USB Connected!"; 
     CharSequence NotificationContent = "USB is Connected!"; 

     Notification notification = new Notification(R.drawable.usbicon, NotificationTicket, 0); 
     Intent notificationIntent = new Intent(context, MyClass.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
     notification.setLatestEventInfo(context, NotificationTitle, NotificationContent, contentIntent); 
     notification.flags |= Notification.FLAG_ONGOING_EVENT; 
     myNotificationManager.notify(NOTIFICATION_ID, notification); 
     } 
} 

Y entonces el receptor para cuando se desconecta esto creo que está muy bien y debería funcionar creo que mi problema es sólo en la clase USBConnect

public class USBDisCon extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.cancel(USBConnect.NOTIFICATION_ID); 
    } 
} 
0

getSystemService indefinido significa más probable es que no ha importado android.app.Activity (getSystemService se define en android.app.Activity).

+0

Lo he importado en realidad y sigo teniendo que es indefinido y quiere crear el método – user577732

+0

¿Nadie tiene alguna idea? Realmente apreciaría algo de ayuda – user577732

3

me encontré con t su problema yo mismo. Creo que el usuario olvidó actualizar el código en la respuesta. context debe estar en DOS VECES! ¡Lo leí mal la primera vez!

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

Espero que esto ayude a alguien confundido similar!

0

Esta es la manera de utilizar la notificación de broadcastReceivers:

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

alarmas Programa de BroadcastReceivers:

AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 

Tienes que utilizar el prefijo de contexto siempre!

Cuestiones relacionadas