为什么LocationListener会在一段时间后变为禁用状态?

时间:2013-12-04 17:36:09

标签: android locationmanager android-location locationlistener

我写了一个位置监听器类,它记录了30000毫秒和100米的更新。它将数据收集到sharedPreferences 10分钟,然后发送整个数据并清除共享密钥。

然而他们说应用程序在2,3小时后没有发送数据?该应用程序在后台运行,而不是服务。这是代码:

MainActivity.class

LocManager locMan= null;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        locMan = new LocManager(MainActivity.this);
}

LocationManager.java

public class LocManager implements LocationListener {

Context cnx;
String glbData = "";
LocationManager manager = null;
public boolean sendOnly1Record = false;
public int saveUserApplicationStatusCalled = 0;
Date firstDate = null;
Date currDate = null;
String USR_ID = "", USR_PASSWORD = "", USR_DOMAIN = "";
private boolean isUploadingNow = false;

public LocManager(Context cnx) {
    this.cnx = cnx;
    manager = (LocationManager) this.cnx
            .getSystemService(Context.LOCATION_SERVICE);
    SharedPreferences sh = cnx.getSharedPreferences(Constants.SH_GLB_DATA,
            Context.MODE_PRIVATE);
    glbData = sh.getString(Constants.SH_GLB_KEY, "");
    USR_ID = sh.getString(Constants.SH_USR_ID_KEY, "nil");
    USR_PASSWORD = sh.getString(Constants.SH_USR_PASS_KEY, "nil");
    USR_DOMAIN = sh.getString(Constants.SH_USR_DOMAIN_KEY, "nil");
    firstDate = new Date();
}

public void startUpdate() {
    Criteria myCriteria = new Criteria();
    myCriteria.setAccuracy(Criteria.ACCURACY_FINE);
    myCriteria.setPowerRequirement(Criteria.POWER_LOW);

    String myProvider = manager.getBestProvider(myCriteria, true);


    long minTimeMillis = 30000;
    manager.requestLocationUpdates(myProvider, minTimeMillis, 100, this);
}

@Override
public void onLocationChanged(Location location) {

    if (sendOnly1Record) {
        sendOnly1Record = false;

        funcSendOnly1Record(location);
    } else if (!isUploadingNow) {
        String str_date = getCurrentStrDate();
        String str_battery_level = "" + getBatteryLevel();
        String str_lat = "" + location.getLatitude();
        String str_lon = "" + location.getLongitude();

        long interval = getDateDiffsinMinutes();

        if (interval > 10) {
            isUploadingNow = true;
            postData();
            firstDate = new Date();
        } else {
            StringBuffer buf = new StringBuffer();
            buf.append(str_date);
            buf.append("|");
            buf.append(str_lat);
            buf.append("|");
            buf.append(str_lon);
            buf.append("|");
            buf.append(str_battery_level);
            buf.append("|");

            buf.append("" + saveUserApplicationStatusCalled);
            buf.append("|");

            glbData = glbData + buf.toString();
            SharedPreferences sh = cnx.getSharedPreferences(
                    Constants.SH_GLB_DATA, Context.MODE_PRIVATE);
            SharedPreferences.Editor ed = sh.edit();
            ed.putString(Constants.SH_GLB_KEY, glbData);
            ed.commit();

            if (saveUserApplicationStatusCalled == 0) {
                saveUserApplicationStatusCalled = 1;
            }
        }

    }
}

@Override
public void onProviderDisabled(String provider) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

private void postData() {

    //Posts the collected data

}


};

public void funcSendOnly1Record(Location location) {

    String str_date = getCurrentStrDate();
    String str_battery_level = "" + getBatteryLevel();

    String str_lat = "" + location.getLatitude();
    String str_lon = "" + location.getLongitude();

    StringBuffer buf = new StringBuffer();
    buf.append(str_date);
    buf.append("|");
    buf.append(str_lat);
    buf.append("|");
    buf.append(str_lon);
    buf.append("|");
    buf.append(str_battery_level);
    buf.append("|");

    buf.append("" + saveUserApplicationStatusCalled);
    buf.append("|");

    //post one data

}


private int getBatteryLevel() {
    Intent batteryIntent = cnx.registerReceiver(null, new IntentFilter(
            Intent.ACTION_BATTERY_CHANGED));
    int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

    // Error checking that probably isn't needed but I added just in case.
    if (level == -1 || scale == -1) {
        return 50;
    }
    float x = ((float) level / (float) scale) * 100.0f;
    return (int) x;
}

private long getDateDiffsinMinutes() {
    currDate = new Date();
    long result = ((currDate.getTime() / 60000) - (firstDate.getTime() / 60000));
    return (int) result;
}

private String getCurrentStrDate() {
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = new Date();
    String str_date = dateFormat.format(date);
    return str_date;
}
}

1 个答案:

答案 0 :(得分:0)

您的后台进程正在终止以释放其他应用程序的内存。这是完全正常和预期的行为。