如何设置Android同步状态图标的动画?
我只想启动和停止状态栏中的同步图标。我原以为使用NotificationManager只需一个简单的电话,但我在网上找不到文档或示例问答。我找到了答案 这显示了如何设置和取消stat\u notify\u sync图标如何设置Android同步状态图标的动画?,android,icons,sync,statusbar,notificationmanager,Android,Icons,Sync,Statusbar,Notificationmanager,我只想启动和停止状态栏中的同步图标。我原以为使用NotificationManager只需一个简单的电话,但我在网上找不到文档或示例问答。我找到了答案 这显示了如何设置和取消stat\u notify\u sync图标 private void showNotification(String authority) { Object service = getSystemService(NOTIFICATION_SERVICE); NotificationManager noti
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);
}
谢谢你的例子,它节省了我一些时间。我在我的应用程序中创建了一个静态方法,这样我就可以在代码中的任何地方轻松地打开/关闭图标。但我还是无法让它动画化 在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);
}
然后从我的应用程序中的任何位置:
MyApplication.setNetworkIndicator(true);
MyApplication.setNetworkIndicator(false);
要获得动画同步图标,可以使用android.R.drawable.ic\u popup\u sync图标。例如,使用较新的notification builder,您可以使用以下内容:
Notification notification = new NotificationCompat.Builder(mContext)
.setContentTitle("my-title")
.setContentText("Loading...")
.setSmallIcon(android.R.drawable.ic_popup_sync)
.setWhen(System.currentTimeMillis())
.setOngoing(true)
.build();
我喜欢您在这个解决方案上的清理风格。@NicoGranelli--NETWORK_ACTIVITY_ID是标识我的状态标志的唯一ID。您可以将其设置为任何值,因为它位于您的命名空间中。@Haraldo--很好。我找到了一个替换链接,希望它永远有效。