问题
I'm using WiFiManager through my app, at some level in the starting of the app it displays all the available WiFi networks to the user and enables him to choose one of them and make a connection to it, I'm making this by registering the CONNECTIVITY_CHANGE
receiver through my manifest.xml:
<receiver android:name="com.automation.standards.WifiReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
and then dealing with the received responses through my BroadcastReceiver
:
public void onReceive(Context c, Intent intent) throws Error{
// all of the work needed
}
The problem is, when receiving such responses while I'm already inside the app(due to WiFi connectivity lost, connecting to another network, or having any connection problems) that causes my app to be very slow and in a lot of causes it hangs and gives me ANR message, so how to overcome that?
for reference, this is my 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
and I pulled the device anr/traces and this is what I got:
----- 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:
There is a time limit to complete it's operations in onReceive()
. Usually its 10 second. So I assume you aren't finishing your task by 10 sec limit thus resulting to ANR
.
The time limit on a broadcast receiver is ten seconds compared to five seconds for an activity. It is a touch of a reprieve, but the limit is still there.
The process hosting the broadcast receiver will start and terminate
along with the broadcast receiver execution. Hence the process will
not stick around after the broadcast receiver's onReceive () method
returns.
you should move your task to an activity or service from onReceive()
来源:https://stackoverflow.com/questions/29728000/wifi-changed-status-affecting-my-app