草庐IT

Android - 正确更新通知进度条

coder 2023-11-20 原文

我仍然是 Android 的新手,我正在努力改进我的通知进度条以使其更流畅,而不是对我的 Pebble 进行一百万次更新并以“正确的方式”进行。这段代码工作得“很好”,就像我在使用它时一样,通知绘制并且进度条按预期完成。

当我将我的 Pebble watch 设置为接受我的应用程序的通知时,这对我来说成了一个问题。这会导致它根据上传速度的快慢,每张图片振动大约 50 次。

作为初学者,我认为我做的这一切都是错误的,并且有更好的方法来做我想做的事情。

我的通知的进度条使用以下代码更新:

private int upload_progress;
private Long time_previous_progress = Calendar.getInstance().getTimeInMillis();

protected void onProgressUpdate(Integer... progress) {
    Long time_now = Calendar.getInstance().getTimeInMillis();
    if(
       ((time_now - time_previous_progress) < 55) // 55ms minimum delay
       || (progress[0] < 0 && progress[0] > 100)  // progress >0 && <100
       || progress[0].equals(upload_progress)     // progress changed
       || ! App.getStatus()                       // watcher is running
       )
    {
        return;
    }
    time_previous_progress = time_now;
    upload_progress = progress[0];
    int upload_counter = getUploadCounter();
    int upload_total = db.getReadyImagesCount();
    NotificationHandler.notify(context, upload_progress, upload_counter, (upload_total + upload_counter));
}

然后使用以下代码生成通知:

public static int notify(Context context, Integer progress, Integer upload_count, Integer upload_total)
{
    Bitmap bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
    String notif_title = context.getResources().getString(R.string.instant_upload_title);

    String notif_progress = context.getResources().getString(R.string.instant_upload_progress);
    String notif_ticker = String.format(notif_progress, upload_count, upload_total);
    String notif_msg = String.format(notif_progress, upload_count, upload_total);

    Intent intent_swipe = new Intent(context, NotifyReceiver.class);
    intent_swipe.setAction("notification_cancelled");
    PendingIntent pendingIntent_swipe = PendingIntent.getBroadcast(context, 0, intent_swipe, PendingIntent.FLAG_CANCEL_CURRENT);

    Intent intent_click = new Intent(context, Settings.class);
    intent_click.putExtra("notification_clicked", true);
    PendingIntent pendingIntent_click = PendingIntent.getActivity(context, 0, intent_click, PendingIntent.FLAG_CANCEL_CURRENT);

    int pro_max = 100;
    int pro_cur = 0;
    if(progress < 100)
    {
        pro_cur = progress;
    }else{
        pro_cur = pro_max = 0;
    }

    //new NotificationCompat.Builder(context)
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context) //PixelRelayApplication.getNotificationBuilder()
        .setTicker(notif_ticker)
        .setContentTitle(notif_title)
        .setContentText(notif_msg)
        .setNumber(upload_count)
        .setSmallIcon(R.drawable.ic_launcher)
        .setLargeIcon(bm)
        .setContentIntent(pendingIntent_click)
        .setDeleteIntent(pendingIntent_swipe)
        .setAutoCancel(true)
        .setOngoing(true)
        .setWhen(Calendar.getInstance().getTimeInMillis())
        .setProgress(pro_max, pro_cur, false);

    Notification notification = builder.build();

    // Put the auto cancel notification flag
    notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE;
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFY_ME_ID, notification);
    return NOTIFY_ME_ID;
}

最佳答案

您可以使用可以正常工作的开源代码来实现您的想法,通过 Github 访问它:Progress Watch - A progress bar watchface for pebble .如果您决定重用代码,我的建议是:

  • 给予适当的信任
  • 如果您仍然发现可以改进的地方,请通知作者
  • 如果你添加了一些新功能,创建一个分支并提交补丁

关于Android - 正确更新通知进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20711015/

有关Android - 正确更新通知进度条的更多相关文章

  1. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  2. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  3. ruby-on-rails - 如何使用 instance_variable_set 正确设置实例变量? - 2

    我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击

  4. ruby-on-rails - 使用 rails 4 设计而不更新用户 - 2

    我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它​​不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数

  5. ruby-on-rails - 正确的 Rails 2.1 做事方式 - 2

    question的一些答案关于redirect_to让我想到了其他一些问题。基本上,我正在使用Rails2.1编写博客应用程序。我一直在尝试自己完成大部分工作(因为我对Rails有所了解),但在需要时会引用Internet上的教程和引用资料。我设法让一个简单的博客正常运行,然后我尝试添加评论。靠我自己,我设法让它进入了可以从script/console添加评论的阶段,但我无法让表单正常工作。我遵循的其中一个教程建议在帖子Controller中创建一个“评论”操作,以添加评论。我的问题是:这是“标准”方式吗?我的另一个问题的答案之一似乎暗示应该有一个CommentsController参

  6. ruby - 我可以将我的 README.textile 以正确的格式放入我的 RDoc 中吗? - 2

    我喜欢使用Textile或Markdown为我的项目编写自述文件,但是当我生成RDoc时,自述文件被解释为RDoc并且看起来非常糟糕。有没有办法让RDoc通过RedCloth或BlueCloth而不是它自己的格式化程序运行文件?它可以配置为自动检测文件后缀的格式吗?(例如README.textile通过RedCloth运行,但README.mdown通过BlueCloth运行) 最佳答案 使用YARD直接代替RDoc将允许您包含Textile或Markdown文件,只要它们的文件后缀是合理的。我经常使用类似于以下Rake任务的东西:

  7. ruby-on-rails - 使用 config.threadsafe 时从 lib/加载模块/类的正确方法是什么!选项? - 2

    我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co

  8. ruby - 调用其他方法的 TDD 方法的正确方法 - 2

    我需要一些关于TDD概念的帮助。假设我有以下代码defexecute(command)casecommandwhen"c"create_new_characterwhen"i"display_inventoryendenddefcreate_new_character#dostufftocreatenewcharacterenddefdisplay_inventory#dostufftodisplayinventoryend现在我不确定要为什么编写单元测试。如果我为execute方法编写单元测试,那不是几乎涵盖了我对create_new_character和display_invent

  9. ruby-on-rails - 有没有办法为 CarrierWave/Fog 设置上传进度指示器? - 2

    我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r

  10. ruby-on-rails - 如何在发布新的 Ruby 或 Rails 版本时收到通知? - 2

    有人知道在发布新版本的Ruby和Rails时收到电子邮件的方法吗?他们有邮件列表,RubyonRails有一个推特,但我不想听到那些随之而来的喧嚣,我只想知道什么时候发布新版本,尤其是那些有安全修复的版本。 最佳答案 从therailsblog获取提要.http://weblog.rubyonrails.org/feed/atom.xml 关于ruby-on-rails-如何在发布新的Ruby或Rails版本时收到通知?,我们在StackOverflow上找到一个类似的问题:

随机推荐