我已经构建了一个应用程序。我的测试用例是在我的 Android 选项卡中,当我断开 wifi 并强制停止应用程序时。现在再次启动应用程序并再次连接到 wifi,然后我的 web 服务没有响应 wifi 并且没有将记录推送到服务器。
代码是..
public void onReceive(Context context, Intent intent) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null) {
if (info.isConnectedOrConnecting()) {
Intent i = new Intent(context, Timeclock.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(i);
} else {
Intent i = new Intent(context, Timeclock.class);
context.stopService(i);
}
}
}
在 Android.manifest.xml 文件中
<receiver android:enabled="true" android:name="com.example.timeclock.activity.StartApp" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.net.wifi.STATE_CHANGE"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
在logcat中它显示为
10-22 16:21:08.743: W/System.err(5950): java.net.UnknownHostException: Unable to resolve host "myexample.com": No address associated with hostname
10-22 16:21:08.743: W/System.err(5950): at java.net.InetAddress.lookupHostByName(InetAddress.java:490)
10-22 16:21:08.743: W/System.err(5950): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:281)
10-22 16:21:08.743: W/System.err(5950): at java.net.InetAddress.getAllByName(InetAddress.java:249)
10-22 16:21:08.743: W/System.err(5950): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
10-22 16:21:08.743: W/System.err(5950): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-22 16:21:08.743: W/System.err(5950): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-22 16:21:08.743: W/System.err(5950): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
10-22 16:21:08.743: W/System.err(5950): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-22 16:21:08.743: W/System.err(5950): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-22 16:21:08.743: W/System.err(5950): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
Wifi 没有响应,即使我检查过它可用。所以,我希望wifi在强制停止应用程序后对应用程序做出响应。