2011-02-24 59 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”,则输出为: Intent {act = some_other_action(has extras)}
  • 对于“still_something_different”,我没有收到更新。

这种不一致的行为是否有合理的解释?

+0

对不起,上面的代码有点不清楚。所以你注册一个永远不会开火的广播(或者至少我看不到)。然后,当你获得gps更新时,你要求一个空白的意图被包裹在PendingIntent中?你如何得到任何输出?我猜你的问题可能与错字有关? – 2011-03-05 18:28:05

+0

你是对的,上面的代码有一个错字:PendingIntent用“some_action”触发一个Intent。然后我改变过滤器中的动作,并获得预期的输出,或者当我根本不期望任何输出时获得奇怪的东西。 – AnonymousCoward 2011-03-10 19:01:31

+0

,以使其更清楚一点:第一个和最后一个案例是预期的(动作匹配,所以我得到的更新,或不匹配,没有更新)。然而,第二种情况既奇怪又意外。我的直觉是,它可能是达尔维克缓存相关... – AnonymousCoward 2011-03-10 19:03:15

回答

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); 

希望它有帮助。