【问题标题】:Android intent takes a long time to be receivedAndroid意图需要很长时间才能收到
【发布时间】:2012-05-16 23:02:27
【问题描述】:

在我的应用程序中,我正在发送更新 appWidget 的意图。问题是,从 logcat 我可以看到意图平均需要十秒钟才能被接收。

 05-16 18:12:54.070: DEBUG/PHCA_Variable(7580): Broadcast sent  
 05-16 18:12:54.080: INFO/ActivityManager(274): Starting: Intent { flg=0x10000000 cmp=com.skipmorrow.phca/.WidgetDialogResponseActivity (has extras) } from pid 7580    
 05-16 18:12:54.100: WARN/ActivityManager(274): Trying to launch com.skipmorrow.phca/.WidgetDialogResponseActivity  
 05-16 18:12:54.170: DEBUG/InputTransport(274): Input channel constructed: name='40832a98 com.skipmorrow.phca/com.skipmorrow.phca.WidgetDialogResponseActivity (server)', ashmemFd=299, receivePipeFd=311, sendPipeFd=310   
 05-16 18:12:54.170: DEBUG/InputTransport(274): Input channel constructed: name='40832a98 com.skipmorrow.phca/com.skipmorrow.phca.WidgetDialogResponseActivity (client)', ashmemFd=304, receivePipeFd=309, sendPipeFd=313   
 05-16 18:12:54.170: DEBUG/InputTransport(274): Input channel destroyed: name='40832a98 com.skipmorrow.phca/com.skipmorrow.phca.WidgetDialogResponseActivity (client)', ashmemFd=304, receivePipeFd=309, sendPipeFd=313 
 05-16 18:12:54.180: DEBUG/InputTransport(7580): Input channel constructed: name='40832a98 com.skipmorrow.phca/com.skipmorrow.phca.WidgetDialogResponseActivity (client)', ashmemFd=66, receivePipeFd=67, sendPipeFd=68 
 05-16 18:12:54.460: INFO/ActivityManager(274): Displayed com.skipmorrow.phca/.WidgetDialogResponseActivity: +362ms 
 05-16 18:12:56.140: INFO/InputDispatcher(274): Delivering key to current input target: action: 0, channel '40832a98 com.skipmorrow.phca/com.skipmorrow.phca.WidgetDialogResponseActivity (server)' 
 05-16 18:12:56.260: INFO/InputDispatcher(274): Delivering key to current input target: action: 1, channel '40832a98 com.skipmorrow.phca/com.skipmorrow.phca.WidgetDialogResponseActivity (server)' 
 05-16 18:12:56.390: DEBUG/InputTransport(7580): Input channel destroyed: name='40832a98 com.skipmorrow.phca/com.skipmorrow.phca.WidgetDialogResponseActivity (client)', ashmemFd=66, receivePipeFd=67, sendPipeFd=68   
 05-16 18:13:03.350: DEBUG/PHCA_PhcaAppWidgetProvider(7580): onReceive(); action = android.appwidget.action.APPWIDGET_UPDATE    

我看到的唯一看起来有点可疑的是活动正在开始的警告。为什么会有这样的警告?除此之外,一切似乎都很好。

发送意图的代码没什么特别的:

    Log.d(MY_DEBUG_TAG, "UpdateAppWidgets()");
    Intent i = new Intent();
    i.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    ComponentName phcaWidget = new ComponentName(ctx, PhcaAppWidgetProvider.class);
    i.putExtra("appWidgetIds", AppWidgetManager.getInstance(ctx).getAppWidgetIds(phcaWidget));

    ctx.sendBroadcast(i);
    Log.d(MY_DEBUG_TAG, "Broadcast sent");

appWidgetProvider 中的 onReceive 也没什么特别的。 Log 语句是第一个命令。

什么会导致意图需要十秒以上才能被接收?

跳过

【问题讨论】:

    标签: android android-intent appwidgetprovider


    【解决方案1】:

    如果您要向其发送 Intent 的 Activity 尚不存在,则必须启动它。 10 秒对于启动应用程序来说似乎很长,但并不疯狂。

    我的应用程序在调试模式下运行速度慢了 5 倍或更慢。如果您处于调试模式,则如果不处于调试模式,它可能会运行得更快。通常,对于连接到运行 eclipse IDE 的 PC 的设备,您可以在不处于调试模式时看到 logcat 消息。如果你可以让它工作,你应该尝试以这种方式运行,看看它是否更快。还可以尝试在手机完全从 Eclipse 中拔出的情况下运行,很难判断它是否更快,但它可能会更快。

    【讨论】:

    • 我尝试在未连接到 Eclipse 的情况下运行该应用程序并得到相同的结果。我没有将意图发送到活动。相反,我将意图发送到 AppWidgetProvider 以更新 appWidget。至于调试模式,有没有办法禁用应用程序的调试模式,或者您是在谈论手机的 USB 调试功能?
    • 我说的是使用“调试方式”或“运行方式”从 Eclipse 启动应用程序,没有比这更深入的了。如果它在电话上裸奔那么慢,我没有好的建议。
    猜你喜欢
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 2018-05-15
    相关资源
    最近更新 更多