代码之家  ›  专栏  ›  技术社区  ›  gone

Android广播接收器和应用程序运行状态

  •  0
  • gone  · 技术社区  · 9 年前

    我在应用程序中使用了第三方推送服务,当从服务器推送消息时,它的后台服务会发送一个我必须收听的广播,这个过程很正常。在函数中 onReceive() ,我可以显示通知或只是默默地做一些工作。是否显示通知取决于应用程序状态,即:

    if(application_is_running){
        if(message_type == chat_message){
            if(chat_activity_is_present){
                /** notification not allowed **/
            }else{
                /** show notification, open the corresponding chat activity on clicking the notification */
            }
        }else{
            /** store the message silently **/
        }
    }else{
        /** show notification, and start the app on clicking the notification**/
    }
    

    当广播接收器从服务器收到推送消息时,通知我的应用程序的最佳做法是什么?

    --编辑--

    通过最佳实践,我指的是将消息发送到应用程序处理的最佳方式(不改变应用程序中的消息处理逻辑)。

    1 回复  |  直到 9 年前
        1
  •  0
  •   KISHORE_ZE    9 年前

    这可能会有所帮助。

    1. 使用服务:这是推荐的,但我不太喜欢。
    2. 意图:你可以启动你的应用程序并设置标志,比如单陀螺,它将检查你的应用是否在后台运行,如果是的话,它只会把它带到应用程序中。如果你需要默默地做这件事,你可以开始一个单独的活动,它基本上显示一个透明的袋子,在启动时它处理意图,然后自己关闭。但当然,我的服务会让用户体验更好,但我在服务方面没有太多知识。