7

我只想启动和停止状态栏中的同步图标。我认为这将是一个使用 NotificationManager 的简单调用,但我在 Web 或 SO 上找不到文档或示例问答。

4

3 回答 3

6

我找到了我的答案...

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

这显示了如何设置和取消 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);
}
于 2011-03-23T08:28:04.440 回答
6

要获得动画同步图标,您可以使用android.R.drawable.ic_popup_syncicon。例如,使用更新的通知生成器,您将使用如下内容:

Notification notification = new NotificationCompat.Builder(mContext)
        .setContentTitle("my-title")
        .setContentText("Loading...")
        .setSmallIcon(android.R.drawable.ic_popup_sync)
        .setWhen(System.currentTimeMillis())
        .setOngoing(true)
.build();
于 2014-01-13T20:50:46.180 回答
4

感谢您的示例,它为我节省了一些时间。我在我的应用程序中创建了一个静态方法,因此我可以轻松地从代码中的任何位置打开/关闭图标。我仍然无法让它动画化。

在 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);
于 2011-04-03T09:50:11.193 回答