2012-02-03 3 views
0

나는 안드로이드 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 ~ (업로드 횟수가 참)입니다. 그리고 내 응용 프로그램은 90 분의 CPU를 사용하여 받아 들일 수 없었습니다. (응용 프로그램의 일부를 업로드하는 것은 HttpRequest와 함께 웹에서 URL을 호출하는 것입니다.)

더 나은 디자인을 구현하려면 어떻게해야합니까? 어떤 제안?

답변

0

LocationManager.requestLocationUpdates의 minInterval 필드에는 밀리 초가 소요됩니다. 당신은 관리자를 600ms마다 또는 기본적으로 최대한 빨리 발사하게하는 600을 통과시킵니다. 나는 당신이 거기에 1000을 놓친 것 같아.

+0

테스트 목적으로 만 사용되었습니다. 죄송합니다 내 게시물에 그것을 언급하는 것을 잊어 버렸습니다. 내 기본 설정에서 600 * 1000을 사용하고 있습니다. 내 게시물을 수정했습니다. – EvanBlack