WiFi更改状态会影响我的应用

时间:2015-04-19 09:20:28

标签: android broadcastreceiver android-wifi wifimanager android-anr-dialog

我正在通过我的应用程序使用WiFiManager,在应用程序启动的某个级别,它向用户显示所有可用的WiFi网络,并使他能够选择其中一个并建立连接,我正在制作通过我的 manifest.xml

注册CONNECTIVITY_CHANGE接收者
    <receiver android:name="com.automation.standards.WifiReceiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>

然后通过我的BroadcastReceiver处理收到的回复:

public void onReceive(Context c, Intent intent) throws Error{
   // all of the work needed
}

问题是,当我已经在应用程序中时(由于WiFi连接丢失,连接到另一个网络,或者有任何连接问题)收到此类响应,导致我的应用程序非常慢并且在很多导致它挂起并给我 ANR 消息,那么如何解决?

供参考,这是我的log

04-19 10:55:07.331: I/System.out(3266): [CDS]shutdownInput in read
04-19 10:55:07.422: D/InputReader(504): AP_PROF:AppLaunch_dispatchPtr:Down:1795475
04-19 10:55:07.422: I/AP_PROF(504): unable to open /proc/mtprof/status entry
04-19 10:55:07.423: D/PowerManagerService(504): userActivityFromNative
04-19 10:55:07.423: D/PowerManagerService(504): userActivityNoUpdateLocked: eventTime=1795475, event=2, flags=0x0, uid=1000
04-19 10:55:07.423: D/PowerManagerNotifier(504): onUserActivity: event=2, uid=1000
04-19 10:55:07.423: D/PowerManagerService(504): updateUserActivitySummaryLocked: mWakefulness=Awake, mUserActivitySummary=0x1, nextTimeout=1848475 (in 53000 ms)
04-19 10:55:07.423: D/PowerManagerService(504): newScreenState = 2
04-19 10:55:07.423: D/PowerManagerDisplayController(504): requestPowerState: screenState=2, useProximitySensor=false, screenBrightness=255, screenAutoBrightnessAdjustment=0.0, useAutoBrightness=false, useEcoBrightness=false, blockScreenOn=false, waitForNegativeProximity=false
04-19 10:55:07.423: I/PowerManagerService(504): setBrightness mButtonLight 0.
04-19 10:55:07.423: D/PowerManagerService(504): updateScreenStateLocked: mDisplayReady=true, newScreenState=2, mWakefulness=1, mWakeLockSummary=0x23, mUserActivitySummary=0x1, mBootCompleted=true
04-19 10:55:07.423: D/PowerManagerService(504): handleSandman: canDream=true, mWakefulness=Awake
04-19 10:55:07.436: I/SurfaceTextureClient(504): [STC::queueBuffer] (this:0x50743310) fps:0.32, dur:52466.98, max:52200.05, min:15.00
04-19 10:55:07.436: I/SurfaceTextureClient(504): [STC::queueBuffer] this:0x50743310, api:2, last queue time elapsed:52200.05
04-19 10:55:07.436: I/BufferQueue(139): [Application Not Responding: com.automation.isolace](this:0x41d3d578,api:2) [queue] fps:0.32, dur:52467.09, max:52200.29, min:15.03
04-19 10:55:07.451: I/BufferQueue(139): [Application Not Responding: com.automation.isolace](this:0x41d3d578,api:2) [release] fps:0.32, dur:52469.17, max:52201.50, min:16.49
04-19 10:55:07.451: I/SurfaceFlinger(139): [SurfaceFlinger] fps:0.069765,dur:14333.80,max:14333.80,min:14333.80
04-19 10:55:07.452: I/SurfaceTextureClient(139): [STC::queueBuffer] (this:0x418db868) fps:0.07, dur:14334.23, max:14334.23, min:14334.23
04-19 10:55:07.452: I/SurfaceTextureClient(139): [STC::queueBuffer] this:0x418db868, api:1, last queue time elapsed:14334.23
04-19 10:55:07.452: I/BufferQueue(139): [FramebufferSurface](this:0x418de008,api:1) [release] fps:0.07, dur:14334.28, max:14334.28, min:14334.28
04-19 10:55:07.452: I/BufferQueue(139): [FramebufferSurface](this:0x418de008,api:1) [queue] fps:0.07, dur:14334.27, max:14334.27, min:14334.27
04-19 10:55:07.508: D/InputReader(504): AP_PROF:AppLaunch_dispatchPtr:Up:1795561
04-19 10:55:07.509: V/Provider/Settings(504): invalidate [system]: current 15 != cached 14
04-19 10:55:07.509: D/SettingsProvider(504): call request for 0
04-19 10:55:07.509: V/SettingsProvider(504): call(system:sound_effects_enabled) for 0
04-19 10:55:07.510: D/SettingsProvider(504): lookupValue table system cache contains Key sound_effects_enabled , value = Bundle[{value=0}]
04-19 10:55:07.510: V/Provider/Settings(504): from db cache, name = sound_effects_enabled , value = 0
04-19 10:55:07.511: W/ActivityManager(504):   Force finishing activity com.automation.isolace/.Login
04-19 10:55:07.540: D/PowerManagerService(504): acquireWakeLockInternal: lock=1093115944, flags=0x1, tag="ActivityManager-Launch", ws=null, uid=1000, pid=504
04-19 10:55:07.540: D/PowerManagerService(504): updateWakeLockSummaryLocked: mWakefulness=Awake, mWakeLockSummary=0x23
04-19 10:55:07.540: D/PowerManagerService(504): newScreenState = 2
04-19 10:55:07.541: D/PowerManagerDisplayController(504): requestPowerState: screenState=2, useProximitySensor=false, screenBrightness=255, screenAutoBrightnessAdjustment=0.0, useAutoBrightness=false, useEcoBrightness=false, blockScreenOn=false, waitForNegativeProximity=false
04-19 10:55:07.541: I/PowerManagerService(504): setBrightness mButtonLight 0.
04-19 10:55:07.541: D/PowerManagerService(504): updateScreenStateLocked: mDisplayReady=true, newScreenState=2, mWakefulness=1, mWakeLockSummary=0x23, mUserActivitySummary=0x1, mBootCompleted=true
04-19 10:55:07.541: D/PowerManagerService(504): handleSandman: canDream=true, mWakefulness=Awake
04-19 10:55:07.541: I/ActivityManager(504): Killing ProcessRecord{4158b7a8 3266:com.automation.isolace/u0a10095}: user's request

