I am experimenting with requestLocationUpdates(String provider, long minTime, float minDistance, PendingIntent intent)and BroadcastReceiver. The code is as follows:
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.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);
LocationManager locationManager = (LocationManager)
getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 3000, 0, pending);
}
protected void someCallback(Intent intent) {
printObject(intent);
}
}
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);
}
}
When I run this on the device, the output depends on the value that I use for the action in IntentFilter.
- for "some_action", output: Intent {act = some_action cat = [android.intent.category.DEFAULT] (has additional functions)}
- for "some_other_action", output: Intent {act = some_other_action (has additional functions)}
- for "still_something_different", I am not getting updates.
Is there a reasonable explanation for this inconsistent behavior?
source
share