【问题标题】:does calling locationmanager.removeupdates permanently removes it in android?调用 locationmanager.removeupdates 会在 android 中永久删除它吗?
【发布时间】:2015-08-11 05:33:08
【问题描述】:

如果手机在一分钟间隔内移动,我正在创建一个应用程序以通过 SMS 发送纬度和经度。 在 ArrayList 达到它发送 SMS 的六个元素后,我使用 ArrayList 保存了六个 LatLngs。如果设备在六分钟内停止移动,则ArrayList 将小于六。如果设备在 20 分钟内没有移动,那么它会发送未填充的 ArrayList。我使用CountdownTimer 来实现这一点。 它运行良好,只要我移动它就会发送LatLngs。但是,如果我在 ArrayList 填满之前停下来,它会在 20 分钟后发送 ArrayList 。这不是问题。但是,之后它会以一分钟的间隔继续发送短信。我的代码是

private void sendLog() {

    Toast.makeText(MainPage.this,"Sending Log",Toast.LENGTH_LONG).show();
    final SharedPreferences account=getSharedPreferences("admins", MODE_PRIVATE);
    String interval=account.getString("lti", "");
    int timeInterval=Integer.parseInt(interval);

    final LocationManager logManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
    logManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, timeInterval * 60000, 100, new LocationListener() {
        @Override
        public void onLocationChanged(final Location location) {
            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
            DecimalFormat dFormat = new DecimalFormat("#.####");
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("kk:mm");
            SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM,yy");
            String time = sdf.format(date);
            final String dateLog = sdf1.format(date);

            loglist.add("!+" + dFormat.format(latitude) + ",+" + dFormat.format(longitude) + "," + time);
            Toast.makeText(MainPage.this, loglist.toString(), Toast.LENGTH_SHORT).show();

            CountDownTimer countDownTimer = new CountDownTimer(1200000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    if (loglist.size() == 6) {
                        for (int j = 0; j < loglist.size(); j++) {
                            log.append(loglist.get(j).toString());
                        }
                        Toast.makeText(MainPage.this, log.toString(), Toast.LENGTH_SHORT).show();
                        SmsManager smsManager = SmsManager.getDefault();
                        smsManager.sendTextMessage(logPreferences.getString("admin1", ""), null, "  " + log.toString() + "!" + dateLog + "!", null, null);
                        loglist.removeAll(loglist);
                        log.setLength(0);
                    }
                }

                @Override
                public void onFinish() {
                    if (loglist.size()<6) {
                        for (int j = 0; j < loglist.size(); j++) {
                            log.append(loglist.get(j).toString());
                        }
                        SmsManager smsManager = SmsManager.getDefault();
                        smsManager.sendTextMessage(logPreferences.getString("admin1", ""), null, "  " + log.toString() + "!" + dateLog + "!", null, null);
                        loglist.removeAll(loglist);
                        log.setLength(0);
                    }
                }
            }.start();
        }

我想打电话给logManaager.removeupdates()。这是否会导致无法再次调用 locationUpdates。因为我希望设备在移动时发送。帮帮我。

【问题讨论】:

    标签: java android arraylist location locationmanager


    【解决方案1】:

    您调用logManager.removeUpdates() 的直觉是正确的。

    致电removeUpdates() 后,您可以再次请求更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多