2011-02-21 7 views

Respuesta

5

encontré mi respuesta ...

http://libs-for-android.googlecode.com/svn-history/r46/trunk/src/com/google/android/accounts/AbstractSyncService.java

Esto muestra cómo configurar y cancelar el icono stat_notify_sync.

private void showNotification(String authority) { 
    Object service = getSystemService(NOTIFICATION_SERVICE); 
    NotificationManager notificationManager = (NotificationManager) service; 
    int icon = android.R.drawable.stat_notify_sync; 
    String tickerText = null; 
    long when = 0; 
    Notification notification = new Notification(icon, tickerText, when); 
    Context context = this; 
    CharSequence contentTitle = "mobi"; //createNotificationTitle(); 
    CharSequence contentText = "bob"; //createNotificationText(); 
    PendingIntent contentIntent = createNotificationIntent(); 
    notification.when = System.currentTimeMillis(); 
    notification.flags |= Notification.FLAG_ONGOING_EVENT; 
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
    notificationManager.notify(mNotificationId, notification); 
} 

private void cancelNotification() { 
    Object service = getSystemService(NOTIFICATION_SERVICE); 
    NotificationManager nm = (NotificationManager) service; 
    nm.cancel(mNotificationId); 
} 
+1

enlace está roto ... – HGPB

+0

@Haraldo - buena captura. Encontré un enlace de reemplazo, con suerte, será válido para siempre. – mobibob

+0

enlace está roto ... – Arjun

4

Gracias por su ejemplo, me ha ahorrado algo de tiempo. Creé un método estático en mi aplicación para poder activar/desactivar el ícono fácilmente desde cualquier parte de mi código. Sin embargo, todavía no puedo hacerlo animar.

En MyApplication.java:

private static Context context; 
private static NotificationManager nm; 

public void onCreate(){ 
     context = getApplicationContext(); 
     nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
... 
} 

public static void setNetworkIndicator(boolean state) {  
    if (state == false) { 
     nm.cancel(NETWORK_ACTIVITY_ID); 
     return; 
    } 

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT); 
    Notification n = new Notification(android.R.drawable.stat_notify_sync, null, System.currentTimeMillis()); 
    n.setLatestEventInfo(context, "SMR7", "Network Communication", contentIntent); 
    n.flags |= Notification.FLAG_ONGOING_EVENT; 
    n.flags |= Notification.FLAG_NO_CLEAR; 
    nm.notify(NETWORK_ACTIVITY_ID, n); 
} 

Y entonces desde cualquier lugar en mi solicitud:

MyApplication.setNetworkIndicator(true); 

MyApplication.setNetworkIndicator(false); 
+0

Me gusta su estilo de limpieza en esta solución. – mobibob

+0

¿Qué es NETWORK_ACTIVITY_ID? – NicoGranelli

+0

@NicoGranelli - NETWORK_ACTIVITY_ID es mi identificación única para identificar mis banderas de estado. Puede establecerlo en cualquier valor ya que está en su espacio de nombres. – mobibob

5

Para obtener un icono de sincronización de animación se puede utilizar android.R.drawable.ic_popup_sync icono. Por ejemplo, utilizando la más reciente constructor de notificación, que tendría que utilizar algo como:

Notification notification = new NotificationCompat.Builder(mContext) 
     .setContentTitle("my-title") 
     .setContentText("Loading...") 
     .setSmallIcon(android.R.drawable.ic_popup_sync) 
     .setWhen(System.currentTimeMillis()) 
     .setOngoing(true) 
.build(); 
Cuestiones relacionadas