私はAndroid OSで位置情報を取得するためにここで多くのスレッドから研究され、いくつかのデザインを試しました。しかし、私は毎回たくさんのバッテリーを使い切っていましたが、それを取り除くことはできませんでした。ここに私のコードの一部があります。もちろんアンドロイドでバックグラウンドでネットワークの場所を取得
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
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
<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>
、これは私のコードのほんの一部です。定期的にアラームを追加することで、同じことをサービスで試してみましたが、それもバッテリーを消耗していました。
この例では、間隔を10分に設定し、場所の更新(requestLocationUpdates)を登録すると、場所の更新がランダムに(30秒ごとに)更新されています。どうすればそれを取り除くことができますか?もし10分が経過した場合にチェックし、もしそうならサーバにアップロードするのは間違っていますか?
1日後にcount_ignoredとcount_uploadedをチェックすると、無視されたカウントは2100〜であり、アップロードされたカウントは50〜(アップロードカウントがtrue)でした。私のアプリケーションは90分のCPUで使用されていましたが、これは受け入れられませんでした。 (アプリケーションの一部をアップロードするだけで、HttpRequestを使用してWeb上でURLが呼び出されます)。
より優れたデザインを実装するにはどうすればよいですか?助言がありますか?
これはテスト用です。申し訳ありませんが私のポストでそれを言及するのを忘れました。私は600 * 1000である私の好みからそれを使用しています。私のポストを修正しました。 – EvanBlack