我正在使用GoogleMap
实例来显示用户的位置。一切正常,直到我关闭wi-fi
。应用程序停止响应,甚至出现ANR
。日志说UI
线程上有太多工作。我知道GoogleMap
需要网络才有用。当网络出现故障时是否有任何方法可以捕获或者是否必须让广播接收器通知用户并显示错误?
这是我对地图的实例化:
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
googleMap.setMyLocationEnabled(true);
在我看来,只有当我将onLocationChangeListener设置为
时才会出现这种情况答案 0 :(得分:0)
您可以实施LocationListener
课程然后它会覆盖某些方法,然后在onProviderDisabled
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
// do your action here
}
答案 1 :(得分:0)
如果网络出现故障,我可以帮助你,创建一个新的java类
public class ConnectionChangeReceiver extends BroadcastReceiver
{
@Override
public void onReceive( Context context, Intent intent )
{
ConnectivityManager connectivityManager = (ConnectivityManager)
context.getSystemService( Context.CONNECTIVITY_SERVICE );
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
NetworkInfo mobNetInfo =
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE );
if ( activeNetInfo != null )
{
Toast.makeText( context, "Active Network Type : " +
activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
}
if( mobNetInfo != null )
{
Toast.makeText( context, "Mobile Network Type : " +
mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
}
}
}
在" manifest"下的AndroidManifest.xml中元素添加
<!-- Needed to check when the network connection changes -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
在&#34;应用程序&#34; element:在AndroidManifest.xml中添加
<receiver android:name="com.blackboard.androidtest.receiver.ConnectionChangeReceiver"
android:label="NetworkConnection">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>