【问题标题】:Android phone hanging because of continuous location updateAndroid 手机因位置不断更新而挂起
【发布时间】: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


【解决方案1】:

您的m_Runnable 正在发布自己以每秒运行一次。每次运行时,它都会调用locationUpdate(),它会注册一个新的LocationListener。五分钟后,您将有 300 个侦听器处理每个更新。这可能就是您的应用陷入困境的原因!

您无需在每次想要接收位置更新时都注册新的侦听器。只需在活动设置生命周期方法中注册一次。不要忘记在相应的拆卸生命周期方法中取消注册它。您可能应该使用onStart/onStop,而不是onCreate/onDestroy。如果您需要在应用处于后台时接收更新,则侦听器应属于Service

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-30
    • 2012-05-16
    • 2014-05-02
    • 1970-01-01
    • 2014-09-19
    • 2016-04-25
    • 2023-03-31
    • 1970-01-01
    相关资源
    最近更新 更多