获得在背景ANDROID网络位置 [英] Getting network location in background in ANDROID

查看:105
本文介绍了获得在背景ANDROID网络位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从这里得到的位置信息,在Android操作系统的多个线程研究,并尝试了一些设计。不过,我获得了大量的电池耗尽,每次,只是无法摆脱它。这里是我的code的一部分。

I am researched from many threads here for getting location information in Android OS, and tried few designs. However, I was getting lots of battery drain everytime, just couldn't get rid of it. Here is a part of my code.

LocationReceiver.Java

LocationReceiver.Java

public class LocationReceiver extends BroadcastReceiver {
public static double latestUpdate = 0;
public static int status = 0;
public static int count_upload = 0;
public static int count_ignored = 0;
public static Geocoder gc;
public void onReceive(Context context, Intent intent) {
    // Do this when the system sends the intent
    Bundle b = intent.getExtras();
    Location loc = (Location) b
            .get(android.location.LocationManager.KEY_LOCATION_CHANGED);
    if (loc == null)
        return;
    if (gc == null)
        gc = new Geocoder(context);
    update(loc, context);
}

public void update(Location loc, Context context) {
    // Here I am checking if 10 minutes passed from last update to now.
    // and if so, I am uploading my location to my server.
    if (latestUpdate != 0
            && latestUpdate + ((LaunchReceiver.interval - 2) * 1000) > SystemClock
                    .elapsedRealtime()) {
        // duplicate (ignore)
        count_ignored++;
    } else {
        // Upload to server on an async task.
        // ...
        LocationReceiver.latestUpdate = SystemClock.elapsedRealtime();
        count_upload++;
    }

}
}

LaunchReceiver.Java

LaunchReceiver.Java

public class LaunchReceiver extends BroadcastReceiver {
public static boolean registered = false;
public static LocationManager lm;
public static int interval = 600000;
public static PendingIntent pendingIntent;
SharedPreferences sharedPrefs = null;

@Override
public void onReceive(Context context, Intent intent) {
    if (registered)
        return;
    sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    interval = Integer.parseInt(sharedPrefs.getString("updates_interval",
            "600000"));
    Intent in = new Intent("bdd.sanalmusavir.LOCATION_READY");
    pendingIntent = PendingIntent.getBroadcast(context, 0, in,
            PendingIntent.FLAG_UPDATE_CURRENT);
    lm = (LocationManager) context
            .getSystemService(Context.LOCATION_SERVICE);
    // Register for broadcast intents
    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, interval,
            0, pendingIntent);

    registered = true;
}
}

AndroidManifest.xml中

AndroidManifest.Xml

<application
    <receiver
        android:name=".LaunchReceiver"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>
    <receiver android:name=".LocationReceiver" >
        <intent-filter>
            <action android:name="bdd.sanalmusavir.LOCATION_READY" />
        </intent-filter>
    </receiver>
</application>

当然,这是我的code只是其中的一部分。同样的事情,他们的服务之前,我试图通过定期添加报警但那是耗尽的电池为好。

Of course, this is just part of my code. I tried before same thing with a service by adding alarms periodically but that was draining battery as well.

在这种做法,让我们说我设置的时间间隔为10分钟,当我注册的位置更新(requestLocationUpdates),我得到的位置更新的随机时间间隔(每30秒左右)。我怎样才能摆脱它?有什么不对的检查,如果通过,并上传到服务器,如果这样的10分

In this practice, let's say I set the interval to 10 minutes, and when I register location updates (requestLocationUpdates), I am getting location updates random intervals (every 30 seconds or so). How can I get rid of it? What is wrong with checking if 10 minutes passed and uploading to server if so?

当我检查count_ignored 1天之后count_uploaded,忽视数为2100〜并上传数为50〜(上传数为真)。而我的应用程序用于90分钟的CPU,这是不可接受的。 (上传应用程序的一部分只是呼吁在网络上的网址,用的Htt prequest)。

When I check count_ignored and count_uploaded after 1 day, ignored count was 2100~ and uploaded count was 50~ (upload count was true). And my application was used 90 minutes of CPU, which was unacceptable. (Upload part of application is just calling an URL on the web, with HttpRequest).

如何实现更好的设计?有什么建议?

How can I implement better design? Any suggestions?

推荐答案

在LocationManager.requestLocationUpdates领域minInterval需要一毫秒的时间。你逝去的600,这将导致经理解雇每600毫秒或基本一样快,因为它可以。我认为你只是缺少一个* 1000在那里。

The field minInterval on LocationManager.requestLocationUpdates takes a millisecond time. You are passing 600 which will cause the manager to fire every 600ms or basically as fast as it can. I think you're just missing a *1000 in there.

这篇关于获得在背景ANDROID网络位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