【发布时间】:2011-07-01 00:16:24
【问题描述】:
我只想启动和停止状态栏中的同步图标。我认为这将是一个使用 NotificationManager 的简单调用,但我在网络或 SO 上找不到文档或示例问答。
【问题讨论】:
标签: android icons sync statusbar notificationmanager
我只想启动和停止状态栏中的同步图标。我认为这将是一个使用 NotificationManager 的简单调用,但我在网络或 SO 上找不到文档或示例问答。
【问题讨论】:
标签: android icons sync statusbar notificationmanager
我找到了答案……
这显示了如何设置和取消 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);
}
【讨论】:
要获得动画同步图标,您可以使用android.R.drawable.ic_popup_sync 图标。例如,使用更新的通知生成器,您可以使用如下内容:
Notification notification = new NotificationCompat.Builder(mContext)
.setContentTitle("my-title")
.setContentText("Loading...")
.setSmallIcon(android.R.drawable.ic_popup_sync)
.setWhen(System.currentTimeMillis())
.setOngoing(true)
.build();
【讨论】:
感谢您的示例,它为我节省了一些时间。我在我的应用程序中创建了一个静态方法,因此我可以轻松地从代码中的任何位置打开/关闭图标。我仍然无法让它动画化。
在 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);
【讨论】: