【问题标题】:How does one animate the Android sync status icon?如何为 Android 同步状态图标设置动画?
【发布时间】:2011-07-01 00:16:24
【问题描述】:

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

【问题讨论】:

    标签: android icons sync statusbar notificationmanager


    【解决方案1】:

    我找到了答案……

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

    【讨论】:

    • @Haraldo -- 很好。我找到了一个替换链接,希望它永远有效。
    【解决方案2】:

    要获得动画同步图标,您可以使用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();
    

    【讨论】:

      【解决方案3】:

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

      在 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);
      

      【讨论】:

      • 我喜欢你在这个解决方案上的清理风格。
      • @NicoGranelli -- NETWORK_ACTIVITY_ID 是我的唯一 ID,用于识别我的状态标志。您可以将其设置为任何值,因为它位于您的命名空间中。
      猜你喜欢
      • 2018-12-18
      • 2020-04-18
      • 1970-01-01
      • 2019-10-06
      • 2013-02-25
      • 1970-01-01
      • 1970-01-01
      • 2014-02-07
      相关资源
      最近更新 更多