2011-02-21 55 views

Trả lời

5

Tôi tìm thấy câu trả lời của tôi ...

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

Điều này cho thấy làm thế nào để thiết lập và hủy bỏ các biểu tượng 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

bị hỏng ... – HGPB

+0

@Haraldo - bắt tốt. Tôi tìm thấy một liên kết thay thế, hy vọng, nó sẽ có hiệu lực mãi mãi. – mobibob

+0

liên kết bị hỏng ... – Arjun

4

Cảm ơn ví dụ của bạn, nó đã giúp tôi tiết kiệm thời gian. Tôi tạo ra một phương pháp tĩnh trong ứng dụng của tôi vì vậy tôi có thể dễ dàng bật/tắt biểu tượng từ bất cứ nơi nào trong mã của tôi. Tôi vẫn không thể làm cho nó hoạt hình.

Trong 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); 
} 

Và sau đó từ bất cứ nơi nào trong ứng dụng của tôi:

MyApplication.setNetworkIndicator(true); 

MyApplication.setNetworkIndicator(false); 
+0

Tôi thích kiểu làm sạch của bạn về giải pháp này. – mobibob

+0

NETWORK_ACTIVITY_ID là gì? – NicoGranelli

+0

@NicoGranelli - NETWORK_ACTIVITY_ID là ID duy nhất của tôi để xác định các cờ trạng thái của tôi. Bạn có thể đặt nó thành bất kỳ giá trị nào vì nó nằm trong không gian tên của bạn. – mobibob

5

Để có được một biểu tượng đồng bộ hoạt hình bạn có thể sử dụng android.R.drawable.ic_popup_sync biểu tượng. Ví dụ: sử dụng trình tạo thông báo gần đây hơn, bạn sẽ sử dụng thông tin như:

Notification notification = new NotificationCompat.Builder(mContext) 
     .setContentTitle("my-title") 
     .setContentText("Loading...") 
     .setSmallIcon(android.R.drawable.ic_popup_sync) 
     .setWhen(System.currentTimeMillis()) 
     .setOngoing(true) 
.build(); 
Liên kết
Các vấn đề liên quan