2

嗨,我已经开发了一个用于获取位置并将其发送到服务器的 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) {
    }

}
4

3 回答 3

2

您可以使用 requestLocationUpdatesLocationManager 类

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

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

要跟踪经过的小时数,您可以使用以下内容:

var1 = System.CurrentTimeMillis(); //Get time when service started

onLocationChanged()

var2 = System.CurrentTimeMillis();

现在计算var2-var1得到经过的时间。如果 >17 小时,removeUpdateslocationManager

于 2013-04-29T16:30:37.230 回答
0

我有一个类似的项目正在进行,我为此使用了一个由AlarmManager触发的服务在这里查看更多信息。

于 2013-04-29T16:22:34.557 回答
0

Schaemelhout 提供的方法和评论中提到的库所采用的方法是我找到的唯一可行的解​​决方案。

使用 AlarmManager 唤醒您的服务可确保定期调用/重新启动该服务。这有助于抵消操作系统/手机可能同时影响您的服务的任何省电操作。

从警报 BroadcastReceiver 注册位置监听器会唤醒 GPS(如果手机处于睡眠状态,GPS 通常不会运行)。在您的警报 BroadcastReceiver 中,您需要获取并保持 WakeLock 并保持清醒,直到结果传回给侦听器。

这种方法的缺点是位置可用需要一段时间,而且准确性可能很差。您的服务可能需要等待一段时间才能达到可接受的准确度(位置准确度值可能是错误的,因此最好等待几个样本)。整个方法需要根据您所需的更新频率和准确性要求进行调整。如果您需要非常频繁和准确的更新,我怀疑持有唤醒锁并保持 GPS 活动可能是唯一的方法。

我还尝试使用 PendingIntent 使用 lm.requestLocationUpdates()。这确实忠实地唤醒了我的服务并传递了一个 Intent,但如果 GPS 在唤醒之前处于休眠状态,则 Intent 缺少位置。

于 2014-03-08T20:55:07.927 回答