2011-02-24 3 views
3

requestLocationUpdates(String provider, long minTime, float minDistance, PendingIntent intent)과 BroadcastReceiver를 실험하고 있습니다. 내가 장치에서이 프로그램을 실행할 때장치에서 IntentFilter 해상도로 이상한 동작이 발생했습니다.

// PendingLocationDemo.java 
public class PendingLocationDemo extends Activity { 

    public TextView output; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     output = (TextView) findViewById(R.id.output); 
     SomeReceiver receiver = new SomeReceiver(this); 
     IntentFilter filter = new IntentFilter(); 
     filter.addAction("some_action"); 
     // filter.addAction("some_other_action"); 
     // filter.addAction("still_something_different"); 
     filter.addCategory(Intent.CATEGORY_DEFAULT); 
     getApplicationContext().registerReceiver(receiver, filter); 
     Intent intent = new Intent(); 
     intent.setAction("some_action"); 
     PendingIntent pending = PendingIntent.getBroadcast(
       getApplicationContext(), 0, intent, 
       PendingIntent.FLAG_CANCEL_CURRENT); 
     // Get the location manager 
     LocationManager locationManager = (LocationManager) 
       getSystemService(LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(
       LocationManager.GPS_PROVIDER, 3000, 0, pending); 
    } 

    protected void someCallback(Intent intent) { 
     printObject(intent); 
    } 
} 

// SomeReceiver.java 
public class SomeReceiver extends BroadcastReceiver { 

    PendingLocationDemo caller; 

    public SomeReceiver(PendingLocationDemo caller) { 
     this.caller = caller; 
    } 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     caller.someCallback(intent); 
    } 
} 

가 출력 내가로 IntentFilter에서 작업에 사용하는 값에 따라 다음과 같이 코드입니다.

"some_action"의
  • 출력은 : 텐트 {법 = some_action 고양이 = android.intent.category.DEFAULT (엑스트라를 가짐)} "some_other_action"의
  • 출력은 : 의도 {act = some_other_action (엑스트라 있음)}
  • "still_something_different"에 대한 업데이트를받지 못했습니다.

이러한 일관성없는 동작에 대한 합리적인 설명이 있습니까?

+0

죄송합니다. 위의 코드는 약간 불명확합니다. 따라서 절대 발사하지 않는 방송에 등록하십시오 (또는 최소한 나는 어디를 보지 못합니다). 그런 다음 gps 업데이트를받을 때 PendingIntent에 래핑 된 빈 의도를 요청합니다. 어떻게 출력 되니? 귀하의 문제는 오타가있을 수 있습니다. –

+0

맞습니다. 위의 코드는 오타가 있습니다. PendingIntent는 "some_action"으로 인 텐트를 실행합니다. 그런 다음 필자는 필터에서 동작을 변경하고 출력이 전혀 필요하지 않을 때 예상 출력이나 이상한 것을 얻습니다. – AnonymousCoward

+0

을 사용하면 첫 번째와 마지막 경우가 예상됩니다 (동작이 일치하므로 업데이트가 있거나 불일치 및 업데이트가 없음). 그러나 두 번째 경우는 이상하고 예기치 않은 경우입니다. 내 직감은 dalvik-cache 관련일지도 모른다는 것입니다 ... – AnonymousCoward

답변

0

이 이상한 동작은 브로드 캐스트 수신기의 등록 때문입니다. 브로드 캐스트 리시버를 등록하는 대신 브로드 캐스트 리시버 인 위치 청취자 내에서 코드를 작성하고 백그라운드에서 위치 업데이트를 청취 할 수 있습니다.

final LocationManager locationManager = (LocationManager) context 
     .getSystemService(Context.LOCATION_SERVICE); 
LocationListener locationListener = new LocationListener() { 

    public void onLocationChanged(Location location) { 
     // Code for action you wish to perform on Location Changed. 
    } 

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

    public void onProviderEnabled(String provider) {} 

    public void onProviderDisabled(String provider) {} 
}; 
// ... 
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 
      3000, locationListener); 

희망이 있습니다.

관련 문제