android7 socket休眠,miui7开发版中,休眠状态下AlarmManager定时广播会失效,网络连接会出错。...

在AlarmManager中设置每隔一小时周期发送广播,设置后能在adb shell中看到。但一般在经过一个周期之后,再次adb shell中查看,就发现alarmmanager中已经没有装订好的广播了。难道是被os自行取消了?我的手机os是miui7 5.10.8开发版 android 5.1。

装订后,在adb shell看到的形式一般如下,

RTC #2: Alarm{3a928873 type 1 when 1446138667081 com.example.weathertrack}

tag=*alarm*:com.example.weather.WAKEUP

operation=PendingIntent{b5186f4: PendingIntentRecord{3d8cfe1d com.example.weathertrack broadcastIntent}}

u0a233:com.example.weathertrack +1s109ms running, 0 wakeups:

+1s109ms 0 wakes 2 alarms: *alarm*:com.example.weather.WAKEUP

设置alarmmanager的代码如下,

public static void sendBroadcast(Context context) {

AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

long setTime = 1000 * 3600;

Intent intent = new Intent(context, WakeUpApp.class);

intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);

intent.setAction("com.example.weather.WAKEUP");

PendingIntent pendingIntent = PendingIntent

.getBroadcast(context, 1562, intent, PendingIntent.FLAG_UPDATE_CURRENT);

manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000 * 60, setTime, pendingIntent);

}

在AndroidManifest中receiver的定义如下,

ADD 1:

我在开发者选项中设置了不锁定屏幕,这样在充电中手机就不会休眠。从中午开始到现在,可以发现AlarmManager运行良好,并没有出现定时广播消失的情况…………看来只能明早起来再看,手机在休眠状态下,定时广播是不是会消失。另外,我已经严重怀疑这是miui的锅,在这过程中我大概找到了另一个让人莫名其妙的bug的出现原因。

ADD2

上午再次在adb shell中查看,定时广播果然没了……看来只要待机时间一长,定时广播就会被取消,实在让人费解。

ADD3

让人帮忙在别的手机上试下,定时更新数据正常,看来这是miui的坑了。

另外还有一个坑。在我app中,本来是想定时发送广播唤起后台服务,然后下载数据。我发现即使AlarmManager正常工作,唤醒后台服务之后,下载必然会报错,

10-30 13:46:37.929 26642-26673/com.example.weathertrack W/System.err: java.net.ConnectException: failed to connect to api.thinkpage.cn/115.29.232.24 (port 443) after 10000ms: isConnected failed: ECONNREFUSED (Connection refused)

10-30 13:46:37.939 26642-26673/com.example.weathertrack W/System.err: at libcore.io.IoBridge.isConnected(IoBridge.java:238)

10-30 13:46:37.939 26642-26673/com.example.weathertrack W/System.err: at libcore.io.IoBridge.connectErrno(IoBridge.java:171)

10-30 13:46:37.939 26642-26673/com.example.weathertrack W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:122)

10-30 13:46:37.939 26642-26673/com.example.weathertrack W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)

10-30 13:46:37.939 26642-26673/com.example.weathertrack W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)

10-30 13:46:37.939 26642-26673/com.example.weathertrack W/System.err: at java.net.Socket.connect(Socket.java:882)

10-30 13:46:37.939 26642-26673/com.example.weathertrack W/System.err: at com.squareup.okhttp.internal.Platform$Android.connectSocket(Platform.java:190)

10-30 13:46:37.939 26642-26673/com.example.weathertrack W/System.err: at com.squareup.okhttp.Connection.connectSocket(Connection.java:196)

10-30 13:46:37.939 26642-26673/com.example.weathertrack W/System.err: at com.squareup.okhttp.Connection.connect(Connection.java:172)

10-30 13:46:37.939 26642-26673/com.example.weathertrack W/System.err: at com.squareup.okhttp.Connection.connectAndSetOwner(Connection.java:367)

10-30 13:46:37.939 26642-26673/com.example.weathertrack W/System.err: at com.squareup.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:128)

10-30 13:46:37.939 26642-26673/com.example.weathertrack W/System.err: at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:328)

10-30 13:46:37.939 26642-26673/com.example.weathertrack W/System.err: at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:245)

10-30 13:46:37.939 26642-26673/com.example.weathertrack W/System.err: at com.squareup.okhttp.Call.getResponse(Call.java:267)

10-30 13:46:37.939 26642-26673/com.example.weathertrack W/System.err: at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:224)

10-30 13:46:37.939 26642-26673/com.example.weathertrack W/System.err: at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:195)

10-30 13:46:37.939 26642-26673/com.example.weathertrack W/System.err: at com.squareup.okhttp.Call.access$100(Call.java:34)

10-30 13:46:37.939 26642-26673/com.example.weathertrack W/System.err: at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:162)

10-30 13:46:37.939 26642-26673/com.example.weathertrack W/System.err: at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)

10-30 13:46:37.939 26642-26673/com.example.weathertrack W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)

10-30 13:46:37.939 26642-26673/com.example.weathertrack W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)

10-30 13:46:37.939 26642-26673/com.example.weathertrack W/System.err: at java.lang.Thread.run(Thread.java:818)

10-30 13:46:37.939 26642-26673/com.example.weathertrack W/System.err: Caused by: android.system.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)

10-30 13:46:37.939 26642-26673/com.example.weathertrack W/System.err: at libcore.io.IoBridge.isConnected(IoBridge.java:223)

10-30 13:46:37.939 26642-26673/com.example.weathertrack W/System.err: ... 21 more

看日志第一行,我查过之后发现这个错误一般是因为服务器端出错造成的。但是在我这个例子中肯定不是网站服务器的问题,每次只要我在打开app的情况下手动更新数据,就能正常下载数据。另外,今天下午别人的测试反馈也是软件定时更新数据正常。

我在adb shell中查看了logcat,发现只要开始网络连接,小米内置的网络助手就会监控,

10-30 13:46:37.929 I/XiaomiFirewall(10262): firewall pkgName:com.example.weathertrack, result:0

但是无法确认是不是小米网络助手的问题,因为即使正常连接,也会返回如上日志。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值