Tôi chỉ muốn bắt đầu và dừng biểu tượng đồng bộ hóa trên thanh trạng thái. Tôi nghĩ rằng nó sẽ là một cuộc gọi đơn giản bằng cách sử dụng NotificationManager, nhưng tôi không thể tìm thấy tài liệu hoặc mẫu Q & A trên web hoặc SO.Làm cách nào để animate biểu tượng trạng thái đồng bộ hóa Android?
Trả lời
Tôi tìm thấy câu trả lời của tôi ...
Đ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);
}
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);
Tôi thích kiểu làm sạch của bạn về giải pháp này. – mobibob
NETWORK_ACTIVITY_ID là gì? – NicoGranelli
@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
Để 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
bị hỏng ... – HGPB
@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
liên kết bị hỏng ... – Arjun