【发布时间】:2018-09-26 05:38:11
【问题描述】:
我正在开发一个安卓应用程序。在我的应用程序中,我需要持续更新位置,并且需要将该 lat lng 存储到 firestore 文档中。所有工作都很好。我不断获取位置更新并存储在 Firestore 中。但问题是当我打开我的应用程序时,我的 android 手机会在 5 分钟内挂起。以下是我关于位置更新的代码。
LocationManager locmngr; // Location manager declaration
locationUpdate(); // Initializing the method in onCreate()
public void locationUpdate()
{
locmngr = (LocationManager) getSystemService(LOCATION_SERVICE);
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) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}, 12);
return;
}
boolean gps_enabled = false;
boolean network_enabled = false;
try {
gps_enabled = locmngr.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
Toast.makeText(TabsActivity.this, ""+ex, Toast.LENGTH_SHORT).show();
}
try {
network_enabled = locmngr.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
Toast.makeText(TabsActivity.this, ""+ex, Toast.LENGTH_SHORT).show();
}
if (!gps_enabled && !network_enabled) {
// notify user
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage("GPS network not enabled");
dialog.setPositiveButton("Open Location Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
//get gps
}
});
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub
}
});
dialog.show();
}
try {
locmngr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 7, 30, new LocationListener() {
@Override
public void onLocationChanged(final Location location) {
// Add a marker at your current location and move the camera
//Log.e("location", location.toString());
if(!user_id.isEmpty()){
// code to write lat longs into firestore
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
ActivityCompat.requestPermissions(TabsActivity.this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 12);
}
});
} catch (Exception ex) {
Toast.makeText(TabsActivity.this, ""+ex, Toast.LENGTH_SHORT).show();
}
}
private final Runnable m_Runnable = new Runnable()
{
public void run()
{
try {
locationUpdate();
TabsActivity.this.mHandler.postDelayed(m_Runnable, 1000);
}
catch (Exception ex)
{
Toast.makeText(TabsActivity.this, ""+ex, Toast.LENGTH_SHORT).show();
}
}
};
我需要每秒更新一次位置。我必须为此做些什么(电话挂起)。请建议我任何其他可能性。提前致谢。
【问题讨论】:
-
电话挂起到底是什么意思?您的应用程序变慢了,还是所有应用程序都变慢了,或者手机是否停止对所有输入做出反应,或者其他什么?您是否使用工具检查过哪些应用正在消耗内存或 CPU?
-
最好使用
FusedLocationProviderAPI。 -
您需要编写一个单独的服务来获取用户位置。并确保它应该在单独的线程而不是 UI 线程上运行。
-
手机停止对所有输入做出反应,一段时间后手机会自行重启。 #罗兰·韦伯
-
你应该使用google api客户端来有效地获取位置更新
标签: android google-cloud-firestore locationmanager android-gps android-developer-api