有没有办法在使用GCM接收推送通知时调试应用程序?

Sho*_*uri 10 java eclipse android google-cloud-messaging

最初我的应用程序已关闭,当我收到来自GCM推送服务的新消息时,我正在我的类中的onMessage()开始一个新的活动GCMIntentService.我想使用调试器来检查一些错误.我的问题是,从推送通知启动时有没有办法调试应用程序?

为了清楚一点,我有一个BroadcastReceiver附加服务.即使应用程序已关闭,它也会收到广播并根据收到的消息启动活动.应用程序可能会在未打开时收到广播.现在,当应用程序打开并且我收到推送消息时,我可以调试它.但问题是,当应用程序关闭并接收广播时,如何调试它?

Wex*_*Wex 9

@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
    /*
      the following line will halt the app here
      to give you time to attach the debugger
    */
    android.os.Debug.waitForDebugger();

    /*
      then you can put a breakpoint on any line
      after that, like here:
    */
    Log.d("onMessageReceived", "From: " + remoteMessage.getFrom());

    ...
}
Run Code Online (Sandbox Code Playgroud)

您可以将该行android.os.Debug.waitForDebugger();放在您要停止执行并附加调试器的代码中的任何位置。只需记住在完成调试后将其删除。

  • 2020 年 7 月仍然完美运行!为了安全起见,我建议始终将其包装在“if (BuildConfig.DEBUG)”中。 (5认同)