android - android 如何获取每半小时更新一次的位置?

标签 android locationmanager

嗨,我开发了一个Android应用程序,用于获取位置并将其发送到服务器,它运行良好,但位置更新不起作用,一旦我必须每半小时获取一次位置并将其发送到服务器,它当前正在运行,这进程应该只运行 9 小时到 17 小时,我该怎么做,请帮助我..!

我的代码:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        {

            Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
            intent.putExtra("enabled", true);
            this.sendBroadcast(intent);

            String provider = Settings.Secure.getString(getContentResolver(),
                    Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
            if (!provider.contains("gps")) { // if gps is disabled
                final Intent poke = new Intent();
                poke.setClassName("com.android.settings",
                        "com.android.settings.widget.SettingsAppWidgetProvider");
                poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
                poke.setData(Uri.parse("3"));
                this.sendBroadcast(poke);

                manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

                String providerName = manager.getBestProvider(new Criteria(),
                        true);
                Location location = manager.getLastKnownLocation(providerName);

                if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

                    TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                    GsmCellLocation loc = (GsmCellLocation) mTelephonyManager
                            .getCellLocation();
                    String networkOperator = mTelephonyManager
                            .getNetworkOperator();

                    Log.d("CID", Integer.toString(loc.getCid()));
                    Log.d("LAC", Integer.toString(loc.getLac()));
                    int mcc = Integer.parseInt(networkOperator.substring(0, 3));
                    int mnc = Integer.parseInt(networkOperator.substring(3));

                    TextView tv1 = (TextView) findViewById(R.id.locationResults);
                    if (loc != null) {
                        tv1.setText("Cell ID: " + loc.getCid() + " , "
                                + "Lac: " + loc.getLac() + "mcc : " + mcc
                                + "mnc : " + mnc);

                    }

                    // manager.requestLocationUpdates(providerName, 1000 * 60 *
                    // 2, 0,this);

                }

            }

            else {

                String providerName = manager.getBestProvider(new Criteria(),
                        true);
                Location location = manager.getLastKnownLocation(providerName);

                TextView tv = (TextView) findViewById(R.id.locationResults);
                if (location != null) {
                    tv.setText(location.getLatitude() + " latitude, "
                            + location.getLongitude() + " longitude");

                    TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                    telephonyManager.getDeviceId();
                    String imei = telephonyManager.getDeviceId();

                    SimpleDateFormat sdf = new SimpleDateFormat(
                            "yyyyMMdd_HHmmss");
                    String cdt = sdf.format(new Date());

                    double latitude = location.getLatitude();
                    double longitude = location.getLongitude();

                    String lat = Double.toString(latitude);
                    String lon = Double.toString(longitude);

                    String locations = Double.toString(latitude) + ","
                            + Double.toString(longitude);


                    // manager.requestLocationUpdates(providerName, 1000 * 60 *
                    // 2, 0,this);

                }

                else {

                    TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                    GsmCellLocation loc = (GsmCellLocation) mTelephonyManager
                            .getCellLocation();
                    String networkOperator = mTelephonyManager
                            .getNetworkOperator();

                    Log.d("CID", Integer.toString(loc.getCid()));
                    Log.d("LAC", Integer.toString(loc.getLac()));
                    int mcc = Integer.parseInt(networkOperator.substring(0, 3));
                    int mnc = Integer.parseInt(networkOperator.substring(3));

                    // TextView tv1 = (TextView)
                    // findViewById(R.id.locationResults);
                    if (loc != null) {
                        tv.setText("Cell ID: " + loc.getCid() + " , " + "Lac: "
                                + loc.getLac() + "mcc : " + mcc + "mnc : "
                                + mnc);

                    }

                }

                manager.requestLocationUpdates(providerName, 1000 * 60 * 10, 1,
                        this);
            }
        }
    }

    // Find the closest Bart Station
    public String findClosestBart(Location loc) {
        double lat = loc.getLatitude();
        double lon = loc.getLongitude();

        double curStatLat = 0;
        double curStatLon = 0;
        double shortestDistSoFar = Double.POSITIVE_INFINITY;
        double curDist;
        String curStat = null;
        String closestStat = null;

        // sort through all the stations
        // write some sort of for loop using the API.

        curDist = Math.sqrt(((lat - curStatLat) * (lat - curStatLat))
                + ((lon - curStatLon) * (lon - curStatLon)));
        if (curDist < shortestDistSoFar) {
            closestStat = curStat;
        }

        return closestStat;

    }

    @Override
    public void onLocationChanged(Location location) {
        TextView tv = (TextView) findViewById(R.id.locationResults);
        if (location != null) {
            tv.setText(location.getLatitude() + " latitude, "
                    + location.getLongitude() + " longitude");
        } else {
            tv.setText("Problem getting gps NETWORK ID : ");

        }
    }



    @Override
    public void onProviderDisabled(String arg0) {

    }

    @Override
    public void onProviderEnabled(String arg0) {
    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    }

}

最佳答案

您可以使用LocationManager类的requestLocationUpdates

requestLocationUpdates (long minTime, float minDistance, Criteria criteria, PendingIntent intent)

因此,您可以根据您的要求将第一个参数指定为 30 分钟。

要跟踪耗时,您可以使用以下内容:

var1 = System.CurrentTimeMillis();//获取服务启动时间

onLocationChanged()中

var2 = System.CurrentTimeMillis();

现在计算var2-var1以获取耗时。如果 >17 小时,则从 locationManager

removeUpdates

关于android - android 如何获取每半小时更新一次的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16283478/

相关文章:

android - 减少 Android Studio 中的 Gradle 同步时间

android - 如何为图像的某些像素设置颜色

Android ListView和滚动问题

java - 如何使用多级数组遍历 Gson 对象

iOS didEnterRegion 从未调用过

android - 如何从后台服务获取位置经纬度并更新

java - 在android中长时间未更改位置时删除更新

android - ImageView:填充水平保持纵横比

Android Fusedlocationproviderclient 无法与 APN Sim 配合使用,可与 Wifi 或普通互联网或离线配合使用

java - 我应该在 LocationManager 调用权限 if 语句中放入什么