如何制作 Android 同步状态图标的动画?

发布于 2024-10-18 02:59:40 字数 78 浏览 9 评论 0原文

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

I simply want to start and stop the sync icon that is in the status bar. I thought it would be a simple call using NotificationManager, but I cannot find the documentation or sample Q&A on the web or SO.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

つ可否回来 2024-10-25 02:59:40

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

To get an animated sync icon you can use android.R.drawable.ic_popup_sync icon. For instance, using the more recent notification builder, you'd use something like:

Notification notification = new NotificationCompat.Builder(mContext)
        .setContentTitle("my-title")
        .setContentText("Loading...")
        .setSmallIcon(android.R.drawable.ic_popup_sync)
        .setWhen(System.currentTimeMillis())
        .setOngoing(true)
.build();
妄断弥空 2024-10-25 02:59:40

我找到了答案...

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

I found my answer ...

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

This shows how to set and cancel the stat_notify_sync icon.

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);
}
静若繁花 2024-10-25 02:59:40

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

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

Thanks for your example, it saved me some time. I created a static method in my Application so I can easily turn the icon on/off from anywhere in my code. I still can't get it to animate though.

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

And then from anywhere in my application:

MyApplication.setNetworkIndicator(true);

MyApplication.setNetworkIndicator(false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文