我正在为Geo fencing开发一个Android应用程序,它使用Google map v2来加载地图。我面临的问题是,当使用3G或4G网络连接初始化地图时,我得到一个空白屏幕。一段时间后,应用程序变得无响应,在使用WIFI初始化时,地图初始化没有任何延迟和任何次数。 我将嵌入下面的代码
GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
queue = Volley.newRequestQueue(getApplicationContext());
pref = getPreferences( Context.MODE_PRIVATE );
//db MANAGER
dbManager = new DBManager(this);
dbManager.open();
InitializeClient();
InitializeDB();
} catch (Exception e) {
e.printStackTrace();
}
mqtt=new MQTT();
try{
if(ip==null){
System.out.println("Mqtt Initialized in Maps Activity");
ip="demo.aiotm.in:1883";
}
//mqtt.setHost("tcp://"+ip);
mqtt.setHost("tcp://10.30.60.242:1883");
connection = mqtt.blockingConnection();
connection.connect();
}catch (Exception e){
e.printStackTrace();
}
}
private void InitializeClient() {
try {
if (mMap==null){
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
//Initializing googleApiClient
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
googleApiClient.connect();
if (mMap==null){
Toast.makeText(getApplicationContext(),"Sorry Unable to create maps",Toast.LENGTH_SHORT).show();
}
}
}catch (Exception e){e.printStackTrace();}
if (this.mMap != null) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}else {
System.out.println(":::::::::::::::::::::::::::::::::: Map Not Initialized :::::::::::::::::::::::::::::::::::::::::;");}
mMap.setMyLocationEnabled(true);
}
if (googleApiClient.isConnected()||googleApiClient.isConnecting()){System.out.println("------------------------------------>Api is connecting<-----------------------------------");}
}
@Override
protected void onResume() {
super.onResume();
InitializeClient();
getCurrentLocation();
}
@Override
protected void onPause() {
super.onPause();
}
我在清单文件中提供的权限
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
有人遇到过这样的问题吗?如果有解决方案,请与我分享!
答案 0 :(得分:0)
我不认为问题是地图初始化,似乎你在主线程上建立连接
mqtt = new MQTT();
try {
if (ip == null) {
System.out.println("Mqtt Initialized in Maps Activity");
ip = "demo.aiotm.in:1883";
}
//mqtt.setHost("tcp://"+ip);
mqtt.setHost("tcp://10.30.60.242:1883");
connection = mqtt.blockingConnection();
connection.connect();
} catch (Exception e) {
e.printStackTrace();
}
可能这个地址tcp://10.30.60.242:1883
只能在您的内部wifi网络中访问,因此在通过移动数据连接进行测试时,调用goeas会超时