我拉了设备 anr / traces ,这就是我得到的:

----- pid 504 at 2015-04-19 10:54:07 -----
Cmd line: system_server

JNI: CheckJNI is off; workarounds are off; pins=4; globals=910 (plus 22 weak)

DALVIK THREADS:
(mutexes: tll=0 tsl=0 tscl=0 ghl=0)

"main" prio=5 tid=1 NATIVE
  | group="main" sCount=1 dsCount=0 obj=0x40f73a18 self=0x40f62f60
  | sysTid=504 nice=0 sched=0/0 cgrp=apps handle=1074292828
  | state=S schedstat=( 1785675930 882426903 4924 ) utm=101 stm=77 core=0
  #00  pc 0002066c  /system/lib/libc.so (__ioctl+8)
  #01  pc 00040b90  /system/lib/libc.so (ioctl+28)
  #02  pc 00016b59  /system/lib/libbinder.so (android::IPCThreadState::talkWithDriver(bool)+132)
  #03  pc 0001734f  /system/lib/libbinder.so (android::IPCThreadState::joinThreadPool(bool)+154)
  #04  pc 00001311  /system/lib/libsystem_server.so (system_init+388)
  #05  pc 0001e4d0  /system/lib/libdvm.so (dvmPlatformInvoke+112)
  #06  pc 0004ddf9  /system/lib/libdvm.so (dvmCallJNIMethod(unsigned int const*, JValue*, Method const*, Thread*)+500)
  #07  pc 000278a0  /system/lib/libdvm.so
  #08  pc 0002b804  /system/lib/libdvm.so (dvmInterpret(Thread*, Method const*, JValue*)+180)
  #09  pc 000613cf  /system/lib/libdvm.so (dvmInvokeMethod(Object*, Method const*, ArrayObject*, ArrayObject*, ClassObject*, bool)+374)
  #10  pc 000692e9  /system/lib/libdvm.so
  #11  pc 000278a0  /system/lib/libdvm.so
  #12  pc 0002b804  /system/lib/libdvm.so (dvmInterpret(Thread*, Method const*, JValue*)+180)
  #13  pc 000610a9  /system/lib/libdvm.so (dvmCallMethodV(Thread*, Method const*, Object*, bool, JValue*, std::__va_list)+272)
  #14  pc 0004a0d1  /system/lib/libdvm.so
  #15  pc 0004d129  /system/lib/libandroid_runtime.so
  #16  pc 0004decd  /system/lib/libandroid_runtime.so (android::AndroidRuntime::start(char const*, char const*)+400)
  #17  pc 00000db7  /system/bin/app_process
  #18  pc 0001bd98  /system/lib/libc.so (__libc_init+64)
  #19  pc 00000ae8  /system/bin/app_process
  at com.android.server.SystemServer.init1(Native Method)
  at com.android.server.SystemServer.main(SystemServer.java:1319)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:511)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
  at dalvik.system.NativeStart.main(Native Method)

"Binder_8" prio=5 tid=65 NATIVE
  | group="main" sCount=1 dsCount=0 obj=0x41809398 self=0x59359cc8
  | sysTid=1874 nice=0 sched=0/0 cgrp=apps handle=1459552608
  | state=S schedstat=( 1289606286 686977311 3543 ) utm=82 stm=46 core=0
  #00  pc 0002066c  /system/lib/libc.so (__ioctl+8)
  #01  pc 00040b90  /system/lib/libc.so (ioctl+28)
  #02  pc 00016b59  /system/lib/libbinder.so (android::IPCThreadState::talkWithDriver(bool)+132)
  #03  pc 0001734f  /system/lib/libbinder.so (android::IPCThreadState::joinThreadPool(bool)+154)
  #04  pc 0001b1c9  /system/lib/libbinder.so
  #05  pc 00011267  /system/lib/libutils.so (android::Thread::_threadLoop(void*)+114)
  #06  pc 0004d053  /system/lib/libandroid_runtime.so (android::AndroidRuntime::javaThreadShell(void*)+150)
  #07  pc 00010dcd  /system/lib/libutils.so
  #08  pc 0000e058  /system/lib/libc.so (__thread_entry+72)
  at dalvik.system.NativeStart.run(Native Method)
  .
  .
  .

1 个答案:

答案 0 :(得分:1)

onReceive()中完成它的操作有一个时间限制。通常是10秒。所以我假设你没有按照10秒的限制完成任务,从而导致ANR

广播接收器的时间限制是10秒,而不是5秒 活动的秒数。这是一种缓解,但限制仍然存在。

托管广播接收器的过程将开始和终止 以及广播接收器执行。因此,这个过程会 在广播接收器的onReceive()方法之后不要留下来 回报。 您应该将任务从onReceive()

移至活动或服务